From 774dc86ab4ba3da3a5fd3c9cc9050cfbd4bfa6ad Mon Sep 17 00:00:00 2001 From: saasfreelancer Date: Tue, 28 Sep 2021 18:04:52 +0500 Subject: [PATCH 1/5] feat: allow for http adapter param - Base client class now accepts Http Adapter object as param. - Add http adapter interface --- lib/recurly/base_client.php | 7 +++++-- lib/recurly/http_adapter_interface.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 lib/recurly/http_adapter_interface.php diff --git a/lib/recurly/base_client.php b/lib/recurly/base_client.php index 691a2d7d..0c4c06ed 100644 --- a/lib/recurly/base_client.php +++ b/lib/recurly/base_client.php @@ -23,10 +23,13 @@ abstract class BaseClient * * @param string $api_key The API key to use when making requests */ - public function __construct(string $api_key, LoggerInterface $logger = null) + public function __construct(string $api_key, LoggerInterface $logger = null, HttpAdapterInterface $http_adapter = null) { $this->_api_key = $api_key; - $this->http = new HttpAdapter; + if (is_null($http_adapter)) { + $http_adapter = new HttpAdapter; + } + $this->http = $http_adapter; if (is_null($logger)) { $logger = new \Recurly\Logger('Recurly', LogLevel::WARNING); } diff --git a/lib/recurly/http_adapter_interface.php b/lib/recurly/http_adapter_interface.php new file mode 100644 index 00000000..843e7a5b --- /dev/null +++ b/lib/recurly/http_adapter_interface.php @@ -0,0 +1,11 @@ + Date: Fri, 4 Mar 2022 16:46:08 -0500 Subject: [PATCH 2/5] Revert "Merge remote-tracking branch 'github-saas/syed/v3-v2021-02-25/guzzle' into v3-v2021-02-25:" This reverts commit e20809e27769d6d28e80d5893df5027f9bde9d02, reversing changes made to d62bdf252e01fb041ed4d971293a60687cdd03b9. --- lib/recurly/base_client.php | 8 ++------ lib/recurly/http_adapter.php | 2 +- lib/recurly/http_adapter_interface.php | 11 ----------- 3 files changed, 3 insertions(+), 18 deletions(-) delete mode 100644 lib/recurly/http_adapter_interface.php diff --git a/lib/recurly/base_client.php b/lib/recurly/base_client.php index 413e9467..39fa7f35 100644 --- a/lib/recurly/base_client.php +++ b/lib/recurly/base_client.php @@ -31,7 +31,7 @@ abstract class BaseClient * In addition to the options managed by BaseClient, it accepts the following options: * - "region" to define the Data Center connection - defaults to "us"; */ - public function __construct(string $api_key, LoggerInterface $logger = null, array $options = [], HttpAdapterInterface $http_adapter = null) + public function __construct(string $api_key, LoggerInterface $logger = null, array $options = []) { $this->_api_key = $api_key; if (isset($options['region'])) { @@ -41,11 +41,7 @@ public function __construct(string $api_key, LoggerInterface $logger = null, arr $this->baseUrl = BaseClient::API_HOSTS[$options['region']]; } - if (is_null($http_adapter)) { - $http_adapter = new HttpAdapter; - } - $this->http = $http_adapter; - + $this->http = new HttpAdapter; if (is_null($logger)) { $logger = new \Recurly\Logger('Recurly', LogLevel::WARNING); } diff --git a/lib/recurly/http_adapter.php b/lib/recurly/http_adapter.php index 429488ac..f4bca544 100644 --- a/lib/recurly/http_adapter.php +++ b/lib/recurly/http_adapter.php @@ -11,7 +11,7 @@ /** * @codeCoverageIgnore */ -class HttpAdapter implements HttpAdapterInterface +class HttpAdapter { private static $_default_options = [ 'ignore_errors' => true diff --git a/lib/recurly/http_adapter_interface.php b/lib/recurly/http_adapter_interface.php deleted file mode 100644 index 843e7a5b..00000000 --- a/lib/recurly/http_adapter_interface.php +++ /dev/null @@ -1,11 +0,0 @@ - Date: Mon, 14 Mar 2022 18:06:24 -0400 Subject: [PATCH 3/5] Fallback to guessing error type from HTTP status code even if "Content-Type: application/json" was received in header. --- lib/recurly/recurly_error.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/recurly/recurly_error.php b/lib/recurly/recurly_error.php index e924e48f..48db934d 100644 --- a/lib/recurly/recurly_error.php +++ b/lib/recurly/recurly_error.php @@ -54,13 +54,17 @@ public static function fromResponse(\Recurly\Response $response): \Recurly\Recur $api_error = \Recurly\Resources\ErrorMayHaveTransaction::fromResponse($response, $error); return new $klass($error->message, $api_error); } - } else { - $error_type = static::errorFromStatus($response->getStatusCode()); - $klass = static::titleize($error_type, '\\Recurly\\Errors\\'); - if (class_exists($klass)) { - return new $klass('An unexpected error has occurred'); - } } + + // "Content-type: application/json" may appear without a body after a HEAD request. + // If the above failed, try guessing from the status code. + $error_type = static::errorFromStatus($response->getStatusCode()); + $klass = static::titleize($error_type, '\\Recurly\\Errors\\'); + if (class_exists($klass)) { + return new $klass('An unexpected error has occurred'); + } + + // No valid error type was found, sorry. $klass = static::_defaultErrorType($response); return new $klass('An unexpected error has occurred'); From c66fd99f352b8b21ba610e21d1c7cb34df601b97 Mon Sep 17 00:00:00 2001 From: Sinus Date: Mon, 14 Mar 2022 19:09:16 -0400 Subject: [PATCH 4/5] Check response validity always, not just on toResource, so that makeRequest AND pagerCount are validated. --- lib/recurly/base_client.php | 1 + lib/recurly/response.php | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/recurly/base_client.php b/lib/recurly/base_client.php index 39fa7f35..d93441a8 100644 --- a/lib/recurly/base_client.php +++ b/lib/recurly/base_client.php @@ -126,6 +126,7 @@ private function _getResponse(\Recurly\Request $request): \Recurly\Response 'response_headers' => $response->getHeaders() ] ); + $response->assertValid(); // throws \Recurly\RecurlyError if response is bad return $response; } diff --git a/lib/recurly/response.php b/lib/recurly/response.php index baf7a988..d59e3fdc 100644 --- a/lib/recurly/response.php +++ b/lib/recurly/response.php @@ -53,6 +53,16 @@ public function getRequest(): \Recurly\Request return $this->_request; } + /** + * Makes sure the response is valid. Throws RecurlyError otherwise. + */ + public function assertValid(): void + { + if ($this->_status_code < 200 || $this->_status_code >= 300) { + throw \Recurly\RecurlyError::fromResponse($this); + } + } + /** * Converts the Response into a \Recurly\RecurlyResource * @@ -60,16 +70,12 @@ public function getRequest(): \Recurly\Request */ public function toResource(): \Recurly\RecurlyResource { - if ($this->_status_code >= 200 && $this->_status_code < 300) { - if (empty($this->_response)) { - return \Recurly\RecurlyResource::fromEmpty($this); - } elseif (in_array($this->getContentType(), static::BINARY_TYPES)) { - return \Recurly\RecurlyResource::fromBinary($this->_response, $this); - } else { - return \Recurly\RecurlyResource::fromResponse($this); - } + if (empty($this->_response)) { + return \Recurly\RecurlyResource::fromEmpty($this); + } elseif (in_array($this->getContentType(), static::BINARY_TYPES)) { + return \Recurly\RecurlyResource::fromBinary($this->_response, $this); } else { - throw \Recurly\RecurlyError::fromResponse($this); + return \Recurly\RecurlyResource::fromResponse($this); } } From 7caf7ce332132c3c3a5e895798305d32d2c62d50 Mon Sep 17 00:00:00 2001 From: Sinus Date: Mon, 14 Mar 2022 19:24:10 -0400 Subject: [PATCH 5/5] Fixed response test to verify assertion, not toResource erroring out, because toResource no longer throws errors - makeRequest does. --- tests/Response_Test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Response_Test.php b/tests/Response_Test.php index 6444190e..1481c68c 100644 --- a/tests/Response_Test.php +++ b/tests/Response_Test.php @@ -175,12 +175,12 @@ public function testToResourceEmpty(): void $this->assertInstanceOf(\Recurly\EmptyResource::class, $result); } - public function testToResourceError(): void + public function testAssertValid(): void { $this->expectException(\Recurly\RecurlyError::class); $response = new Response('', $this->request); $response->setHeaders(['HTTP/1.1 403 Forbidden']); - $result = $response->toResource(); + $result = $response->assertValid(); } public function testGetRawResponse(): void