diff --git a/src/main/java/io/ljunggren/ups/api/UpsApi.java b/src/main/java/io/ljunggren/ups/api/UpsApi.java index 0fc87c1..7066090 100644 --- a/src/main/java/io/ljunggren/ups/api/UpsApi.java +++ b/src/main/java/io/ljunggren/ups/api/UpsApi.java @@ -5,44 +5,68 @@ import org.apache.commons.lang3.StringUtils; +import io.ljunggren.ups.api.model.Address; import io.ljunggren.ups.api.model.AuthToken; -import io.ljunggren.ups.api.service.AuthorizationService; +import io.ljunggren.ups.api.model.UpsResponse; +import io.ljunggren.ups.api.model.rate.request.RateRequest; +import io.ljunggren.ups.api.service.OauthService; +import io.ljunggren.ups.api.service.RateService; import io.ljunggren.ups.api.service.TrackingService; +import io.ljunggren.ups.api.service.AddressService; public class UpsApi { - private AuthorizationService authorizationService; - private TrackingService trackingService; - private String clientId; private String clientSecret; private String accountNumber; - private UpsProperties properties; + + private OauthService oauthService; + private TrackingService trackingService; + private RateService rateService; + private AddressService addressService; public UpsApi(UpsEnvironment environment, String clientId, String clientSecret, String accountNumber) { this.clientId = clientId; this.clientSecret = clientSecret; this.accountNumber = accountNumber; - this.properties = new UpsProperties(environment); - this.authorizationService = new AuthorizationService(properties); - this.trackingService = new TrackingService(properties); - } - - public UpsResponse track(String trackingNumber) throws Exception { - AuthToken authToken = getAuthToken(); - return track(trackingNumber, authToken); - } - - public UpsResponse track(String trackingNumber, AuthToken authToken) throws Exception { - if (!isValid(trackingNumber)) { - return null; - } - String bearerToken = authToken.getAccessToken(); - return trackingService.track(trackingNumber, bearerToken); + this.oauthService = new OauthService(environment.getDomain()); + this.trackingService = new TrackingService(environment.getDomain()); + this.rateService = new RateService(environment.getDomain()); + this.addressService = new AddressService(environment.getDomain()); } public AuthToken getAuthToken() throws Exception { - return authorizationService.getAuthToken(clientId, clientSecret, accountNumber); + return oauthService.getAuthToken(clientId, clientSecret, accountNumber); + } + + public UpsResponse track(String trackingNumber) throws Exception { + String accessToken = getAuthToken().getAccessToken(); + return track(trackingNumber, accessToken); + } + + public UpsResponse track(String trackingNumber, String accessToken) throws Exception { + if (!isValid(trackingNumber)) { + return null; + } + return trackingService.track(trackingNumber, accessToken); + } + + public UpsResponse requestRate(RateRequest rateRequest) throws Exception { + String accessToken = getAuthToken().getAccessToken(); + return requestRate(rateRequest, accessToken); + } + + public UpsResponse requestRate(RateRequest rateRequest, String accessToken) throws Exception { + return rateService.request(rateRequest, accessToken); + } + + public UpsResponse validateAddress(Address address) throws Exception { + String accessToken = getAuthToken().getAccessToken(); + return validateAddress(address, accessToken); + } + + public UpsResponse validateAddress(Address address, String accessToken) throws Exception { + return addressService.validate(address, accessToken); } private boolean isValid(String trackingNumber) { diff --git a/src/main/java/io/ljunggren/ups/api/UpsEnvironment.java b/src/main/java/io/ljunggren/ups/api/UpsEnvironment.java index 230478c..0578e00 100644 --- a/src/main/java/io/ljunggren/ups/api/UpsEnvironment.java +++ b/src/main/java/io/ljunggren/ups/api/UpsEnvironment.java @@ -2,7 +2,17 @@ public enum UpsEnvironment { - CIE, // (customer integration environment) - PRODUCTION + CIE("https://wwwcie.ups.com"), // (customer integration environment) + PRODUCTION("https://onlinetools.ups.com"); + + private String domain; + + private UpsEnvironment(String domain) { + this.domain = domain; + } + + public String getDomain() { + return domain; + } } diff --git a/src/main/java/io/ljunggren/ups/api/UpsProperties.java b/src/main/java/io/ljunggren/ups/api/UpsProperties.java deleted file mode 100644 index c26da31..0000000 --- a/src/main/java/io/ljunggren/ups/api/UpsProperties.java +++ /dev/null @@ -1,39 +0,0 @@ -package io.ljunggren.ups.api; - -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -public class UpsProperties { - - private static Properties properties = new Properties(); - - public UpsProperties(UpsEnvironment environment) { - Map fileMap = generateFileMap(); - setProperties(fileMap.get(environment)); - } - - private static Map generateFileMap() { - Map fileMap = new HashMap(); - fileMap.put(UpsEnvironment.CIE, "/properties/cie/ups.properties"); - fileMap.put(UpsEnvironment.PRODUCTION, "/properties/production/ups.properties"); - return fileMap; - } - - private static void setProperties(String file) { - try { - InputStream is = UpsProperties.class.getResourceAsStream(file); - properties.load(is); - is.close(); - } catch(IOException e) { - throw new RuntimeException("Error loading properties", e); - } - } - - public String getDomain() { - return properties.getProperty("domain"); - } - -} diff --git a/src/main/java/io/ljunggren/ups/api/UpsResponse.java b/src/main/java/io/ljunggren/ups/api/UpsResponse.java deleted file mode 100644 index 09a6206..0000000 --- a/src/main/java/io/ljunggren/ups/api/UpsResponse.java +++ /dev/null @@ -1,28 +0,0 @@ -package io.ljunggren.ups.api; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; - -import io.ljunggren.ups.api.model.tracking.response.Response; -import io.ljunggren.ups.api.model.tracking.response.TrackingResponse; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@AllArgsConstructor -@NoArgsConstructor -@JsonIgnoreProperties(ignoreUnknown = true) -@JsonInclude(Include.NON_NULL) -public class UpsResponse { - - @JsonProperty("trackResponse") - private TrackingResponse trackingResponse; - @JsonProperty("response") - private Response errorResponse; - -} diff --git a/src/main/java/io/ljunggren/ups/api/model/Address.java b/src/main/java/io/ljunggren/ups/api/model/Address.java new file mode 100644 index 0000000..46ab077 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/Address.java @@ -0,0 +1,28 @@ +package io.ljunggren.ups.api.model; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Address { + + @JsonProperty("AddressLine") + private List addressLines; + @JsonProperty("City") + private String city; + @JsonProperty("StateProvinceCode") + private String stateProvinceCode; + @JsonProperty("PostalCode") + private String postalCode; + @JsonProperty("CountryCode") + private String countryCode; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/Error.java b/src/main/java/io/ljunggren/ups/api/model/Error.java new file mode 100644 index 0000000..3cc7cca --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/Error.java @@ -0,0 +1,21 @@ +package io.ljunggren.ups.api.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Error { + + private String code; + private String message; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/ErrorResponse.java b/src/main/java/io/ljunggren/ups/api/model/ErrorResponse.java new file mode 100644 index 0000000..aa00245 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/ErrorResponse.java @@ -0,0 +1,31 @@ +package io.ljunggren.ups.api.model; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class ErrorResponse { + + private List errors; + + public ErrorResponse addError(Error error) { + if (errors == null) { + errors = new ArrayList<>(); + } + errors.add(error); + return this; + } + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/UpsResponse.java b/src/main/java/io/ljunggren/ups/api/model/UpsResponse.java new file mode 100644 index 0000000..b0391b0 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/UpsResponse.java @@ -0,0 +1,33 @@ +package io.ljunggren.ups.api.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import io.ljunggren.ups.api.model.rate.response.RateResponse; +import io.ljunggren.ups.api.model.tracking.response.TrackingResponse; +import io.ljunggren.ups.api.model.validate.response.XAVResponse; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class UpsResponse { + + @JsonProperty("trackResponse") + private TrackingResponse trackingResponse; + @JsonProperty("RateResponse") + private RateResponse rateResponse; + @JsonProperty("XAVResponse") + private XAVResponse xavResponse; + @JsonProperty("response") + private ErrorResponse errorResponse; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/request/CodeDescription.java b/src/main/java/io/ljunggren/ups/api/model/rate/request/CodeDescription.java new file mode 100644 index 0000000..9c3c73f --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/request/CodeDescription.java @@ -0,0 +1,24 @@ +package io.ljunggren.ups.api.model.rate.request; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class CodeDescription { + + @JsonProperty("Code") + private String code; + @JsonProperty("Description") + private String description; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/request/Dimensions.java b/src/main/java/io/ljunggren/ups/api/model/rate/request/Dimensions.java new file mode 100644 index 0000000..d4480b2 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/request/Dimensions.java @@ -0,0 +1,32 @@ +package io.ljunggren.ups.api.model.rate.request; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Dimensions { + + @JsonProperty("Length") + @JsonFormat(shape = JsonFormat.Shape.STRING) + private double lenght; + @JsonProperty("Width") + @JsonFormat(shape = JsonFormat.Shape.STRING) + private double width; + @JsonProperty("Height") + @JsonFormat(shape = JsonFormat.Shape.STRING) + private double height; + @JsonProperty("UnitOfMeasurement") + private CodeDescription unitOfMeasurement; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/request/NameAndAddress.java b/src/main/java/io/ljunggren/ups/api/model/rate/request/NameAndAddress.java new file mode 100644 index 0000000..0a72ac8 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/request/NameAndAddress.java @@ -0,0 +1,23 @@ +package io.ljunggren.ups.api.model.rate.request; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import io.ljunggren.ups.api.model.Address; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class NameAndAddress { + + @JsonProperty("Name") + private String name; + @JsonProperty("Address") + private Address address; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/request/Package.java b/src/main/java/io/ljunggren/ups/api/model/rate/request/Package.java new file mode 100644 index 0000000..003887f --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/request/Package.java @@ -0,0 +1,22 @@ +package io.ljunggren.ups.api.model.rate.request; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Package { + + @JsonProperty("Dimensions") + private Dimensions dimensions; + @JsonProperty("PackageWeight") + private PackageWeight packageWeight; + @JsonProperty("PackagingType") + private CodeDescription packagingType; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/request/PackageWeight.java b/src/main/java/io/ljunggren/ups/api/model/rate/request/PackageWeight.java new file mode 100644 index 0000000..c693881 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/request/PackageWeight.java @@ -0,0 +1,26 @@ +package io.ljunggren.ups.api.model.rate.request; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class PackageWeight { + + @JsonProperty("Weight") + @JsonFormat(shape = JsonFormat.Shape.STRING) + private Integer weight; + @JsonProperty("UnitOfMeasurement") + private CodeDescription unitOfMeasurement; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/request/RateRequest.java b/src/main/java/io/ljunggren/ups/api/model/rate/request/RateRequest.java new file mode 100644 index 0000000..10a9d2a --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/request/RateRequest.java @@ -0,0 +1,18 @@ +package io.ljunggren.ups.api.model.rate.request; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class RateRequest { + + @JsonProperty("Shipment") + private Shipment shipment; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/request/Shipment.java b/src/main/java/io/ljunggren/ups/api/model/rate/request/Shipment.java new file mode 100644 index 0000000..0421351 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/request/Shipment.java @@ -0,0 +1,34 @@ +package io.ljunggren.ups.api.model.rate.request; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Shipment { + + @JsonProperty("Shipper") + private Shipper shipper; + @JsonProperty("ShipTo") + private NameAndAddress shipTo; + @JsonProperty("ShipFrom") + private NameAndAddress shipFrom; + @JsonProperty("NumOfPieces") + private String numOfPieces; + @JsonProperty("Package") + private List packages; + @JsonProperty("ShipmentRatingOptions") + private ShipmentRatingOptions shipmentRatingOptions; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/request/ShipmentRatingOptions.java b/src/main/java/io/ljunggren/ups/api/model/rate/request/ShipmentRatingOptions.java new file mode 100644 index 0000000..0283bad --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/request/ShipmentRatingOptions.java @@ -0,0 +1,18 @@ +package io.ljunggren.ups.api.model.rate.request; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class ShipmentRatingOptions { + + @JsonProperty("NegotiatedRatesIndicator") + private String negotiatedRatesIndicator = "Y"; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/request/Shipper.java b/src/main/java/io/ljunggren/ups/api/model/rate/request/Shipper.java new file mode 100644 index 0000000..0555d9a --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/request/Shipper.java @@ -0,0 +1,27 @@ +package io.ljunggren.ups.api.model.rate.request; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import io.ljunggren.ups.api.model.Address; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Shipper { + + @JsonProperty("Name") + private String name; + @JsonProperty("Address") + private Address address; + @JsonProperty("ShipperNumber") + private String accountNumber; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/response/Charge.java b/src/main/java/io/ljunggren/ups/api/model/rate/response/Charge.java new file mode 100644 index 0000000..c72bbf2 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/response/Charge.java @@ -0,0 +1,20 @@ +package io.ljunggren.ups.api.model.rate.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Charge { + + @JsonProperty("CurrencyCode") + private String currencyCode; + @JsonProperty("MonetaryValue") + private String monetaryValue; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/response/NegotiatedRate.java b/src/main/java/io/ljunggren/ups/api/model/rate/response/NegotiatedRate.java new file mode 100644 index 0000000..0a06555 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/response/NegotiatedRate.java @@ -0,0 +1,18 @@ +package io.ljunggren.ups.api.model.rate.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class NegotiatedRate { + + @JsonProperty("TotalCharge") + private Charge totalCharge; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/response/RateResponse.java b/src/main/java/io/ljunggren/ups/api/model/rate/response/RateResponse.java new file mode 100644 index 0000000..10d1d59 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/response/RateResponse.java @@ -0,0 +1,20 @@ +package io.ljunggren.ups.api.model.rate.response; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class RateResponse { + + @JsonProperty("RatedShipment") + private List ratedShipments; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/rate/response/RatedShipment.java b/src/main/java/io/ljunggren/ups/api/model/rate/response/RatedShipment.java new file mode 100644 index 0000000..5fef09d --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/rate/response/RatedShipment.java @@ -0,0 +1,25 @@ +package io.ljunggren.ups.api.model.rate.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.ljunggren.ups.api.model.rate.request.CodeDescription; + +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class RatedShipment { + + @JsonProperty("Service") + private CodeDescription service; + @JsonProperty("TotalCharges") + private Charge totalCharges; + @JsonProperty("NegotiatedRateCharges") + private NegotiatedRate negotiatedRate; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/tracking/response/Response.java b/src/main/java/io/ljunggren/ups/api/model/tracking/response/Response.java deleted file mode 100644 index 0a4d9bc..0000000 --- a/src/main/java/io/ljunggren/ups/api/model/tracking/response/Response.java +++ /dev/null @@ -1,31 +0,0 @@ -package io.ljunggren.ups.api.model.tracking.response; - -import java.util.ArrayList; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@AllArgsConstructor -@NoArgsConstructor -@JsonIgnoreProperties(ignoreUnknown = true) -@JsonInclude(Include.NON_NULL) -public class Response { - - private List errors; - - public Response addError(MessageCode error) { - if (errors == null) { - errors = new ArrayList<>(); - } - errors.add(error); - return this; - } - -} diff --git a/src/main/java/io/ljunggren/ups/api/model/validate/request/AddressKeyFormat.java b/src/main/java/io/ljunggren/ups/api/model/validate/request/AddressKeyFormat.java new file mode 100644 index 0000000..c40d14c --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/validate/request/AddressKeyFormat.java @@ -0,0 +1,60 @@ +package io.ljunggren.ups.api.model.validate.request; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.ljunggren.ups.api.model.Address; + +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class AddressKeyFormat { + + @JsonProperty("AddressLine") + private List addressLines; + @JsonProperty("PoliticalDivision2") + private String city; + @JsonProperty("PoliticalDivision1") + private String stateProvinceCode; + @JsonProperty("PostcodePrimaryLow") + private String postalCodePrimary; + @JsonProperty("PostcodeExtendedLow") + private String postalCodeExtended; + @JsonProperty("CountryCode") + private String countryCode; + + public AddressKeyFormat(Address address) { + this.addressLines = address.getAddressLines(); + this.city = address.getCity(); + this.stateProvinceCode = address.getStateProvinceCode(); + initPostalCode(address.getPostalCode()); + this.countryCode = address.getCountryCode(); + } + + private void initPostalCode(String postalCode) { + String[] split = postalCode.split("-"); + if (split.length == 2) { + this.postalCodePrimary = split[0]; + this.postalCodeExtended = split[1]; + return; + } + this.postalCodePrimary = postalCode; + } + + public String getPostalCode() { + return postalCodeExtended == null ? postalCodePrimary : + String.format("%s-%s", postalCodePrimary, postalCodeExtended); + } + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/validate/request/XAVRequest.java b/src/main/java/io/ljunggren/ups/api/model/validate/request/XAVRequest.java new file mode 100644 index 0000000..c5dc810 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/validate/request/XAVRequest.java @@ -0,0 +1,18 @@ +package io.ljunggren.ups.api.model.validate.request; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class XAVRequest { + + @JsonProperty("AddressKeyFormat") + private AddressKeyFormat addressKeyFormat; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/validate/response/Candidate.java b/src/main/java/io/ljunggren/ups/api/model/validate/response/Candidate.java new file mode 100644 index 0000000..d839d20 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/validate/response/Candidate.java @@ -0,0 +1,19 @@ +package io.ljunggren.ups.api.model.validate.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.ljunggren.ups.api.model.validate.request.AddressKeyFormat; +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class Candidate { + + @JsonProperty("AddressKeyFormat") + private AddressKeyFormat addressKeyFormat; + +} diff --git a/src/main/java/io/ljunggren/ups/api/model/validate/response/XAVResponse.java b/src/main/java/io/ljunggren/ups/api/model/validate/response/XAVResponse.java new file mode 100644 index 0000000..b8230bf --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/model/validate/response/XAVResponse.java @@ -0,0 +1,20 @@ +package io.ljunggren.ups.api.model.validate.response; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import lombok.Data; + +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(Include.NON_NULL) +public class XAVResponse { + + @JsonProperty("Candidate") + private List candidates; + +} diff --git a/src/main/java/io/ljunggren/ups/api/service/AddressService.java b/src/main/java/io/ljunggren/ups/api/service/AddressService.java new file mode 100644 index 0000000..7c0344c --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/service/AddressService.java @@ -0,0 +1,53 @@ +package io.ljunggren.ups.api.service; + +import java.io.UnsupportedEncodingException; + +import org.apache.http.Header; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.message.BasicHeader; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.ljunggren.ups.api.model.Address; +import io.ljunggren.ups.api.model.UpsResponse; +import io.ljunggren.ups.api.model.validate.request.AddressKeyFormat; +import io.ljunggren.ups.api.model.validate.request.XAVRequest; +import lombok.AllArgsConstructor; +import lombok.Data; + +public class AddressService extends BaseService { + + public AddressService(String domain) { + super(domain); + } + + private final String URI = domain + "/api/addressvalidation/v2/1"; + + public UpsResponse validate(Address address, String accessToken) throws Exception { + return validate(address, accessToken, getHttpClient()); + } + + public UpsResponse validate(Address address, String accessToken, CloseableHttpClient httpClient) throws Exception { + HttpPost post = new HttpPost(URI); + Header[] headers = generateHeaders(accessToken); + AddressKeyFormat addressKeyFormat = new AddressKeyFormat(address); + XAVRequest xavRequest = new XAVRequest(); + xavRequest.setAddressKeyFormat(addressKeyFormat); + return post(post, headers, new Request(xavRequest), httpClient); + } + + private Header[] generateHeaders(String accessToken) throws UnsupportedEncodingException { + return new Header[] { + new BasicHeader("Authorization", "Bearer " + accessToken) + }; + } + + @Data + @AllArgsConstructor + private class Request { + @JsonProperty("XAVRequest") + private XAVRequest xavRequest; + } + +} diff --git a/src/main/java/io/ljunggren/ups/api/service/AuthorizationService.java b/src/main/java/io/ljunggren/ups/api/service/AuthorizationService.java deleted file mode 100644 index e3cc24a..0000000 --- a/src/main/java/io/ljunggren/ups/api/service/AuthorizationService.java +++ /dev/null @@ -1,50 +0,0 @@ -package io.ljunggren.ups.api.service; - -import java.io.UnsupportedEncodingException; -import java.util.Arrays; -import java.util.Base64; -import java.util.List; - -import org.apache.http.Header; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.entity.StringEntity; -import org.apache.http.message.BasicHeader; -import org.apache.http.message.BasicNameValuePair; - -import io.ljunggren.json.utils.JsonUtils; -import io.ljunggren.ups.api.UpsProperties; -import io.ljunggren.ups.api.model.AuthToken; -import io.ljunggren.ups.api.tracking.util.HttpUtils; - -public class AuthorizationService { - - private UpsProperties properties; - - public AuthorizationService(UpsProperties upsProperties) { - this.properties = upsProperties; - } - - public AuthToken getAuthToken(String clientId, String clientSecret, String accountNumber) throws Exception { - Header[] headers = generateHeaders(clientId, clientSecret, accountNumber); - StringEntity entity = generateAuthEntity(); - String json = HttpUtils.post(properties.getDomain() + "/security/v1/oauth/token", headers, entity); - return JsonUtils.jsonToObject(json, AuthToken.class); - } - - private Header[] generateHeaders(String clientId, String clientSecret, String accountNumber) throws UnsupportedEncodingException { - String base64String = Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes("UTF-8")); - return new Header[] { - new BasicHeader("x-merchant-id", accountNumber), - new BasicHeader("content-type", "application/x-www-form-urlencoded"), - new BasicHeader("authorization", "Basic " + base64String) - }; - } - - private StringEntity generateAuthEntity() throws UnsupportedEncodingException { - List urlParameters = Arrays.asList( - new BasicNameValuePair("grant_type", "client_credentials") - ); - return new UrlEncodedFormEntity(urlParameters); - } - -} diff --git a/src/main/java/io/ljunggren/ups/api/service/BaseService.java b/src/main/java/io/ljunggren/ups/api/service/BaseService.java new file mode 100644 index 0000000..f6c44bc --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/service/BaseService.java @@ -0,0 +1,98 @@ +package io.ljunggren.ups.api.service; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; + +import org.apache.http.Header; +import org.apache.http.HttpResponse; +import org.apache.http.client.config.CookieSpecs; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; + +import io.ljunggren.json.utils.JsonUtils; +import io.ljunggren.ups.api.model.Error; +import io.ljunggren.ups.api.model.ErrorResponse; +import io.ljunggren.ups.api.model.UpsResponse; + +public class BaseService { + + protected String domain; + + protected BaseService(String domain) { + this.domain = domain; + } + + protected static final List KNOWN_RESPONSE_CODES = Arrays.asList(new Integer[] { + 200, 400, 403, 404, 500, 503 + }); + protected static final int THROTTLING_CODE = 429; + + protected CloseableHttpClient getHttpClient() { + return HttpClients.custom() + .setDefaultRequestConfig(RequestConfig.custom() + .setCookieSpec(CookieSpecs.STANDARD).build()) + .build(); + } + + protected static UpsResponse get(HttpGet get, Header[] headers, CloseableHttpClient httpClient) throws IOException { + get.setHeaders(headers); + return execute(get, httpClient); + } + + protected static UpsResponse post(HttpPost post, Header[] headers, Object request, CloseableHttpClient httpClient) throws IOException { + post.setHeaders(headers); + post.setEntity(new StringEntity(JsonUtils.objectToJson(request))); + return execute(post, httpClient); + } + + protected static UpsResponse execute(HttpRequestBase httpRequest, CloseableHttpClient httpClient) throws IOException { + try { + CloseableHttpResponse response = httpClient.execute(httpRequest); + int responseCode = response.getStatusLine().getStatusCode(); + String json = getResult(response); + if (KNOWN_RESPONSE_CODES.contains(responseCode)) { + return JsonUtils.jsonToObject(json, UpsResponse.class); + } + UpsResponse upsResponse = new UpsResponse(); + if (THROTTLING_CODE == responseCode) { + ErrorResponse errorResponse = new ErrorResponse().addError( + new Error("Throttled", "Too many requests")); + upsResponse.setErrorResponse(errorResponse); + return upsResponse; + } + ErrorResponse errorResponse = new ErrorResponse().addError( + new Error("Error", String.format("Unknown response code %d", responseCode))); + upsResponse.setErrorResponse(errorResponse); + return upsResponse; + } catch(Exception e) { + UpsResponse trackingResponse = new UpsResponse(); + ErrorResponse errorResponse = new ErrorResponse().addError( + new Error("Error", e.getMessage())); + trackingResponse.setErrorResponse(errorResponse); + return trackingResponse; + } finally { + httpClient.close(); + } + } + + protected static String getResult(HttpResponse response) throws UnsupportedOperationException, IOException { + BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)); + StringBuilder result = new StringBuilder(); + String line = new String(); + while ((line = rd.readLine()) != null) { + result.append(line); + } + return result.toString(); + } + +} diff --git a/src/main/java/io/ljunggren/ups/api/service/OauthService.java b/src/main/java/io/ljunggren/ups/api/service/OauthService.java new file mode 100644 index 0000000..c381560 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/service/OauthService.java @@ -0,0 +1,61 @@ +package io.ljunggren.ups.api.service; + +import java.io.UnsupportedEncodingException; +import java.util.Arrays; +import java.util.Base64; +import java.util.List; + +import org.apache.http.Header; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.message.BasicHeader; +import org.apache.http.message.BasicNameValuePair; + +import io.ljunggren.json.utils.JsonUtils; +import io.ljunggren.ups.api.model.AuthToken; + +public class OauthService extends BaseService { + + public OauthService(String domain) { + super(domain); + } + private final String URI = domain + "/security/v1/oauth/token"; + + public AuthToken getAuthToken(String clientId, String clientSecret, String accountNumber) throws Exception { + return getAuthToken(clientId, clientSecret, accountNumber, getHttpClient()); + } + + public AuthToken getAuthToken(String clientId, String clientSecret, String accountNumber, CloseableHttpClient httpClient) throws Exception { + HttpPost post = new HttpPost(URI); + Header[] headers = generateHeaders(clientId, clientSecret, accountNumber); + try { + post.setHeaders(headers); + post.setEntity(generateAuthEntity()); + CloseableHttpResponse response = httpClient.execute(post); + String json = getResult(response); + return JsonUtils.jsonToObject(json, AuthToken.class); + } finally { + httpClient.close(); + } + } + + private Header[] generateHeaders(String clientId, String clientSecret, String accountNumber) throws UnsupportedEncodingException { + String base64String = Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes("UTF-8")); + return new Header[] { + new BasicHeader("x-merchant-id", accountNumber), + new BasicHeader("content-type", "application/x-www-form-urlencoded"), + new BasicHeader("authorization", "Basic " + base64String) + }; + } + + private StringEntity generateAuthEntity() throws UnsupportedEncodingException { + List urlParameters = Arrays.asList( + new BasicNameValuePair("grant_type", "client_credentials") + ); + return new UrlEncodedFormEntity(urlParameters); + } + +} diff --git a/src/main/java/io/ljunggren/ups/api/service/RateService.java b/src/main/java/io/ljunggren/ups/api/service/RateService.java new file mode 100644 index 0000000..0777f1f --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/service/RateService.java @@ -0,0 +1,51 @@ +package io.ljunggren.ups.api.service; + +import java.io.UnsupportedEncodingException; +import java.util.UUID; + +import org.apache.http.Header; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.message.BasicHeader; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import io.ljunggren.ups.api.model.UpsResponse; +import io.ljunggren.ups.api.model.rate.request.RateRequest; +import lombok.AllArgsConstructor; +import lombok.Data; + +public class RateService extends BaseService { + + public RateService(String domain) { + super(domain); + } + + private final String URI = domain + "/api/rating/v2409/Shop"; + + public UpsResponse request(RateRequest rateRequest, String accessToken) throws Exception { + return request(rateRequest, accessToken, getHttpClient()); + } + + public UpsResponse request(RateRequest rateRequest, String accessToken, CloseableHttpClient httpClient) throws Exception { + HttpPost post = new HttpPost(URI); + Header[] headers = generateHeaders(accessToken); + return post(post, headers, new Request(rateRequest), httpClient); + } + + private Header[] generateHeaders(String accessToken) throws UnsupportedEncodingException { + return new Header[] { + new BasicHeader("transId", UUID.randomUUID().toString()), + new BasicHeader("transactionSrc", domain), + new BasicHeader("Authorization", "Bearer " + accessToken) + }; + } + + @Data + @AllArgsConstructor + private class Request { + @JsonProperty("RateRequest") + private RateRequest rateRequest; + } + +} diff --git a/src/main/java/io/ljunggren/ups/api/service/TrackingService.java b/src/main/java/io/ljunggren/ups/api/service/TrackingService.java index 11edf5d..43f721e 100644 --- a/src/main/java/io/ljunggren/ups/api/service/TrackingService.java +++ b/src/main/java/io/ljunggren/ups/api/service/TrackingService.java @@ -1,34 +1,38 @@ package io.ljunggren.ups.api.service; import java.io.UnsupportedEncodingException; +import java.util.UUID; import org.apache.http.Header; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.message.BasicHeader; -import io.ljunggren.json.utils.JsonUtils; -import io.ljunggren.ups.api.UpsProperties; -import io.ljunggren.ups.api.UpsResponse; -import io.ljunggren.ups.api.tracking.util.HttpUtils; +import io.ljunggren.ups.api.model.UpsResponse; -public class TrackingService { +public class TrackingService extends BaseService { - private UpsProperties properties; + public TrackingService(String domain) { + super(domain); + } - public TrackingService(UpsProperties upsProperties) { - this.properties = upsProperties; + private final String URI = domain + "/api/track/v1/details/"; + + public UpsResponse track(String trackingNumber, String accessToken) throws Exception { + return track(trackingNumber, accessToken, getHttpClient()); } - public UpsResponse track(String trackingNumber, String bearerToken) throws Exception { - Header[] headers = generateHeaders(bearerToken); - String json = HttpUtils.get(properties.getDomain() + "/api/track/v1/details/" + trackingNumber, headers); - return JsonUtils.jsonToObject(json, UpsResponse.class); + public UpsResponse track(String trackingNumber, String accessToken, CloseableHttpClient httpClient) throws Exception { + HttpGet get = new HttpGet(URI + trackingNumber); + Header[] headers = generateHeaders(accessToken); + return get(get, headers, httpClient); } - private Header[] generateHeaders(String bearerToken) throws UnsupportedEncodingException { + private Header[] generateHeaders(String accessToken) throws UnsupportedEncodingException { return new Header[] { - new BasicHeader("transId", "testing"), - new BasicHeader("transactionSrc", "tracking.ljunggren.io"), - new BasicHeader("Authorization", "Bearer " + bearerToken) + new BasicHeader("transId", UUID.randomUUID().toString()), + new BasicHeader("transactionSrc", domain), + new BasicHeader("Authorization", "Bearer " + accessToken) }; } diff --git a/src/main/java/io/ljunggren/ups/api/service/util/CodeUtils.java b/src/main/java/io/ljunggren/ups/api/service/util/CodeUtils.java new file mode 100644 index 0000000..66467d2 --- /dev/null +++ b/src/main/java/io/ljunggren/ups/api/service/util/CodeUtils.java @@ -0,0 +1,27 @@ +package io.ljunggren.ups.api.service.util; + +public class CodeUtils { + + public static String shippingServiceCodeToString(String code) { + switch (code) { + case "01": return "UPS Next Day Air"; + case "02": return "UPS 2nd Day Air"; + case "03": return "UPS Ground"; + case "07": return "UPS Worldwide Express"; + case "08": return "UPS Worldwide Expedited"; + case "11": return "UPS Standard"; + case "12": return "UPS 3 Day Select"; + case "13": return "UPS Next Day Air Saver"; + case "14": return "UPS Next Day Air Early A.M."; + case "54": return "UPS Worldwide Express Plus"; + case "59": return "UPS 2nd Day Air A.M."; + case "65": return "UPS Saver"; + case "82": return "UPS Today Standard"; + case "83": return "UPS Today Dedicated Courier"; + case "85": return "UPS Today Express"; + case "86": return "UPS Today Express Saver"; + default: return "Unknown"; + } + } + +} diff --git a/src/main/java/io/ljunggren/ups/api/tracking/util/HttpUtils.java b/src/main/java/io/ljunggren/ups/api/tracking/util/HttpUtils.java deleted file mode 100644 index 6252d94..0000000 --- a/src/main/java/io/ljunggren/ups/api/tracking/util/HttpUtils.java +++ /dev/null @@ -1,67 +0,0 @@ -package io.ljunggren.ups.api.tracking.util; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; - -import org.apache.http.Header; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpRequestBase; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; - -public class HttpUtils { - - public static String get(String url, Header[] headers) throws Exception { - HttpGet httpGet = new HttpGet(url); - if (headers != null && headers.length > 0) { - httpGet.setHeaders(headers); - } - return execute(httpGet); - } - - public static String post(String url, Header[] headers) throws Exception { - return post(url, headers, null); - } - - public static String post(String url, Header[] headers, StringEntity entity) throws Exception { - HttpPost httpPost = new HttpPost(url); - if (headers != null && headers.length > 0) { - httpPost.setHeaders(headers); - } - if (entity != null) { - httpPost.setEntity(entity); - } - return execute(httpPost); - } - - private static String execute(HttpRequestBase httpRequest) throws Exception { - CloseableHttpClient client = HttpClients.createDefault(); - try { - CloseableHttpResponse response = client.execute(httpRequest); - if (response.getStatusLine().getStatusCode() != 200) { - throw new Exception(String.format("Response code %d returned for url: %s", - response.getStatusLine().getStatusCode(), httpRequest.getURI())); - } - return getResult(response); - } finally { - client.close(); - } - } - - private static String getResult(HttpResponse response) throws UnsupportedOperationException, IOException { - BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)); - StringBuilder result = new StringBuilder(); - String line = new String(); - while ((line = rd.readLine()) != null) { - result.append(line); - } - return result.toString(); - } - -} diff --git a/src/main/resources/properties/cie/ups.properties b/src/main/resources/properties/cie/ups.properties deleted file mode 100644 index a258ace..0000000 --- a/src/main/resources/properties/cie/ups.properties +++ /dev/null @@ -1 +0,0 @@ -domain=https://wwwcie.ups.com diff --git a/src/main/resources/properties/production/ups.properties b/src/main/resources/properties/production/ups.properties deleted file mode 100644 index 96cc0ce..0000000 --- a/src/main/resources/properties/production/ups.properties +++ /dev/null @@ -1 +0,0 @@ -domain=https://onlinetools.ups.com diff --git a/src/test/java/io/ljunggren/ups/api/UpsPropertiesTest.java b/src/test/java/io/ljunggren/ups/api/UpsPropertiesTest.java deleted file mode 100644 index 54218be..0000000 --- a/src/test/java/io/ljunggren/ups/api/UpsPropertiesTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package io.ljunggren.ups.api; - -import static org.junit.Assert.assertNotNull; - -import org.junit.Test; - -public class UpsPropertiesTest { - - @Test - public void getTrackingUrlCieTest() { - UpsProperties properties = new UpsProperties(UpsEnvironment.CIE); - String url = properties.getDomain(); - assertNotNull(url); - } - - @Test - public void getTrackingUrlProductionTest() { - UpsProperties properties = new UpsProperties(UpsEnvironment.PRODUCTION); - String url = properties.getDomain(); - assertNotNull(url); - } - -} diff --git a/src/test/java/io/ljunggren/ups/api/UpsResponseTest.java b/src/test/java/io/ljunggren/ups/api/UpsResponseTest.java index 36d0152..2767dca 100644 --- a/src/test/java/io/ljunggren/ups/api/UpsResponseTest.java +++ b/src/test/java/io/ljunggren/ups/api/UpsResponseTest.java @@ -8,6 +8,7 @@ import org.junit.Test; import io.ljunggren.json.utils.JsonUtils; +import io.ljunggren.ups.api.model.UpsResponse; public class UpsResponseTest { diff --git a/src/test/java/io/ljunggren/ups/api/model/rate/request/RateRequestTest.java b/src/test/java/io/ljunggren/ups/api/model/rate/request/RateRequestTest.java new file mode 100644 index 0000000..0da09dd --- /dev/null +++ b/src/test/java/io/ljunggren/ups/api/model/rate/request/RateRequestTest.java @@ -0,0 +1,25 @@ +package io.ljunggren.ups.api.model.rate.request; + +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; + +import io.ljunggren.json.utils.JsonUtils; + +public class RateRequestTest { + + private String readFromResources(String file) throws IOException { + return IOUtils.toString(this.getClass().getResourceAsStream(file), "UTF-8"); + } + + @Test + public void parseTest() throws IOException { + String json = readFromResources("/io/ljunggren/ups/api/model/rate/request/rate-request.json"); + RateRequest response = JsonUtils.jsonToObject(json, RateRequest.class); + System.out.println(response); + } + +} diff --git a/src/test/resources/io/ljunggren/ups/api/model/rate/request/rate-request.json b/src/test/resources/io/ljunggren/ups/api/model/rate/request/rate-request.json new file mode 100644 index 0000000..f4d1ca3 --- /dev/null +++ b/src/test/resources/io/ljunggren/ups/api/model/rate/request/rate-request.json @@ -0,0 +1,60 @@ +{ + "Request": { + "TransactionReference": { + "CustomerContext": "CustomerContext" + } + }, + "Shipment": { + "ShipTo": { + "Name": "James", + "Address": { + "AddressLine": [ + "9904 State Run Dr" + ], + "City": "Alpharetta", + "StateProvinceCode": "GA", + "PostalCode": "30005", + "CountryCode": "US" + } + }, + "ShipFrom": { + "Name": "ShipFromName", + "Address": { + "AddressLine": [ + "12 Main ST", + "Apt 24" + ], + "City": "TIMONIUM", + "StateProvinceCode": "MD", + "PostalCode": "21093", + "CountryCode": "US" + } + }, + "Package": [{ + "SimpleRate": { + "Description": "SimpleRateDescription", + "Code": "XS" + }, + "PackagingType": { + "Code": "02", + "Description": "Packaging" + }, + "Dimensions": { + "UnitOfMeasurement": { + "Code": "IN", + "Description": "Inches" + }, + "Length": "5", + "Width": "5", + "Height": "5" + }, + "PackageWeight": { + "UnitOfMeasurement": { + "Code": "LBS", + "Description": "Pounds" + }, + "Weight": "1" + } + }] + } +} \ No newline at end of file