Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync Compatibility with psr/http-message 2.0 #2

Merged
merged 3 commits into from
Sep 16, 2024
Merged
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"php-http/httplug": "^1.0 || ^2.0",
"php-http/message": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.0"
"php-http/discovery": "^1.0",
"psr/http-client": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0 || ^9.0",
Expand Down
13 changes: 6 additions & 7 deletions lib/SparkPost/SparkPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace SparkPost;

use Http\Client\HttpClient;
use Http\Client\HttpAsyncClient;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\RequestFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;

class SparkPost
Expand All @@ -16,7 +15,7 @@ class SparkPost
private $version = '2.3.0';

/**
* @var HttpClient|HttpAsyncClient used to make requests
* @var ClientInterface used to make requests
*/
private $httpClient;

Expand Down Expand Up @@ -52,7 +51,7 @@ class SparkPost
/**
* Sets up the SparkPost instance.
*
* @param HttpClient $httpClient - An httplug client or adapter
* @param ClientInterface $httpClient - An httplug client or adapter
* @param array $options - An array to overide default options or a string to be used as an API key
*/
public function __construct($httpClient, array $options)
Expand Down Expand Up @@ -270,14 +269,14 @@ public function getUrl($path, $params = [])
/**
* Sets $httpClient to be used for request.
*
* @param HttpClient|HttpAsyncClient $httpClient - the client to be used for request
* @param ClientInterface $httpClient - the client to be used for request
*
* @return SparkPost
*/
public function setHttpClient($httpClient)
{
if (!($httpClient instanceof HttpAsyncClient || $httpClient instanceof HttpClient)) {
throw new \LogicException(sprintf('Parameter to SparkPost::setHttpClient must be instance of "%s" or "%s"', HttpClient::class, HttpAsyncClient::class));
if (!($httpClient instanceof ClientInterface)) {
throw new \LogicException(sprintf('Parameter to SparkPost::setHttpClient must be instance of "%s"', ClientInterface::class));
}

$this->httpClient = $httpClient;
Expand Down
44 changes: 22 additions & 22 deletions lib/SparkPost/SparkPostResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SparkPost;

use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface as ResponseInterface;
use Psr\Http\Message\StreamInterface as StreamInterface;

Expand Down Expand Up @@ -39,84 +40,83 @@ public function getRequest()
}

/**
* Returns the body.
* Gets the body in json format.
*
* @return array $body - the json decoded body from the http response
* @return array Decoded body.
*/
public function getBody()
{
$body = $this->response->getBody();
$body_string = $body->__toString();

$json = json_decode($body_string, true);
public function getBodyAsJson() : array {
return json_decode($this->getBody()->getContents(), true);
}

return $json;
public function getBody() : StreamInterface
{
return $this->response->getBody();
}

/**
* 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();
}
Expand Down