Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/java/com/easypost/service/EasyPostClient.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.easypost.service;

import java.util.Map;
import java.util.function.Function;

import com.easypost.Constants;
import com.easypost.exception.EasyPostException;
import com.easypost.exception.General.MissingParameterError;
import com.easypost.hooks.RequestHook;
import com.easypost.hooks.RequestHookResponses;
import com.easypost.hooks.ResponseHook;
import com.easypost.hooks.ResponseHookResponses;
import com.easypost.http.Requestor;
import com.easypost.http.Requestor.RequestMethod;

import lombok.Getter;

Expand Down Expand Up @@ -215,4 +219,23 @@ public String getApiVersion() {
public String getApiBase() {
return apiBase;
}

/**
* Make an API call to the EasyPost API.
*
* This public, generic interface is useful for making arbitrary API calls to the EasyPost API that
* are not yet supported by the client library's services. When possible, the service for your use case
* should be used instead as it provides a more convenient and higher-level interface depending on the endpoint.
*
* @param method The HTTP method to use for the request.
* @param endpoint The endpoint to call (e.g., "/addresses").
* @param params The parameters to send with the request.
* @return A Map containing the API response.
* @throws EasyPostException when the request fails.
*/
@SuppressWarnings("unchecked")
public Map<String, Object> makeApiCall(RequestMethod method, String endpoint, Map<String, Object> params)
throws EasyPostException {
return Requestor.request(method, endpoint, params, Map.class, this);
}
}
88 changes: 88 additions & 0 deletions src/test/cassettes/client/make_api_call.json

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

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

import static org.junit.jupiter.api.Assertions.assertEquals;

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

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.easypost.exception.EasyPostException;
import com.easypost.http.Requestor.RequestMethod;

public final class EasyPostClientTest {
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("client", TestUtils.ApiKey.TEST);
}

/**
* Test making a generic API call.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testMakeApiCall() throws EasyPostException {
vcr.setUpTest("make_api_call");

Map<String, Object> params = new HashMap<>();
params.put("page_size", 1);

Map<String, Object> response = vcr.client.makeApiCall(RequestMethod.GET, "addresses", params);

List<?> addresses = (List<?>) response.get("addresses");
assertEquals(1, addresses.size());
Map<String, Object> firstAddress = (Map<String, Object>) addresses.get(0);
assertEquals("Address", firstAddress.get("object"));
}
}
Loading