From 61d03262a996cac4322dff259dea2af14c32f620 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 12 Oct 2015 19:23:58 +0200 Subject: [PATCH] Removed dependency on Happyr/AutoDiscovery --- composer.json | 4 +++- src/Mailgun/Connection/RestClient.php | 28 ++++++++++++++++++--------- src/Mailgun/Mailgun.php | 11 +++++++++-- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 48126df..61f13a2 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,9 @@ "description": "The Mailgun SDK provides methods for all API functions.", "require": { "guzzlehttp/psr7": "~1.2", - "happyr/http-auto-discovery": "0.1.*" + "php-http/adapter-implementation": "^0.1", + "php-http/adapter": "^0.1", + "php-http/discovery": "^0.1" }, "require-dev": { "php": ">=5.4.0", diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index c3dec70..3c5f846 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -4,13 +4,14 @@ namespace Mailgun\Connection; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; +use Http\Adapter\HttpAdapter; +use Http\Discovery\HttpAdapterDiscovery; use Mailgun\Connection\Exceptions\GenericHTTPError; use Mailgun\Connection\Exceptions\InvalidCredentials; use Mailgun\Connection\Exceptions\MissingRequiredParameters; use Mailgun\Connection\Exceptions\MissingEndpoint; use Mailgun\Constants\Api; use Mailgun\Constants\ExceptionMessages; -use Happyr\HttpAutoDiscovery\Client as HttpClient; use Psr\Http\Message\ResponseInterface; /** @@ -24,9 +25,9 @@ class RestClient private $apiKey; /** - * @var HttpClient + * @var HttpAdapter */ - protected $httpClient; + protected $httpAdapter; /** * @var string @@ -39,21 +40,30 @@ class RestClient * @param string $apiVersion * @param bool $ssl */ - public function __construct($apiKey, $apiHost, $apiVersion, $ssl) + public function __construct($apiKey, $apiHost, $apiVersion, $ssl, HttpAdapter $adapter = null) { $this->apiKey = $apiKey; + $this->httpAdapter = $httpAdapter; $this->apiEndpoint = $this->generateEndpoint($apiHost, $apiVersion, $ssl); } /** - * Get a HttpClient. + * @param string $method + * @param string $uri + * @param array $headers + * @param array $body + * @param array $files * - * @return HttpClient + * @return \stdClass + * @throws GenericHTTPError + * @throws InvalidCredentials + * @throws MissingEndpoint + * @throws MissingRequiredParameters */ protected function send($method, $uri, array $headers = [], $body = [], $files = []) { - if ($this->httpClient === null) { - $this->httpClient = new HttpClient(); + if ($this->httpAdapter === null) { + $this->httpAdapter = HttpAdapterDiscovery::find(); } $headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; @@ -65,7 +75,7 @@ class RestClient } $request = new Request($method, $this->apiEndpoint.$uri, $headers, $body); - $response = $this->httpClient->sendRequest($request); + $response = $this->httpAdapter->sendRequest($request); return $this->responseHandler($response); } diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index f9921ef..afc4eaa 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -2,6 +2,7 @@ namespace Mailgun; +use Http\Adapter\HttpAdapter; use Mailgun\Constants\ExceptionMessages; use Mailgun\Messages\Exceptions; use Mailgun\Connection\RestClient; @@ -33,9 +34,15 @@ class Mailgun{ * @param string $apiVersion * @param bool $ssl */ - public function __construct($apiKey = null, $apiEndpoint = "api.mailgun.net", $apiVersion = "v3", $ssl = true){ + public function __construct( + $apiKey = null, + $apiEndpoint = "api.mailgun.net", + $apiVersion = "v3", + $ssl = true, + HttpAdapter $adapter = null + ) { $this->apiKey = $apiKey; - $this->restClient = new RestClient($apiKey, $apiEndpoint, $apiVersion, $ssl); + $this->restClient = new RestClient($apiKey, $apiEndpoint, $apiVersion, $ssl, $adapter); } /**