Skip to content

Commit

Permalink
update the code
Browse files Browse the repository at this point in the history
  • Loading branch information
jchen293 committed Jul 9, 2024
1 parent f48a0aa commit 46df5dd
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 110 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## Next Release

- Adds new `updateUps` function under `CarrierAccountService`
- Starting `2024-08-05`, UPS accounts can no longer be updated through the normal update function and must use the UPS specific `updateUps` function
- 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)

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/easypost/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public abstract static class CarrierAccountTypes {
"FedexSmartpostAccount");
}

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

public abstract static class Http {
public static final String API_BASE = "https://api.easypost.com";
public static final String CHARSET = "UTF-8";
Expand Down
50 changes: 25 additions & 25 deletions src/main/java/com/easypost/service/CarrierAccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,7 @@ public class CarrierAccountService {
public CarrierAccount create(final Map<String, Object> params) throws EasyPostException {
String type = (String) params.get("type");
Map<String, Object> wrappedParams = new HashMap<String, Object>();

if (type == null) {
throw new MissingParameterError(
String.format(Constants.ErrorMessages.MISSING_REQUIRED_PARAMETER, "carrier account type"));
} else if (type.equals("UpsAccount")) {
wrappedParams.put("ups_oauth_registrations", params);
} else {
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 @@ -97,28 +88,19 @@ public List<CarrierAccount> all(final Map<String, Object> params) throws EasyPos
* @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.CARRIER_TYPES_WITH_CUSTOM_WORKFLOW.contains(type)
? "ups_oauth_registrations/"
: "carrier_accounts/") + id;

return Requestor.request(RequestMethod.PUT, endpoint, wrappedParams, CarrierAccount.class,
client);
}

/**
* Update UPS carrier account.
*
* @param id The ID of UPS account
* @param params parameters to update.
* @return updated CarrierAccount object.
* @throws EasyPostException when the request fails.
*/
public CarrierAccount updateUps(String id, final Map<String, Object> params) throws EasyPostException {
String endpoint = "ups_oauth_registrations/" + id;

return Requestor.request(RequestMethod.PUT, endpoint, params, CarrierAccount.class, client);
}

/**
* Delete this carrier account.
Expand Down Expand Up @@ -148,4 +130,22 @@ private static String selectCarrierAccountCreationEndpoint(final String carrierA
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.CARRIER_TYPES_WITH_CUSTOM_WORKFLOW.contains(carrierAccountType)
? "ups_oauth_registrations"
: "carrier_account";
}
}
200 changes: 186 additions & 14 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 46df5dd

Please sign in to comment.