Skip to content

Commit

Permalink
Merge pull request #322 from EasyPost/315_persistent_http_client
Browse files Browse the repository at this point in the history
feat: persist the HTTP client inside of EasyPostClient
  • Loading branch information
Justintime50 authored Nov 29, 2023
2 parents 6648c59 + 7fba589 commit e6e4176
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Drops support for PHP 7.4
- Adds support for PHP 8.3
- Persists the HTTP client inside of the `EasyPostClient` via the `httpClient` property to reduce memory consumption on consecutive requests
- Removed `withCarbonOffset` parameter from `create`, `buy`, and `regenerateRates` functions of the Shipment service as EasyPost now offers Carbon Neutral shipments by default for free
- Fixes a bug where the original filtering criteria of `all` calls wasn't passed along to `getNextPage` calls. Now, these are persisted via a `_params` key on response objects locally
- Removes the undocumented `createAndBuy` function from the `Batch` service. The proper usage is to create a batch first and buy it separately
Expand Down
3 changes: 3 additions & 0 deletions lib/EasyPost/EasyPostClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use EasyPost\Service\TrackerService;
use EasyPost\Service\UserService;
use EasyPost\Service\WebhookService;
use GuzzleHttp\Client;

/**
* Client used to send requests to the EasyPost API.
Expand Down Expand Up @@ -71,6 +72,7 @@ class EasyPostClient extends BaseService
private $mockingUtility;
public $requestEvent;
public $responseEvent;
public $httpClient;

/**
* Constructor for an EasyPostClient.
Expand All @@ -93,6 +95,7 @@ public function __construct(
$this->mockingUtility = $mockingUtility;
$this->requestEvent = new RequestHook();
$this->responseEvent = new ResponseHook();
$this->httpClient = new Client();

if (!$this->apiKey) {
throw new MissingParameterException(
Expand Down
4 changes: 1 addition & 3 deletions lib/EasyPost/Http/Requestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use EasyPost\Exception\Api\TimeoutException;
use EasyPost\Exception\Api\UnauthorizedException;
use EasyPost\Exception\Api\UnknownApiException;
use GuzzleHttp\Client;

class Requestor
{
Expand Down Expand Up @@ -205,10 +204,9 @@ private static function requestRaw($client, $method, $url, $params, $beta = fals
$httpStatus = $matchingRequest->responseInfo->statusCode;
$responseHeaders = [];
} else {
$guzzleClient = new Client();
$requestOptions['headers'] = $headers;
try {
$response = $guzzleClient->request($method, $absoluteUrl, $requestOptions);
$response = $client->httpClient->request($method, $absoluteUrl, $requestOptions);
} catch (\GuzzleHttp\Exception\ConnectException $error) {
throw new HttpException(sprintf(Constants::COMMUNICATION_ERROR, 'EasyPost', $error->getMessage()));
}
Expand Down
91 changes: 48 additions & 43 deletions test/cassettes/addresses/getNextPage.yml

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

0 comments on commit e6e4176

Please sign in to comment.