Class DefaultShipEngineService
- java.lang.Object
-
- com.broadleafcommerce.shipping.service.integrations.shipengine.service.DefaultShipEngineService
-
- All Implemented Interfaces:
ShipEngineService
public class DefaultShipEngineService extends Object implements ShipEngineService
Interacts with the ShipEngine API- Author:
- Cade Rea (cade-rea)
-
-
Constructor Summary
Constructors Constructor Description DefaultShipEngineService(org.springframework.web.reactive.function.client.WebClient webClient, ShipEngineProperties shipEngineProperties, WeightCalculationService weightCalculationService)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected BulkRateRequest
buildBulkRateRequest(@NonNull List<com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest> fulfillmentPricingRequests)
Build a ShipEngineBulkRateRequest
from a List ofFulfillmentPricingRequest
.protected EstimateRequest
buildEstimateRequest(@NonNull com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Build a ShipEngineEstimateRequest
from aFulfillmentPricingRequest
.protected LabelDownloadRequest
buildLabelFromRateRequest()
Build a ShipEngineLabelDownloadRequest
.protected LabelRequest
buildLabelRequest(@NonNull com.broadleafcommerce.order.client.domain.OrderFulfillment orderFulfillment, com.broadleafcommerce.order.common.domain.Address fromAddress)
Build a ShipEngineLabelRequest
from anOrderFulfillment
.protected List<ShipEnginePackage>
buildPackageList(@NonNull com.broadleafcommerce.shipping.domain.Weight packageWeight)
Build aShipEnginePackage
with the given weight.protected RatesRequest
buildRatesRequest(@NonNull com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Build a ShipEngineRatesRequest
from aFulfillmentPricingRequest
.protected ShipEngineAddress
buildShipFromAddress(com.broadleafcommerce.order.common.domain.Address fromAddress)
Build aShipEngineAddress
from anAddress
.protected ShipEngineAddress
buildShipFromAddress(com.broadleafcommerce.shipping.domain.ShippingAddress fromAddress)
Build aShipEngineAddress
from aShippingAddress
.protected ShipEngineShipment
buildShipment(@NonNull com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Build aShipEngineShipment
from the givenFulfillmentPricingRequest
.protected ShipEngineAddress
buildShipToAddress(@NonNull com.broadleafcommerce.order.common.domain.Address toAddress)
Build aShipEngineAddress
from anAddress
protected ShipEngineAddress
buildShipToAddress(@NonNull com.broadleafcommerce.shipping.domain.ShippingAddress toAddress, String customerName)
Build aShipEngineAddress
from aShippingAddress
and customer name.protected <I,O>
OcallShipEngineAPI(ShipEngineRequest<I,O> shipEngineRequest)
Calls the ShipEngine API.protected String
getAddressValidationMode()
Get the configured address validation modeBulkRateResponseList
getBulkRates(@NonNull List<com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest> fulfillmentPricingRequests)
Get rates for a list ofFulfillmentPricingRequest
.protected List<String>
getCarrierIds()
Get theCarrier
s enabled in the ShipEngine dashboard.List<String>
getCarriers()
Get a list ofCarrier
that are configured in the ShipEngine account.EstimateResponse
getEstimate(@NonNull com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Get rate estimates from all carriers for the givenFulfillmentPricingRequest
.RatesResponse
getRates(@NonNull com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Get all available rates for the givenFulfillmentPricingRequest
.protected ShipEngineProperties
getShipEngineProperties()
protected org.springframework.web.reactive.function.client.WebClient
getWebClient()
protected WeightCalculationService
getWeightCalculationService()
protected List<String>
processCarrierResponse(@NonNull ListCarriersResponse listCarriersResponse)
Process aListCarriersResponse
from ShipEngine into a list of carrier id strings.LabelResponse
purchaseLabel(@NonNull com.broadleafcommerce.order.client.domain.OrderFulfillment orderFulfillment, com.broadleafcommerce.order.common.domain.Address fromAddress)
Purchase a label with anOrderFulfillment
.LabelResponse
purchaseLabelFromRate(@NonNull String rateId)
Purchase a label from a previously quotedRate
.LabelResponse
retrieveLabel(@NonNull String labelId)
Retrieve an existing shipping label.Rate
retrieveRate(@NonNull String rateId)
Retrieve an existingRate
.RateResponse
retrieveRateForShipment(@NonNull String shipmentId)
Retrieve a previously calculated rate with the shipment id.
-
-
-
Constructor Detail
-
DefaultShipEngineService
public DefaultShipEngineService(org.springframework.web.reactive.function.client.WebClient webClient, ShipEngineProperties shipEngineProperties, WeightCalculationService weightCalculationService)
-
-
Method Detail
-
getEstimate
public EstimateResponse getEstimate(@NonNull @NonNull com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Description copied from interface:ShipEngineService
Get rate estimates from all carriers for the givenFulfillmentPricingRequest
. Estimates can be retrieved with less information than is required for a rate quote.- Specified by:
getEstimate
in interfaceShipEngineService
- Parameters:
fulfillmentPricingRequest
- Provides addresses and items for a shipment.- Returns:
- An
EstimateResponse
with estimatedRate
s
-
getRates
public RatesResponse getRates(@NonNull @NonNull com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Description copied from interface:ShipEngineService
Get all available rates for the givenFulfillmentPricingRequest
.- Specified by:
getRates
in interfaceShipEngineService
- Parameters:
fulfillmentPricingRequest
- Provides addresses and items for a shipment.- Returns:
- A
RatesResponse
with a list ofRate
s.
-
getBulkRates
public BulkRateResponseList getBulkRates(@NonNull @NonNull List<com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest> fulfillmentPricingRequests)
Description copied from interface:ShipEngineService
Get rates for a list ofFulfillmentPricingRequest
. This is an asynchronous operation on ShipEngine. Rates should be retrieved withShipEngineService.retrieveRateForShipment(java.lang.String)
- Specified by:
getBulkRates
in interfaceShipEngineService
- Parameters:
fulfillmentPricingRequests
- Provides addresses and items for a shipment.- Returns:
- A
BulkRateResponseList
with a list of statuses for each shipment submitted. - See Also:
- ShipEngine Docs - Rate Multiple Shipments
-
retrieveRateForShipment
public RateResponse retrieveRateForShipment(@NonNull @NonNull String shipmentId)
Description copied from interface:ShipEngineService
Retrieve a previously calculated rate with the shipment id. This can be used withShipEngineService.getBulkRates(java.util.List<com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest>)
to retrieve the rates from a bulk request.- Specified by:
retrieveRateForShipment
in interfaceShipEngineService
- Parameters:
shipmentId
- The ShipEngine id of the shipment- Returns:
- A
RatesResponse
with a list ofRate
s.
-
purchaseLabel
public LabelResponse purchaseLabel(@NonNull @NonNull com.broadleafcommerce.order.client.domain.OrderFulfillment orderFulfillment, @Nullable com.broadleafcommerce.order.common.domain.Address fromAddress)
Description copied from interface:ShipEngineService
Purchase a label with anOrderFulfillment
. TheFulfillmentOption
should be set on theOrderFulfillment
.- Specified by:
purchaseLabel
in interfaceShipEngineService
- Parameters:
orderFulfillment
- Provides addresses and items for a shipment.fromAddress
- The address that the package will be sent from. If not provided, will use the default address configured with properties. SeeShipEngineProperties.DefaultFromAddress
.- Returns:
- A
LabelResponse
with a URL to download the shipping label.
-
purchaseLabelFromRate
public LabelResponse purchaseLabelFromRate(@NonNull @NonNull String rateId)
Description copied from interface:ShipEngineService
Purchase a label from a previously quotedRate
.- Specified by:
purchaseLabelFromRate
in interfaceShipEngineService
- Parameters:
rateId
- The ShipEngine rate id.- Returns:
- A
LabelResponse
with a URL to download the shipping label.
-
getCarriers
public List<String> getCarriers()
Description copied from interface:ShipEngineService
Get a list ofCarrier
that are configured in the ShipEngine account.- Specified by:
getCarriers
in interfaceShipEngineService
- Returns:
- A list of Carriers that are configured in the ShipEngine dashboard.
-
retrieveRate
public Rate retrieveRate(@NonNull @NonNull String rateId)
Description copied from interface:ShipEngineService
Retrieve an existingRate
.- Specified by:
retrieveRate
in interfaceShipEngineService
- Parameters:
rateId
- The ShipEngine rate id.- Returns:
- The
Rate
associated with the given rateId.
-
retrieveLabel
public LabelResponse retrieveLabel(@NonNull @NonNull String labelId)
Description copied from interface:ShipEngineService
Retrieve an existing shipping label. This method does not purchase labels.- Specified by:
retrieveLabel
in interfaceShipEngineService
- Parameters:
labelId
- The ShipEngine label id.- Returns:
- A
LabelResponse
with a URL to download the shipping label.
-
buildEstimateRequest
protected EstimateRequest buildEstimateRequest(@NonNull @NonNull com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Build a ShipEngineEstimateRequest
from aFulfillmentPricingRequest
.- Parameters:
fulfillmentPricingRequest
- Provides addresses and items for a shipment.- Returns:
- An
EstimateRequest
to send to ShipEngine.
-
buildRatesRequest
protected RatesRequest buildRatesRequest(@NonNull @NonNull com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Build a ShipEngineRatesRequest
from aFulfillmentPricingRequest
.- Parameters:
fulfillmentPricingRequest
- Provides addresses and items for a shipment.- Returns:
- A
RatesRequest
to send to ShipEngine.
-
buildBulkRateRequest
protected BulkRateRequest buildBulkRateRequest(@NonNull @NonNull List<com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest> fulfillmentPricingRequests)
Build a ShipEngineBulkRateRequest
from a List ofFulfillmentPricingRequest
.- Parameters:
fulfillmentPricingRequests
- Provides addresses and items for shipments.- Returns:
- A
BulkRateRequest
to send to ShipEngine.
-
buildLabelRequest
protected LabelRequest buildLabelRequest(@NonNull @NonNull com.broadleafcommerce.order.client.domain.OrderFulfillment orderFulfillment, @Nullable com.broadleafcommerce.order.common.domain.Address fromAddress)
Build a ShipEngineLabelRequest
from anOrderFulfillment
.- Parameters:
orderFulfillment
- Provides addresses and items for a shipment.fromAddress
- The address that the package will be sent from. If not provided, will use the default address configured with properties. SeeShipEngineProperties.DefaultFromAddress
.- Returns:
- A
LabelRequest
to send to ShipEngine.
-
buildLabelFromRateRequest
protected LabelDownloadRequest buildLabelFromRateRequest()
Build a ShipEngineLabelDownloadRequest
.- Returns:
- A
LabelDownloadRequest
to send to ShipEngine.
-
processCarrierResponse
protected List<String> processCarrierResponse(@NonNull @NonNull ListCarriersResponse listCarriersResponse)
Process aListCarriersResponse
from ShipEngine into a list of carrier id strings.- Parameters:
listCarriersResponse
- TheListCarriersResponse
from ShipEngine.- Returns:
- A list of
Carrier
ids.
-
callShipEngineAPI
protected <I,O> O callShipEngineAPI(ShipEngineRequest<I,O> shipEngineRequest)
Calls the ShipEngine API. The givenShipEngineRequest
should have all data needed for the API call.- Type Parameters:
I
- The type of Input, ie the type of the body of the request.O
- The type of Output, ie the response type- Parameters:
shipEngineRequest
- Holds request header and body data needed by WebClient to exchange with the ShipEngine API.- Returns:
- An object of type O
-
getCarrierIds
protected List<String> getCarrierIds()
Get theCarrier
s enabled in the ShipEngine dashboard. This will make an API call unlessShipEngineProperties.getCarriersFromProperties
is true. In that case, carrier ids can be specified inShipEngineProperties.carriers
.- Returns:
- A list of
Carrier
id strings.
-
buildShipment
protected ShipEngineShipment buildShipment(@NonNull @NonNull com.broadleafcommerce.shipping.domain.FulfillmentPricingRequest fulfillmentPricingRequest)
Build aShipEngineShipment
from the givenFulfillmentPricingRequest
.- Parameters:
fulfillmentPricingRequest
- Provides addresses and items for a shipment.- Returns:
- A
ShipEngineShipment
to be included in a ShipEngine request.
-
buildPackageList
protected List<ShipEnginePackage> buildPackageList(@NonNull @NonNull com.broadleafcommerce.shipping.domain.Weight packageWeight)
Build aShipEnginePackage
with the given weight.- Parameters:
packageWeight
- TheWeight
of the package.- Returns:
- A singleton list of
ShipEnginePackage
to include in other requests.
-
getAddressValidationMode
protected String getAddressValidationMode()
Get the configured address validation mode- Returns:
- The address validation mode
-
buildShipToAddress
protected ShipEngineAddress buildShipToAddress(@NonNull @NonNull com.broadleafcommerce.shipping.domain.ShippingAddress toAddress, String customerName)
Build aShipEngineAddress
from aShippingAddress
and customer name.- Parameters:
toAddress
- The destination address for the package.customerName
- The name of the recipient.- Returns:
- A
ShipEngineAddress
to include in a ShipEngine request.
-
buildShipToAddress
protected ShipEngineAddress buildShipToAddress(@NonNull @NonNull com.broadleafcommerce.order.common.domain.Address toAddress)
Build aShipEngineAddress
from anAddress
- Parameters:
toAddress
- The destination address for the package.- Returns:
- A
ShipEngineAddress
to include in a ShipEngine request.
-
buildShipFromAddress
protected ShipEngineAddress buildShipFromAddress(@Nullable com.broadleafcommerce.shipping.domain.ShippingAddress fromAddress)
Build aShipEngineAddress
from aShippingAddress
. If the provided address is null, this will use the default FROM address configured withShipEngineProperties.DefaultFromAddress
.- Parameters:
fromAddress
- The address that the package is being shipped from.- Returns:
- A
ShipEngineAddress
to include in a ShipEngine request.
-
buildShipFromAddress
protected ShipEngineAddress buildShipFromAddress(@Nullable com.broadleafcommerce.order.common.domain.Address fromAddress)
Build aShipEngineAddress
from anAddress
. If the provided address is null, this will use the default FROM address configured withShipEngineProperties.DefaultFromAddress
.- Parameters:
fromAddress
- The address that the package is being shipped from.- Returns:
- A
ShipEngineAddress
to include in a ShipEngine request.
-
getWebClient
protected org.springframework.web.reactive.function.client.WebClient getWebClient()
-
getShipEngineProperties
protected ShipEngineProperties getShipEngineProperties()
-
getWeightCalculationService
protected WeightCalculationService getWeightCalculationService()
-
-