All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sphere.sdk.shippingmethods.queries.ShippingMethodsByLocationGetImpl Maven / Gradle / Ivy

There is a newer version: 2.16.0
Show newest version
package io.sphere.sdk.shippingmethods.queries;

import com.fasterxml.jackson.core.type.TypeReference;
import com.neovisionaries.i18n.CountryCode;
import io.sphere.sdk.http.NameValuePair;
import io.sphere.sdk.http.UrlQueryBuilder;
import io.sphere.sdk.queries.MetaModelGetDslImpl;
import io.sphere.sdk.shippingmethods.ShippingMethod;
import io.sphere.sdk.shippingmethods.expansion.ShippingMethodExpansionModel;

import javax.annotation.Nullable;
import javax.money.CurrencyUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
 * Retrieves all the shipping methods that can ship to the given location.
 * If the currency parameter is given, then the shipping methods must also have a rate defined in the specified currency.
 *
 * {@include.example io.sphere.sdk.shippingmethods.queries.ShippingMethodsByLocationGetIntegrationTest#execution()}
 */
final class ShippingMethodsByLocationGetImpl extends MetaModelGetDslImpl, ShippingMethod, ShippingMethodsByCartGet, ShippingMethodExpansionModel>
        implements ShippingMethodsByLocationGet {
    ShippingMethodsByLocationGetImpl(final CountryCode country, @Nullable final String state, @Nullable final CurrencyUnit currency) {
        super(ShippingMethodEndpoint.ENDPOINT.withTypeReference(new TypeReference>() {
            @Override
            public String toString() {
                return "TypeReference>";
            }
        }), "", ShippingMethodExpansionModel.of(), ShippingMethodsByCartGetImpl::new, createQueryParameters(country, state, currency));
    }

    private static List createQueryParameters(final CountryCode country, @Nullable final String state, @Nullable final CurrencyUnit currency) {
        final List queryParameters = new ArrayList<>();
        queryParameters.add(NameValuePair.of("country", country.getAlpha2()));
        Optional.ofNullable(state).ifPresent(x -> queryParameters.add(NameValuePair.of("state", x)));
        Optional.ofNullable(currency).ifPresent(x -> queryParameters.add(NameValuePair.of("currency", x.getCurrencyCode())));

        return queryParameters;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy