Skip to content

Commit 70c5a4f

Browse files
Justintime50claude
andcommitted
feat: add a generic API request interface
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent aa71613 commit 70c5a4f

3 files changed

Lines changed: 161 additions & 0 deletions

File tree

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

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

3+
import java.util.Map;
34
import java.util.function.Function;
45

56
import com.easypost.Constants;
7+
import com.easypost.exception.EasyPostException;
68
import com.easypost.exception.General.MissingParameterError;
79
import com.easypost.hooks.RequestHook;
810
import com.easypost.hooks.RequestHookResponses;
911
import com.easypost.hooks.ResponseHook;
1012
import com.easypost.hooks.ResponseHookResponses;
13+
import com.easypost.http.Requestor;
14+
import com.easypost.http.Requestor.RequestMethod;
1115

1216
import lombok.Getter;
1317

@@ -215,4 +219,23 @@ public String getApiVersion() {
215219
public String getApiBase() {
216220
return apiBase;
217221
}
222+
223+
/**
224+
* Make an API call to the EasyPost API.
225+
*
226+
* This public, generic interface is useful for making arbitrary API calls to the EasyPost API that
227+
* are not yet supported by the client library's services. When possible, the service for your use case
228+
* should be used instead as it provides a more convenient and higher-level interface depending on the endpoint.
229+
*
230+
* @param method The HTTP method to use for the request.
231+
* @param endpoint The endpoint to call (e.g., "/addresses").
232+
* @param params The parameters to send with the request.
233+
* @return A Map containing the API response.
234+
* @throws EasyPostException when the request fails.
235+
*/
236+
@SuppressWarnings("unchecked")
237+
public Map<String, Object> makeApiCall(RequestMethod method, String endpoint, Map<String, Object> params)
238+
throws EasyPostException {
239+
return Requestor.request(method, endpoint, params, Map.class, this);
240+
}
218241
}

src/test/cassettes/client/make_api_call.json

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.easypost;
2+
3+
import com.easypost.exception.EasyPostException;
4+
import com.easypost.http.Requestor.RequestMethod;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
15+
public final class EasyPostClientTest {
16+
private static TestUtils.VCR vcr;
17+
18+
/**
19+
* Set up the testing environment for this file.
20+
*
21+
* @throws EasyPostException when the request fails.
22+
*/
23+
@BeforeAll
24+
public static void setup() throws EasyPostException {
25+
vcr = new TestUtils.VCR("client", TestUtils.ApiKey.TEST);
26+
}
27+
28+
/**
29+
* Test making a generic API call.
30+
*
31+
* @throws EasyPostException when the request fails.
32+
*/
33+
@Test
34+
public void testMakeApiCall() throws EasyPostException {
35+
vcr.setUpTest("make_api_call");
36+
37+
Map<String, Object> params = new HashMap<>();
38+
params.put("page_size", 1);
39+
40+
Map<String, Object> response = vcr.client.makeApiCall(RequestMethod.GET, "addresses", params);
41+
42+
assertNotNull(response);
43+
List<?> addresses = (List<?>) response.get("addresses");
44+
assertNotNull(addresses);
45+
assertEquals(1, addresses.size());
46+
47+
Map<String, Object> firstAddress = (Map<String, Object>) addresses.get(0);
48+
assertEquals("Address", firstAddress.get("object"));
49+
}
50+
}

0 commit comments

Comments
 (0)