Skip to content

Commit

Permalink
- Move API key functions to API Key service
Browse files Browse the repository at this point in the history
  • Loading branch information
nwithan8 committed Sep 29, 2023
1 parent 8e01cf5 commit dec3cbd
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 42 deletions.
29 changes: 29 additions & 0 deletions src/main/java/com/easypost/service/ApiKeyService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.easypost.service;

import com.easypost.Constants;
import com.easypost.exception.EasyPostException;
import com.easypost.exception.General.FilteringError;
import com.easypost.http.Requestor;
import com.easypost.http.Requestor.RequestMethod;
import com.easypost.model.ApiKey;
import com.easypost.model.ApiKeys;

import java.util.List;
import java.util.Objects;

public class ApiKeyService {
private final EasyPostClient client;

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

return Requestor.request(RequestMethod.GET, endpoint, null, ApiKeys.class, client);
}

/**
* Get this User's API keys.
*
* @param id The ID of the user.
* @return List of ApiKey objects.
* @throws EasyPostException when the request fails.
*/
public List<ApiKey> retrieveApiKeysForUser(final String id) throws EasyPostException {
ApiKeys parentKeys = all();

if (Objects.equals(id, parentKeys.getId())) {
return parentKeys.getKeys();
}

for (int i = 0; i < parentKeys.getChildren().size(); i++) {
if (id.equals(parentKeys.getChildren().get(i).getId())) {
return parentKeys.getChildren().get(i).getKeys();
}
}

throw new FilteringError(String.format(Constants.ErrorMessages.NO_OBJECT_FOUND, "API keys"));
}
}
1 change: 1 addition & 0 deletions src/main/java/com/easypost/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public void delete(final String id) throws EasyPostException {
/**
* Get this User's API keys.
*
* @deprecated Use {@link ApiKeyService#retrieveApiKeysForUser(String)} instead.
* @param id The ID of the user.
* @return List of ApiKey objects.
* @throws EasyPostException when the request fails.
Expand Down
File renamed without changes.
File renamed without changes.
92 changes: 92 additions & 0 deletions src/test/cassettes/billing/delete_payment_method.json

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

92 changes: 92 additions & 0 deletions src/test/cassettes/billing/fund_wallet.json

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

98 changes: 98 additions & 0 deletions src/test/java/com/easypost/ApiKeyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.easypost;

import com.easypost.exception.EasyPostException;
import com.easypost.exception.General.FilteringError;
import com.easypost.model.ApiKey;
import com.easypost.model.ApiKeys;
import com.easypost.model.User;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

public final class ApiKeyTest {
private static String testUserId = null;
private static TestUtils.VCR vcr;

/**
* Set up the testing environment for this file.
*
* @throws EasyPostException when the request fails.
*/
@BeforeAll
public static void setUp() throws EasyPostException {
vcr = new TestUtils.VCR("api_key", TestUtils.ApiKey.PRODUCTION);
}

/**
* Clean up test attributes after each unit test.
*/
@AfterEach
public void cleanup() {
if (testUserId != null) {
try {
User user = vcr.client.user.retrieve(testUserId);
vcr.client.user.delete(user.getId());
testUserId = null;
} catch (Exception e) {
// in case we try to delete something that's already been deleted
}
}
}

/**
* Create a user.
*
* @return User object
*/
private static User createUser() throws EasyPostException {
Map<String, Object> params = new HashMap<>();
params.put("name", "Test User");
User user = vcr.client.user.create(params);
testUserId = user.getId(); // trigger deletion after test
return user;
}

/**
* Test retrieving all API keys.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testAllApiKeys() throws EasyPostException {
vcr.setUpTest("all_api_keys");

ApiKeys apikeys = vcr.client.apiKey.all();

assertInstanceOf(ApiKeys.class, apikeys);

List<ApiKey> apiKeys = vcr.client.apiKey.retrieveApiKeysForUser(apikeys.getId());

assertNotNull(apiKeys);
}

/**
* Test retrieving all API keys for a user.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testApiKeys() throws EasyPostException {
vcr.setUpTest("api_keys");

User user = createUser();

List<ApiKey> apiKeys = vcr.client.apiKey.retrieveApiKeysForUser(user.getId());

assertNotNull(apiKeys);

assertThrows(FilteringError.class, () -> vcr.client.apiKey.retrieveApiKeysForUser("invalid_id"));
}
}
Loading

0 comments on commit dec3cbd

Please sign in to comment.