From 318598741f527a9a610156270309caf77abcbed5 Mon Sep 17 00:00:00 2001 From: Omar Date: Mon, 23 Oct 2023 14:33:20 +0200 Subject: [PATCH 1/3] Issue #212: Compatibility with psr/http-message 2.0 --- lib/SparkPost/SparkPostResponse.php | 32 +++++++++++++++-------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/SparkPost/SparkPostResponse.php b/lib/SparkPost/SparkPostResponse.php index 402a2b2..266ba63 100644 --- a/lib/SparkPost/SparkPostResponse.php +++ b/lib/SparkPost/SparkPostResponse.php @@ -2,6 +2,8 @@ namespace SparkPost; +use GuzzleHttp\Psr7\Stream; +use Psr\Http\Message\MessageInterface; use Psr\Http\Message\ResponseInterface as ResponseInterface; use Psr\Http\Message\StreamInterface as StreamInterface; @@ -43,80 +45,80 @@ public function getRequest() * * @return array $body - the json decoded body from the http response */ - public function getBody() + public function getBody() : StreamInterface { $body = $this->response->getBody(); $body_string = $body->__toString(); $json = json_decode($body_string, true); - return $json; + return new Stream($json); } /** * pass these down to the response given in the constructor. */ - public function getProtocolVersion() + public function getProtocolVersion() : string { return $this->response->getProtocolVersion(); } - public function withProtocolVersion($version) + public function withProtocolVersion($version) : MessageInterface { return $this->response->withProtocolVersion($version); } - public function getHeaders() + public function getHeaders() : array { return $this->response->getHeaders(); } - public function hasHeader($name) + public function hasHeader($name) : bool { return $this->response->hasHeader($name); } - public function getHeader($name) + public function getHeader($name) : array { return $this->response->getHeader($name); } - public function getHeaderLine($name) + public function getHeaderLine($name) : string { return $this->response->getHeaderLine($name); } - public function withHeader($name, $value) + public function withHeader($name, $value) : MessageInterface { return $this->response->withHeader($name, $value); } - public function withAddedHeader($name, $value) + public function withAddedHeader($name, $value) : MessageInterface { return $this->response->withAddedHeader($name, $value); } - public function withoutHeader($name) + public function withoutHeader($name) : MessageInterface { return $this->response->withoutHeader($name); } - public function withBody(StreamInterface $body) + public function withBody(StreamInterface $body) : MessageInterface { return $this->response->withBody($body); } - public function getStatusCode() + public function getStatusCode() : int { return $this->response->getStatusCode(); } - public function withStatus($code, $reasonPhrase = '') + public function withStatus($code, $reasonPhrase = '') : ResponseInterface { return $this->response->withStatus($code, $reasonPhrase); } - public function getReasonPhrase() + public function getReasonPhrase() : string { return $this->response->getReasonPhrase(); } From b5e14f80738aa82a9eff22a5cf7c087aa0f25b04 Mon Sep 17 00:00:00 2001 From: Omar Date: Mon, 23 Oct 2023 14:40:06 +0200 Subject: [PATCH 2/3] Instead of returning the body as Json return the body as a stream --- lib/SparkPost/SparkPostResponse.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/lib/SparkPost/SparkPostResponse.php b/lib/SparkPost/SparkPostResponse.php index 266ba63..038cf5d 100644 --- a/lib/SparkPost/SparkPostResponse.php +++ b/lib/SparkPost/SparkPostResponse.php @@ -2,7 +2,6 @@ namespace SparkPost; -use GuzzleHttp\Psr7\Stream; use Psr\Http\Message\MessageInterface; use Psr\Http\Message\ResponseInterface as ResponseInterface; use Psr\Http\Message\StreamInterface as StreamInterface; @@ -40,19 +39,9 @@ public function getRequest() return $this->request; } - /** - * Returns the body. - * - * @return array $body - the json decoded body from the http response - */ public function getBody() : StreamInterface { - $body = $this->response->getBody(); - $body_string = $body->__toString(); - - $json = json_decode($body_string, true); - - return new Stream($json); + return $this->response->getBody(); } /** From 0406937af4a562ce4024398c3728bc46b58ea4cf Mon Sep 17 00:00:00 2001 From: Omar Date: Mon, 23 Oct 2023 14:53:21 +0200 Subject: [PATCH 3/3] Function to get the body in the same format before using psr http-message 2.0 --- lib/SparkPost/SparkPostResponse.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/SparkPost/SparkPostResponse.php b/lib/SparkPost/SparkPostResponse.php index 038cf5d..d953787 100644 --- a/lib/SparkPost/SparkPostResponse.php +++ b/lib/SparkPost/SparkPostResponse.php @@ -39,6 +39,15 @@ public function getRequest() return $this->request; } + /** + * Gets the body in json format. + * + * @return array Decoded body. + */ + public function getBodyAsJson() : array { + return json_decode($this->getBody()->getContents(), true); + } + public function getBody() : StreamInterface { return $this->response->getBody();