Skip to content
Draft
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
21 changes: 21 additions & 0 deletions lib/EasyPost/EasyPostClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use EasyPost\Exception\General\EasyPostException;
use EasyPost\Hook\RequestHook;
use EasyPost\Hook\ResponseHook;
use EasyPost\Http\Requestor;
use EasyPost\Service\AddressService;
use EasyPost\Util\InternalUtil;
use EasyPost\Service\ApiKeyService;
use EasyPost\Service\BaseService;
use EasyPost\Service\BatchService;
Expand Down Expand Up @@ -260,4 +262,23 @@ public function unsubscribeFromResponseHook(callable $function): void
{
$this->responseEvent->removeHandler($function);
}

/**
* 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 string $method
* @param string $endpoint
* @param mixed $params
* @return mixed
*/
public function makeApiCall(string $method, string $endpoint, mixed $params = null): mixed
{
$response = Requestor::request($this, $method, $endpoint, $params);

return InternalUtil::convertToEasyPostObject($this, $response);
}
}
31 changes: 31 additions & 0 deletions test/EasyPost/EasyPostClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@

class EasyPostClientTest extends TestCase
{
/**
* Setup the testing environment for this file.
*/
public static function setUpBeforeClass(): void
{
TestUtil::setupVcrTests();
}

/**
* Cleanup the testing environment once finished.
*/
public static function tearDownAfterClass(): void
{
TestUtil::teardownVcrTests();
}

/**
* Test setting and getting the API key for different EasyPostClients.
*/
Expand Down Expand Up @@ -66,4 +82,19 @@ public function testInvalidServiceProperty(): void
);
}
}

/**
* Test making an API call using the generic makeApiCall method.
*/
public function testMakeApiCall(): void
{
TestUtil::setupCassette('client/makeApiCall.yml');

$client = new EasyPostClient((string)getenv('EASYPOST_TEST_API_KEY'));

$response = $client->makeApiCall('get', '/addresses', ['page_size' => 1]);

$this->assertCount(1, $response['addresses']);
$this->assertEquals('Address', $response['addresses'][0]['object']);
}
}
84 changes: 84 additions & 0 deletions test/cassettes/client/makeApiCall.yml

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

Loading