Skip to content

Commit ddc9882

Browse files
authored
- Move API key functions to API Key service (#285)
1 parent 8e01cf5 commit ddc9882

File tree

8 files changed

+312
-42
lines changed

8 files changed

+312
-42
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.easypost.service;
22

3+
import com.easypost.Constants;
34
import com.easypost.exception.EasyPostException;
5+
import com.easypost.exception.General.FilteringError;
46
import com.easypost.http.Requestor;
57
import com.easypost.http.Requestor.RequestMethod;
8+
import com.easypost.model.ApiKey;
69
import com.easypost.model.ApiKeys;
710

11+
import java.util.List;
12+
import java.util.Objects;
13+
814
public class ApiKeyService {
915
private final EasyPostClient client;
1016

@@ -28,4 +34,27 @@ public ApiKeys all() throws EasyPostException {
2834

2935
return Requestor.request(RequestMethod.GET, endpoint, null, ApiKeys.class, client);
3036
}
37+
38+
/**
39+
* Get this User's API keys.
40+
*
41+
* @param id The ID of the user.
42+
* @return List of ApiKey objects.
43+
* @throws EasyPostException when the request fails.
44+
*/
45+
public List<ApiKey> retrieveApiKeysForUser(final String id) throws EasyPostException {
46+
ApiKeys parentKeys = all();
47+
48+
if (Objects.equals(id, parentKeys.getId())) {
49+
return parentKeys.getKeys();
50+
}
51+
52+
for (int i = 0; i < parentKeys.getChildren().size(); i++) {
53+
if (id.equals(parentKeys.getChildren().get(i).getId())) {
54+
return parentKeys.getChildren().get(i).getKeys();
55+
}
56+
}
57+
58+
throw new FilteringError(String.format(Constants.ErrorMessages.NO_OBJECT_FOUND, "API keys"));
59+
}
3160
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public void delete(final String id) throws EasyPostException {
100100
/**
101101
* Get this User's API keys.
102102
*
103+
* @deprecated Use {@link ApiKeyService#retrieveApiKeysForUser(String)} instead.
103104
* @param id The ID of the user.
104105
* @return List of ApiKey objects.
105106
* @throws EasyPostException when the request fails.

src/test/cassettes/billing/delete_payment_method.json

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/test/cassettes/billing/fund_wallet.json

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.easypost;
2+
3+
import com.easypost.exception.EasyPostException;
4+
import com.easypost.exception.General.FilteringError;
5+
import com.easypost.model.ApiKey;
6+
import com.easypost.model.ApiKeys;
7+
import com.easypost.model.User;
8+
import org.junit.jupiter.api.AfterEach;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
19+
20+
public final class ApiKeyTest {
21+
private static String testUserId = null;
22+
private static TestUtils.VCR vcr;
23+
24+
/**
25+
* Set up the testing environment for this file.
26+
*
27+
* @throws EasyPostException when the request fails.
28+
*/
29+
@BeforeAll
30+
public static void setUp() throws EasyPostException {
31+
vcr = new TestUtils.VCR("api_key", TestUtils.ApiKey.PRODUCTION);
32+
}
33+
34+
/**
35+
* Clean up test attributes after each unit test.
36+
*/
37+
@AfterEach
38+
public void cleanup() {
39+
if (testUserId != null) {
40+
try {
41+
User user = vcr.client.user.retrieve(testUserId);
42+
vcr.client.user.delete(user.getId());
43+
testUserId = null;
44+
} catch (Exception e) {
45+
// in case we try to delete something that's already been deleted
46+
}
47+
}
48+
}
49+
50+
/**
51+
* Create a user.
52+
*
53+
* @return User object
54+
*/
55+
private static User createUser() throws EasyPostException {
56+
Map<String, Object> params = new HashMap<>();
57+
params.put("name", "Test User");
58+
User user = vcr.client.user.create(params);
59+
testUserId = user.getId(); // trigger deletion after test
60+
return user;
61+
}
62+
63+
/**
64+
* Test retrieving all API keys.
65+
*
66+
* @throws EasyPostException when the request fails.
67+
*/
68+
@Test
69+
public void testAllApiKeys() throws EasyPostException {
70+
vcr.setUpTest("all_api_keys");
71+
72+
ApiKeys apikeys = vcr.client.apiKey.all();
73+
74+
assertInstanceOf(ApiKeys.class, apikeys);
75+
76+
List<ApiKey> apiKeys = vcr.client.apiKey.retrieveApiKeysForUser(apikeys.getId());
77+
78+
assertNotNull(apiKeys);
79+
}
80+
81+
/**
82+
* Test retrieving all API keys for a user.
83+
*
84+
* @throws EasyPostException when the request fails.
85+
*/
86+
@Test
87+
public void testApiKeys() throws EasyPostException {
88+
vcr.setUpTest("api_keys");
89+
90+
User user = createUser();
91+
92+
List<ApiKey> apiKeys = vcr.client.apiKey.retrieveApiKeysForUser(user.getId());
93+
94+
assertNotNull(apiKeys);
95+
96+
assertThrows(FilteringError.class, () -> vcr.client.apiKey.retrieveApiKeysForUser("invalid_id"));
97+
}
98+
}

0 commit comments

Comments
 (0)