Skip to content

Commit

Permalink
Update CarrierService create and update methods (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchen293 authored Jul 10, 2024
1 parent 8ab823a commit b3edb2f
Show file tree
Hide file tree
Showing 8 changed files with 720 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## Next Release

- Routes `UpsAccount`, `UpsMailInnovationsAccount`, and `UpsSurepostAccount` create/update requests to the new `/ups_oauth_registrations` endpoint
- Starting `2024-08-05`, UPS accounts will require a new payload to register or update. See [UPS OAuth 2.0 Update](https://support.easypost.com/hc/en-us/articles/26635027512717-UPS-OAuth-2-0-Update?utm_medium=email&_hsenc=p2ANqtz-96MmFtWICOzy9sKRbbcZSiMovZSrY3MSX1_bgY9N3f9yLVfWQdLhjAGq-SmNcOnDIS6GYhZ0OApjDBrGkKyLLMx1z6_TFOVp6-wllhEFQINrkuRuc&_hsmi=313130292&utm_content=313130292&utm_source=hs_email) for more details

## v7.2.1 (2024-04-12)

- Fix `Fields` serialization bug causing carrier account operations to fail
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ install-styleguide: | update-examples-submodule
curl -LJs https://github.com/checkstyle/checkstyle/releases/download/checkstyle-10.3.1/checkstyle-10.3.1-all.jar -o checkstyle.jar
sh examples/symlink_directory_files.sh examples/style_guides/java .

## init-examples-submodule - Initialize the examples submodule
init-examples-submodule:
git submodule init
git submodule update

## install - Install requirements
install: | update-examples-submodule
install: | init-examples-submodule

## lint - Lints the project
lint: checkstyle scan
Expand Down
2 changes: 1 addition & 1 deletion examples
Submodule examples updated 194 files
7 changes: 6 additions & 1 deletion src/main/java/com/easypost/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ public abstract static class ErrorCodes {

public abstract static class CarrierAccountTypes {
public static final List<String> CARRIER_TYPES_WITH_CUSTOM_WORKFLOW = ImmutableList.of("FedexAccount",
"UpsAccount", "FedexSmartpostAccount");
"FedexSmartpostAccount");
}

public abstract static class UpsAccountTypes {
public static final List<String> UPS_OAUTH_CARRIER_ACCOUNT_TYPES = ImmutableList.of("UpsAccount",
"UpsMailInnovationsAccount", "UpsSurepostAccount");
}

public abstract static class Http {
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/com/easypost/service/CarrierAccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,8 @@ public class CarrierAccountService {
*/
public CarrierAccount create(final Map<String, Object> params) throws EasyPostException {
String type = (String) params.get("type");
if (type == null) {
throw new MissingParameterError(
String.format(Constants.ErrorMessages.MISSING_REQUIRED_PARAMETER, "carrier account type"));
}

Map<String, Object> wrappedParams = new HashMap<String, Object>();
wrappedParams.put("carrier_account", params);

wrappedParams.put(selectTopLayerKey(type), params);
String endpoint = selectCarrierAccountCreationEndpoint(type);

return Requestor.request(RequestMethod.POST, endpoint, wrappedParams, CarrierAccount.class, client);
Expand Down Expand Up @@ -88,16 +82,20 @@ public List<CarrierAccount> all(final Map<String, Object> params) throws EasyPos
/**
* Update this carrier account.
*
* @param params parameters to update.
* @param id The ID of carrier account
* @param params parameters to update.
* @return updated CarrierAccount object.
* @throws EasyPostException when the request fails.
*/
public CarrierAccount update(String id, final Map<String, Object> params) throws EasyPostException {
CarrierAccount carrierAccount = this.retrieve(id);
String type = (String) carrierAccount.getType();
Map<String, Object> wrappedParams = new HashMap<String, Object>();
wrappedParams.put("carrier_account", params);
wrappedParams.put(selectTopLayerKey(type), params);

String endpoint = "carrier_accounts/" + id;
String endpoint = (Constants.UpsAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(type)
? "ups_oauth_registrations/"
: "carrier_accounts/") + id;

return Requestor.request(RequestMethod.PUT, endpoint, wrappedParams, CarrierAccount.class,
client);
Expand Down Expand Up @@ -125,8 +123,28 @@ public void delete(String id) throws EasyPostException {
private static String selectCarrierAccountCreationEndpoint(final String carrierAccountType) {
if (Constants.CarrierAccountTypes.CARRIER_TYPES_WITH_CUSTOM_WORKFLOW.contains(carrierAccountType)) {
return "carrier_accounts/register";
} else if (Constants.UpsAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(carrierAccountType)) {
return "ups_oauth_registrations";
} else {
return "carrier_accounts";
}
}

/**
* Select the top-layer key for the carrier account creation/update request based on the
* carrier type.
*
* @param carrierAccountType The type of carrier account to create.
* @return The top-layer key for the carrier account creation/update request.
*/
private static String selectTopLayerKey(final String carrierAccountType) throws EasyPostException {
if (carrierAccountType == null) {
throw new MissingParameterError(
String.format(Constants.ErrorMessages.MISSING_REQUIRED_PARAMETER, "carrier account type"));
}

return Constants.UpsAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(carrierAccountType)
? "ups_oauth_registrations"
: "carrier_account";
}
}
263 changes: 263 additions & 0 deletions src/test/cassettes/carrier_account/create_with_ups.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b3edb2f

Please sign in to comment.