Skip to content

Commit 1cbd4f9

Browse files
authored
Merge pull request #240 from EasyPost/php_versions
chore: drop 7.3, add 8.2
2 parents 6e76636 + e1bb89a commit 1cbd4f9

23 files changed

+165
-154
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@ on:
88

99
jobs:
1010
lint:
11-
runs-on: ubuntu-latest
11+
runs-on: ubuntu-20.04
1212
steps:
1313
- uses: actions/checkout@v3
1414
- name: set up php
1515
uses: shivammathur/setup-php@v2
1616
with:
17-
php-version: "8.1"
17+
php-version: "8.2"
1818
- name: install dependencies
1919
run: make install
2020
- name: lint
2121
run: make lint
2222
run-tests:
23-
runs-on: ubuntu-latest
23+
runs-on: ubuntu-20.04
2424
strategy:
2525
matrix:
26-
phpversion: ["7.3", "7.4", "8.0", "8.1"]
26+
# TODO: We are getting segfaults on PHP 8.2 when running the test suite. This needs to be investigated before enabling
27+
phpversion: ["7.4", "8.0", "8.1"]
2728
steps:
2829
- uses: actions/checkout@v3
2930
- name: set up php
3031
uses: shivammathur/setup-php@v2
3132
with:
3233
php-version: ${{ matrix.phpversion }}
34+
coverage: xdebug
3335
- name: get composer cache directory
3436
id: composer-cache
3537
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ clean:
99
## coverage - Runs the test suite and generates a coverage report
1010
coverage:
1111
composer coverage
12-
bin/coverage-check build/logs/clover.xml 86 --only-percentage
12+
bin/coverage-check build/logs/clover.xml 82 --only-percentage
1313

1414
## docs - Generate documentation for the library
1515
docs:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Upgrading major versions of this project? Refer to the [Upgrade Guide](UPGRADE_G
7171

7272
## Development
7373

74-
**NOTE:** Recording VCR cassettes only works with PHP 7.3 or 7.4. Once recorded, tests can be run on PHP 7 or 8.
74+
**NOTE:** Recording VCR cassettes only works with PHP 7.4. Once recorded, tests can be run on PHP 7.4 or 8.0+.
7575

7676
```bash
7777
# Install dependencies

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"require": {
2121
"ext-json": "*",
22-
"php": ">=7.3",
22+
"php": ">=7.4",
2323
"guzzlehttp/guzzle": "^7.5"
2424
},
2525
"require-dev": {

composer.lock

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

lib/EasyPost/EasyPostClient.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use EasyPost\Constant\Constants;
66
use EasyPost\Exception\Error;
77
use EasyPost\Service\AddressService;
8+
use EasyPost\Service\BaseService;
89
use EasyPost\Service\BatchService;
910
use EasyPost\Service\BillingService;
1011
use EasyPost\Service\CarrierAccountService;
@@ -52,8 +53,36 @@
5253
* @property UserService $user;
5354
* @property WebhookService $webhook;
5455
*/
55-
class EasyPostClient extends Service\BaseService
56+
class EasyPostClient extends BaseService
5657
{
58+
// Client properties
59+
private $apiKey;
60+
private $timeout;
61+
private $apiBase;
62+
63+
// Services
64+
public $address;
65+
public $batch;
66+
public $billing;
67+
public $carrierAccount;
68+
public $customsInfo;
69+
public $customsItem;
70+
public $endShipper;
71+
public $event;
72+
public $insurance;
73+
public $order;
74+
public $parcel;
75+
public $pickup;
76+
public $rate;
77+
public $referralCustomer;
78+
public $refund;
79+
public $report;
80+
public $scanForm;
81+
public $shipment;
82+
public $tracker;
83+
public $user;
84+
public $webhook;
85+
5786
/**
5887
* Constructor for an EasyPostClient.
5988
*
@@ -63,14 +92,13 @@ class EasyPostClient extends Service\BaseService
6392
*/
6493
public function __construct($apiKey, $timeout = Constants::TIMEOUT, $apiBase = Constants::API_BASE)
6594
{
66-
// TODO: Make these all read only when we support PHP >= 8.1
67-
6895
// Client properties
6996
$this->apiKey = $apiKey;
7097
$this->timeout = $timeout;
7198
$this->apiBase = $apiBase;
7299

73-
// Service
100+
// Services
101+
// TODO: Make these all read only when we support PHP >= 8.1
74102
$this->address = new AddressService($this);
75103
$this->batch = new BatchService($this);
76104
$this->billing = new BillingService($this);
@@ -97,4 +125,34 @@ public function __construct($apiKey, $timeout = Constants::TIMEOUT, $apiBase = C
97125
throw new Error('No API key provided. See https://www.easypost.com/docs for details, or contact ' . Constants::SUPPORT_EMAIL . ' for assistance.');
98126
}
99127
}
128+
129+
/**
130+
* Get the API key of an EasyPostClient.
131+
*
132+
* @return string
133+
*/
134+
public function getApiKey()
135+
{
136+
return $this->apiKey;
137+
}
138+
139+
/**
140+
* Get the timeout of an EasyPostClient.
141+
*
142+
* @return float
143+
*/
144+
public function getTimeout()
145+
{
146+
return $this->timeout;
147+
}
148+
149+
/**
150+
* Get the API Base URL of an EasyPostClient.
151+
*
152+
* @return string
153+
*/
154+
public function getApiBase()
155+
{
156+
return $this->apiBase;
157+
}
100158
}

lib/EasyPost/Exception/Error.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
*/
1111
class Error extends \Exception
1212
{
13+
private $httpBody;
14+
private $httpStatus;
15+
private $jsonBody;
16+
public $code;
17+
public $ecode;
18+
public $errors;
19+
public $message;
20+
1321
/**
1422
* Constructor.
1523
*

lib/EasyPost/Http/Requestor.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,27 @@
33
namespace EasyPost\Http;
44

55
use EasyPost\Constant\Constants;
6+
use EasyPost\EasyPostClient;
67
use EasyPost\EasypostObject;
78
use EasyPost\Exception\Error;
89
use GuzzleHttp\Client;
910

1011
class Requestor
1112
{
12-
/**
13-
* Constructor.
14-
*
15-
* @param EasyPostClient $client
16-
*/
17-
public function __construct($client)
18-
{
19-
$this->client = $client;
20-
}
21-
2213
/**
2314
* Get the API URL.
2415
*
16+
* @param EasyPostClient $client
2517
* @param string $url
2618
* @param bool $beta
2719
* @return string
2820
*/
29-
private function absoluteUrl($url = '', $beta = false)
21+
private static function absoluteUrl($client, $url = '', $beta = false)
3022
{
3123
if ($beta) {
3224
$apiBase = Constants::API_BASE . '/' . Constants::BETA_API_VERSION;
3325
} else {
34-
$apiBase = $this->client->apiBase . '/' . Constants::API_VERSION;
26+
$apiBase = $client->getApiBase() . '/' . Constants::API_VERSION;
3527
}
3628

3729
return "{$apiBase}{$url}";
@@ -120,37 +112,39 @@ public static function urlEncode($arr, $prefix = null)
120112
/**
121113
* Make a request to the EasyPost API.
122114
*
115+
* @param EasyPostClient $client
123116
* @param string $method
124117
* @param string $url
125118
* @param mixed $params
126119
* @param bool $beta
127120
* @return array
128121
* @throws \EasyPost\Exception\Error
129122
*/
130-
public function request($method, $url, $params = null, $beta = false)
123+
public static function request($client, $method, $url, $params = null, $beta = false)
131124
{
132-
list($responseBody, $httpStatus) = $this->requestRaw($method, $url, $params, $beta);
133-
$httpBody = $this->interpretResponse($responseBody, $httpStatus);
125+
list($responseBody, $httpStatus) = self::requestRaw($client, $method, $url, $params, $beta);
126+
$httpBody = self::interpretResponse($responseBody, $httpStatus);
134127

135128
return $httpBody;
136129
}
137130

138131
/**
139132
* Internal logic required to make a request to the EasyPost API.
140133
*
134+
* @param EasyPostClient $client
141135
* @param string $method
142136
* @param string $url
143137
* @param mixed $params
144138
* @param bool $beta
145139
* @return array
146140
* @throws \EasyPost\Exception\Error
147141
*/
148-
private function requestRaw($method, $url, $params, $beta = false)
142+
private static function requestRaw($client, $method, $url, $params, $beta = false)
149143
{
150-
$absoluteUrl = $this->absoluteUrl($url, $beta);
144+
$absoluteUrl = self::absoluteUrl($client, $url, $beta);
151145
$requestOptions = [
152146
'http_errors' => false, // we set this false here so we can do our own error handling
153-
'timeout' => $this->client->timeout,
147+
'timeout' => $client->getTimeout(),
154148
];
155149
$params = self::encodeObjects($params);
156150
if (in_array(strtolower($method), ['get', 'delete'])) {
@@ -166,7 +160,7 @@ private function requestRaw($method, $url, $params, $beta = false)
166160

167161
$headers = [
168162
'Accept' => 'application/json',
169-
'Authorization' => "Bearer {$this->client->apiKey}",
163+
'Authorization' => "Bearer {$client->getApiKey()}",
170164
'Content-Type' => 'application/json',
171165
'User-Agent' => 'EasyPost/v2 PhpClient/' . Constants::LIBRARY_VERSION . " PHP/$phpVersion OS/$osType OSVersion/$osVersion OSArch/$osArch",
172166
];
@@ -176,7 +170,7 @@ private function requestRaw($method, $url, $params, $beta = false)
176170
try {
177171
$response = $guzzleClient->request($method, $absoluteUrl, $requestOptions);
178172
} catch (\GuzzleHttp\Exception\ConnectException $error) {
179-
$message = "Unexpected error communicating with EasyPost. If this problem persists please let us know at {$this->supportEmail}. {$error->getMessage()}";
173+
$message = 'Unexpected error communicating with EasyPost. If this problem persists please let us know at ' . Constants::SUPPORT_EMAIL . ".{$error->getMessage()}";
180174
throw new Error($message, null, null);
181175
}
182176

@@ -199,7 +193,7 @@ private function requestRaw($method, $url, $params, $beta = false)
199193
* @return mixed
200194
* @throws \EasyPost\Exception\Error
201195
*/
202-
public function interpretResponse($httpBody, $httpStatus)
196+
public static function interpretResponse($httpBody, $httpStatus)
203197
{
204198
try {
205199
$response = json_decode($httpBody, true);
@@ -208,7 +202,7 @@ public function interpretResponse($httpBody, $httpStatus)
208202
}
209203

210204
if ($httpStatus < 200 || $httpStatus >= 300) {
211-
$this->handleApiError($httpBody, $httpStatus, $response);
205+
self::handleApiError($httpBody, $httpStatus, $response);
212206
}
213207

214208
return $response;
@@ -222,7 +216,7 @@ public function interpretResponse($httpBody, $httpStatus)
222216
* @param array $response
223217
* @throws \EasyPost\Exception\Error
224218
*/
225-
public function handleApiError($httpBody, $httpStatus, $response)
219+
public static function handleApiError($httpBody, $httpStatus, $response)
226220
{
227221
if (!is_array($response) || !isset($response['error'])) {
228222
throw new Error("Invalid response object from API: HTTP Status: ({$httpStatus}) {$httpBody})", $httpStatus, $httpBody);

0 commit comments

Comments
 (0)