Skip to content

Commit d648442

Browse files
committed
feat: route Amazon create request
1 parent 2b52734 commit d648442

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- `referralCustomer.addCreditCardFromStripe`
1010
- `referralCustomer.addBankAccountFromStripe`
1111
- Adds `tracking_codes` param to tracker index endpoint
12+
- Routes `AmazonShippingAccount` to the correct endpoint
1213
- Fixes error parsing
1314
- Allows for alternative format of `errors` field (previously we deserialized the `errors` field into a list of `Error` objects; however, sometimes the errors are simply a list of strings. This change make the `errors` field a list of `Object` allowing for either the new `FieldError` object or a list of strings. Users will need to check for the type of error returned and handle appropriately)
1415
- Removed the unused `Error` model

src/main/java/com/easypost/Constants.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@ public abstract static class ErrorCodes {
6161
}
6262

6363
public abstract static class CarrierAccountTypes {
64-
public static final List<String> CARRIER_TYPES_WITH_CUSTOM_WORKFLOW = ImmutableList.of("FedexAccount",
65-
"FedexSmartpostAccount");
66-
}
64+
public static final List<String> CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOW = ImmutableList.of(
65+
"FedexAccount", "FedexSmartpostAccount"
66+
);
67+
68+
public static final List<String> UPS_OAUTH_CARRIER_ACCOUNT_TYPES = ImmutableList.of(
69+
"UpsAccount", "UpsMailInnovationsAccount", "UpsSurepostAccount"
70+
);
6771

68-
public abstract static class UpsAccountTypes {
69-
public static final List<String> UPS_OAUTH_CARRIER_ACCOUNT_TYPES = ImmutableList.of("UpsAccount",
70-
"UpsMailInnovationsAccount", "UpsSurepostAccount");
72+
public static final List<String> CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH = ImmutableList.of(
73+
"AmazonShippingAccount"
74+
);
7175
}
7276

7377
public abstract static class Http {

src/main/java/com/easypost/service/CarrierAccountService.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public CarrierAccount update(String id, final Map<String, Object> params) throws
9393
Map<String, Object> wrappedParams = new HashMap<String, Object>();
9494
wrappedParams.put(selectTopLayerKey(type), params);
9595

96-
String endpoint = (Constants.UpsAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(type)
96+
String endpoint = (Constants.CarrierAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(type)
9797
? "ups_oauth_registrations/"
9898
: "carrier_accounts/") + id;
9999

@@ -121,10 +121,12 @@ public void delete(String id) throws EasyPostException {
121121
* @return The endpoint for the carrier account creation request.
122122
*/
123123
private static String selectCarrierAccountCreationEndpoint(final String carrierAccountType) {
124-
if (Constants.CarrierAccountTypes.CARRIER_TYPES_WITH_CUSTOM_WORKFLOW.contains(carrierAccountType)) {
124+
if (Constants.CarrierAccountTypes.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOW.contains(carrierAccountType)) {
125125
return "carrier_accounts/register";
126-
} else if (Constants.UpsAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(carrierAccountType)) {
126+
} else if (Constants.CarrierAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(carrierAccountType)) {
127127
return "ups_oauth_registrations";
128+
} else if (Constants.CarrierAccountTypes.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.contains(carrierAccountType)) {
129+
return "carrier_accounts/register_oauth";
128130
} else {
129131
return "carrier_accounts";
130132
}
@@ -143,8 +145,12 @@ private static String selectTopLayerKey(final String carrierAccountType) throws
143145
String.format(Constants.ErrorMessages.MISSING_REQUIRED_PARAMETER, "carrier account type"));
144146
}
145147

146-
return Constants.UpsAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(carrierAccountType)
147-
? "ups_oauth_registrations"
148-
: "carrier_account";
148+
if (Constants.CarrierAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(carrierAccountType)) {
149+
return "ups_oauth_registrations";
150+
} else if (Constants.CarrierAccountTypes.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.contains(carrierAccountType)) {
151+
return "carrier_account_oauth_registrations";
152+
} else {
153+
return "carrier_account";
154+
}
149155
}
150156
}

src/test/java/com/easypost/CarrierAccountTest.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,18 @@ private static CarrierAccount createUpsCarrierAccount() throws EasyPostException
4343
data.put("type", "UpsAccount");
4444
data.put("account_number", "123456789");
4545

46-
CarrierAccount upsAccount = vcr.client.carrierAccount.create(data);
47-
testCarrierAccountId = upsAccount.getId(); // trigger deletion after test
48-
return upsAccount;
46+
CarrierAccount carrierAccount = vcr.client.carrierAccount.create(data);
47+
testCarrierAccountId = carrierAccount.getId(); // trigger deletion after test
48+
return carrierAccount;
49+
}
50+
51+
private static CarrierAccount createAmazonCarrierAccount() throws EasyPostException {
52+
Map<String, Object> data = new HashMap<>();
53+
data.put("type", "AmazonShippingAccount");
54+
55+
CarrierAccount carrierAccount = vcr.client.carrierAccount.create(data);
56+
testCarrierAccountId = carrierAccount.getId(); // trigger deletion after test
57+
return carrierAccount;
4958
}
5059

5160
/**
@@ -126,11 +135,27 @@ public void testCreateWithCustomWorkflow() throws EasyPostException {
126135
public void testCreateWithUPS() throws EasyPostException {
127136
vcr.setUpTest("create_with_ups");
128137

129-
CarrierAccount upsAccount = createUpsCarrierAccount();
138+
CarrierAccount carrierAccount = createUpsCarrierAccount();
130139

131-
assertInstanceOf(CarrierAccount.class, upsAccount);
132-
assertTrue(upsAccount.getId().startsWith("ca_"));
133-
assertEquals("UpsAccount", upsAccount.getType());
140+
assertInstanceOf(CarrierAccount.class, carrierAccount);
141+
assertTrue(carrierAccount.getId().startsWith("ca_"));
142+
assertEquals("UpsAccount", carrierAccount.getType());
143+
}
144+
145+
/**
146+
* Test creating an Amazon carrier account.
147+
*
148+
* @throws EasyPostException when the request fails.
149+
*/
150+
@Test
151+
public void testCreateWithAmazon() throws EasyPostException {
152+
// vcr.setUpTest("create_with_amazon");
153+
154+
CarrierAccount carrierAccount = createAmazonCarrierAccount();
155+
156+
assertInstanceOf(CarrierAccount.class, carrierAccount);
157+
assertTrue(carrierAccount.getId().startsWith("ca_"));
158+
assertEquals("AmazonShippingAccount", carrierAccount.getType());
134159
}
135160

136161
/**
@@ -197,15 +222,15 @@ public void testUpdate() throws EasyPostException {
197222
public void testUpdateUpsAccount() throws EasyPostException {
198223
vcr.setUpTest("update_ups");
199224

200-
CarrierAccount upsAccount = createUpsCarrierAccount();
225+
CarrierAccount carrierAccount = createUpsCarrierAccount();
201226
Map<String, Object> updateParams = new HashMap<>();
202227
updateParams.put("account_number", "987654321");
203228

204-
CarrierAccount updatedUpsAccount = vcr.client.carrierAccount.update(upsAccount.getId(), updateParams);
229+
CarrierAccount updatedCarrierAccount = vcr.client.carrierAccount.update(carrierAccount.getId(), updateParams);
205230

206-
assertInstanceOf(CarrierAccount.class, updatedUpsAccount);
207-
assertTrue(updatedUpsAccount.getId().startsWith("ca_"));
208-
assertEquals("UpsAccount", updatedUpsAccount.getType());
231+
assertInstanceOf(CarrierAccount.class, updatedCarrierAccount);
232+
assertTrue(updatedCarrierAccount.getId().startsWith("ca_"));
233+
assertEquals("UpsAccount", updatedCarrierAccount.getType());
209234
}
210235

211236
/**

0 commit comments

Comments
 (0)