Skip to content

Commit 7ebfc48

Browse files
Justintime50claude
andcommitted
feat: add a generic API request interface
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. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 10d7952 commit 7ebfc48

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

lib/EasyPost/EasyPostClient.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use EasyPost\Exception\General\EasyPostException;
77
use EasyPost\Hook\RequestHook;
88
use EasyPost\Hook\ResponseHook;
9+
use EasyPost\Http\Requestor;
910
use EasyPost\Service\AddressService;
11+
use EasyPost\Util\InternalUtil;
1012
use EasyPost\Service\ApiKeyService;
1113
use EasyPost\Service\BaseService;
1214
use EasyPost\Service\BatchService;
@@ -260,4 +262,23 @@ public function unsubscribeFromResponseHook(callable $function): void
260262
{
261263
$this->responseEvent->removeHandler($function);
262264
}
265+
266+
/**
267+
* Make an API call to the EasyPost API.
268+
*
269+
* This public, generic interface is useful for making arbitrary API calls to the EasyPost API that
270+
* are not yet supported by the client library's services. When possible, the service for your use case
271+
* should be used instead as it provides a more convenient and higher-level interface depending on the endpoint.
272+
*
273+
* @param string $method
274+
* @param string $endpoint
275+
* @param mixed $params
276+
* @return mixed
277+
*/
278+
public function makeApiCall(string $method, string $endpoint, mixed $params = null): mixed
279+
{
280+
$response = Requestor::request($this, $method, $endpoint, $params);
281+
282+
return InternalUtil::convertToEasyPostObject($this, $response);
283+
}
263284
}

test/EasyPost/EasyPostClientTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88

99
class EasyPostClientTest extends TestCase
1010
{
11+
/**
12+
* Setup the testing environment for this file.
13+
*/
14+
public static function setUpBeforeClass(): void
15+
{
16+
TestUtil::setupVcrTests();
17+
}
18+
19+
/**
20+
* Cleanup the testing environment once finished.
21+
*/
22+
public static function tearDownAfterClass(): void
23+
{
24+
TestUtil::teardownVcrTests();
25+
}
26+
1127
/**
1228
* Test setting and getting the API key for different EasyPostClients.
1329
*/
@@ -66,4 +82,19 @@ public function testInvalidServiceProperty(): void
6682
);
6783
}
6884
}
85+
86+
/**
87+
* Test making an API call using the generic makeApiCall method.
88+
*/
89+
public function testMakeApiCall(): void
90+
{
91+
TestUtil::setupCassette('client/makeApiCall.yml');
92+
93+
$client = new EasyPostClient((string)getenv('EASYPOST_TEST_API_KEY'));
94+
95+
$response = $client->makeApiCall('get', '/addresses', ['page_size' => 1]);
96+
97+
$this->assertCount(1, $response['addresses']);
98+
$this->assertEquals('Address', $response['addresses'][0]['object']);
99+
}
69100
}

test/cassettes/client/makeApiCall.yml

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

0 commit comments

Comments
 (0)