
com.flexport.api.resources.shipment.ShipmentClient Maven / Gradle / Ivy
The newest version!
package com.flexport.api.resources.shipment;
import com.flexport.api.core.ClientOptions;
import com.flexport.api.core.ObjectMappers;
import com.flexport.api.resources.shipment.requests.ShipmentListRequest;
import com.flexport.api.resources.shipment.requests.ShipmentsShareableRequest;
import com.flexport.api.resources.shipment.requests.UpdateShipment;
import com.flexport.api.types.ShipmentListResponse;
import com.flexport.api.types.ShipmentRetrieveResponse;
import com.flexport.api.types.ShipmentUpdateResponse;
import com.flexport.api.types.ShipmentsShareableResponse;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class ShipmentClient {
protected final ClientOptions clientOptions;
public ShipmentClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
public ShipmentListResponse list(ShipmentListRequest request) {
HttpUrl.Builder _httpUrl = HttpUrl.parse(
this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("shipments");
if (request.getPage().isPresent()) {
_httpUrl.addQueryParameter("page", request.getPage().get().toString());
}
if (request.getPer().isPresent()) {
_httpUrl.addQueryParameter("per", request.getPer().get().toString());
}
if (request.getSort().isPresent()) {
_httpUrl.addQueryParameter("sort", request.getSort().get().toString());
}
if (request.getDirection().isPresent()) {
_httpUrl.addQueryParameter("direction", request.getDirection().get().toString());
}
if (request.getFTransportationMode().isPresent()) {
_httpUrl.addQueryParameter(
"f.transportation_mode",
request.getFTransportationMode().get().toString());
}
if (request.getFStatus().isPresent()) {
_httpUrl.addQueryParameter("f.status", request.getFStatus().get().toString());
}
if (request.getFStatusesAny().isPresent()) {
_httpUrl.addQueryParameter(
"f.statuses.any", request.getFStatusesAny().get().toString());
}
if (request.getFContainerNumber().isPresent()) {
_httpUrl.addQueryParameter(
"f.container_number", request.getFContainerNumber().get());
}
if (request.getFPurchaseOrder().isPresent()) {
_httpUrl.addQueryParameter(
"f.purchase_order", request.getFPurchaseOrder().get());
}
if (request.getFSku().isPresent()) {
_httpUrl.addQueryParameter("f.sku", request.getFSku().get());
}
if (request.getFUpdatedAtGt().isPresent()) {
_httpUrl.addQueryParameter(
"f.updated_at.gt", request.getFUpdatedAtGt().get());
}
if (request.getFUpdatedAtLt().isPresent()) {
_httpUrl.addQueryParameter(
"f.updated_at.lt", request.getFUpdatedAtLt().get());
}
if (request.getFArchivedAtGt().isPresent()) {
_httpUrl.addQueryParameter(
"f.archived_at.gt", request.getFArchivedAtGt().get());
}
if (request.getFArchivedAtLt().isPresent()) {
_httpUrl.addQueryParameter(
"f.archived_at.lt", request.getFArchivedAtLt().get());
}
if (request.getFMasterBillNumber().isPresent()) {
_httpUrl.addQueryParameter(
"f.master_bill_number", request.getFMasterBillNumber().get());
}
if (request.getFHouseBillNumber().isPresent()) {
_httpUrl.addQueryParameter(
"f.house_bill_number", request.getFHouseBillNumber().get());
}
if (request.getFConsigneeExternalRef().isPresent()) {
_httpUrl.addQueryParameter(
"f.consignee_external_ref",
request.getFConsigneeExternalRef().get());
}
if (request.getFOriginAddressRef().isPresent()) {
_httpUrl.addQueryParameter(
"f.origin_address_ref", request.getFOriginAddressRef().get());
}
if (request.getFDestinationAddressRef().isPresent()) {
_httpUrl.addQueryParameter(
"f.destination_address_ref",
request.getFDestinationAddressRef().get());
}
if (request.getFMetadataYourMetadataKey().isPresent()) {
_httpUrl.addQueryParameter(
"f.metadata.YOUR_METADATA_KEY",
request.getFMetadataYourMetadataKey().get());
}
if (request.getFWantsFreightManagementBco().isPresent()) {
_httpUrl.addQueryParameter(
"f.wants_freight_management_bco",
request.getFWantsFreightManagementBco().get().toString());
}
if (request.getFWantsFlexportFreight().isPresent()) {
_httpUrl.addQueryParameter(
"f.wants_flexport_freight",
request.getFWantsFlexportFreight().get().toString());
}
if (request.getFWantsImportCustomsService().isPresent()) {
_httpUrl.addQueryParameter(
"f.wants_import_customs_service",
request.getFWantsImportCustomsService().get().toString());
}
if (request.getFWantsExportCustomsService().isPresent()) {
_httpUrl.addQueryParameter(
"f.wants_export_customs_service",
request.getFWantsExportCustomsService().get().toString());
}
if (request.getFWantsCommercialInvoiceTranscription().isPresent()) {
_httpUrl.addQueryParameter(
"f.wants_commercial_invoice_transcription",
request.getFWantsCommercialInvoiceTranscription().get().toString());
}
if (request.getFWantsFlexportInsurance().isPresent()) {
_httpUrl.addQueryParameter(
"f.wants_flexport_insurance",
request.getFWantsFlexportInsurance().get().toString());
}
if (request.getFWantsPickupService().isPresent()) {
_httpUrl.addQueryParameter(
"f.wants_pickup_service",
request.getFWantsPickupService().get().toString());
}
if (request.getFWantsTradeDeclarationService().isPresent()) {
_httpUrl.addQueryParameter(
"f.wants_trade_declaration_service",
request.getFWantsTradeDeclarationService().get().toString());
}
if (request.getFVisibilityOnly().isPresent()) {
_httpUrl.addQueryParameter(
"f.visibility_only", request.getFVisibilityOnly().get().toString());
}
if (request.getFWantsDeliveryService().isPresent()) {
_httpUrl.addQueryParameter(
"f.wants_delivery_service",
request.getFWantsDeliveryService().get().toString());
}
if (request.getFBelongsToABuyersConsol().isPresent()) {
_httpUrl.addQueryParameter(
"f.belongs_to_a_buyers_consol",
request.getFBelongsToABuyersConsol().get().toString());
}
RequestBody _requestBody = null;
Request.Builder _requestBuilder = new Request.Builder()
.url(_httpUrl.build())
.method("GET", _requestBody)
.headers(Headers.of(clientOptions.headers()))
.addHeader("Content-Type", "application/json");
Request _request = _requestBuilder.build();
try {
Response _response = clientOptions.httpClient().newCall(_request).execute();
if (_response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), ShipmentListResponse.class);
}
throw new RuntimeException();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public ShipmentRetrieveResponse retrieve(String id) {
HttpUrl _httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("shipments")
.addPathSegment(id)
.build();
Request _request = new Request.Builder()
.url(_httpUrl)
.method("GET", null)
.headers(Headers.of(clientOptions.headers()))
.addHeader("Content-Type", "application/json")
.build();
try {
Response _response = clientOptions.httpClient().newCall(_request).execute();
if (_response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), ShipmentRetrieveResponse.class);
}
throw new RuntimeException();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public ShipmentUpdateResponse update(int id, UpdateShipment request) {
HttpUrl _httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("shipments")
.addPathSegment(Integer.toString(id))
.build();
Map _requestBodyProperties = new HashMap<>();
_requestBodyProperties.put("metadata", request.getMetadata());
RequestBody _requestBody;
try {
_requestBody = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(_requestBodyProperties),
MediaType.parse("application/json"));
} catch (Exception e) {
throw new RuntimeException(e);
}
Request.Builder _requestBuilder = new Request.Builder()
.url(_httpUrl)
.method("PATCH", _requestBody)
.headers(Headers.of(clientOptions.headers()))
.addHeader("Content-Type", "application/json");
Request _request = _requestBuilder.build();
try {
Response _response = clientOptions.httpClient().newCall(_request).execute();
if (_response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), ShipmentUpdateResponse.class);
}
throw new RuntimeException();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public ShipmentsShareableResponse shipmentsShareable(ShipmentsShareableRequest request) {
HttpUrl _httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("shipments_shareable")
.build();
Map _requestBodyProperties = new HashMap<>();
_requestBodyProperties.put("shipment_ids", request.getShipmentIds());
RequestBody _requestBody;
try {
_requestBody = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(_requestBodyProperties),
MediaType.parse("application/json"));
} catch (Exception e) {
throw new RuntimeException(e);
}
Request.Builder _requestBuilder = new Request.Builder()
.url(_httpUrl)
.method("POST", _requestBody)
.headers(Headers.of(clientOptions.headers()))
.addHeader("Content-Type", "application/json");
Request _request = _requestBuilder.build();
try {
Response _response = clientOptions.httpClient().newCall(_request).execute();
if (_response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), ShipmentsShareableResponse.class);
}
throw new RuntimeException();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy