From ffab6c9e39cc5bc0c63599f11d0206baf1362fef Mon Sep 17 00:00:00 2001 From: Anders Munk Date: Thu, 23 Jul 2015 13:27:32 +0200 Subject: [PATCH 01/19] Bumped to guzzlehttp/guzzle:~6.0 PHPUnit OK (49 tests, 75 assertions) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 61bc460..d0d55fc 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "mailgun/mailgun-php", "description": "The Mailgun SDK provides methods for all API functions.", "require": { - "guzzlehttp/guzzle": "~5.0" + "guzzlehttp/guzzle": "~6.0" }, "require-dev": { "php": ">=5.4.0", From 04f1baa2fdbb4d3fba035e28ce66ec676e382523 Mon Sep 17 00:00:00 2001 From: Anders Munk Date: Thu, 23 Jul 2015 14:25:28 +0200 Subject: [PATCH 02/19] Updated rest client to use PSR7. No phpunit tests yet; will come soon. Manual sending message to mailgun API works. --- src/Mailgun/Connection/RestClient.php | 84 ++++++++++++++++----------- 1 file changed, 49 insertions(+), 35 deletions(-) diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 02e65db..9ea695d 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -3,16 +3,15 @@ namespace Mailgun\Connection; use GuzzleHttp\Client as Guzzle; -use GuzzleHttp\Message\ResponseInterface; -use GuzzleHttp\Post\PostBodyInterface; -use GuzzleHttp\Post\PostFile; -use GuzzleHttp\Query; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; 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 Psr\Http\Message\ResponseInterface; /** * This class is a wrapper for the Guzzle (HTTP Client Library). @@ -39,14 +38,12 @@ class RestClient { $this->apiKey = $apiKey; $this->mgClient = new Guzzle([ - 'base_url'=>$this->generateEndpoint($apiEndpoint, $apiVersion, $ssl), - 'defaults'=>[ - 'auth' => array(Api::API_USER, $this->apiKey), - 'exceptions' => false, - 'config' => ['curl' => [ CURLOPT_FORBID_REUSE => true ]], - 'headers' => [ - 'User-Agent' => Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION, - ], + 'base_uri'=>$this->generateEndpoint($apiEndpoint, $apiVersion, $ssl), + 'auth' => array(Api::API_USER, $this->apiKey), + 'exceptions' => false, + 'config' => ['curl' => [ CURLOPT_FORBID_REUSE => true ]], + 'headers' => [ + 'User-Agent' => Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION, ], ]); } @@ -65,26 +62,47 @@ class RestClient */ public function post($endpointUrl, $postData = array(), $files = array()) { - $request = $this->mgClient->createRequest('POST', $endpointUrl, ['body' => $postData]); - /** @var \GuzzleHttp\Post\PostBodyInterface $postBody */ - $postBody = $request->getBody(); - $postBody->setAggregator(Query::duplicateAggregator()); + $request = new Request('post', $endpointUrl); + $postFiles = []; $fields = ['message', 'attachment', 'inline']; foreach ($fields as $fieldName) { if (isset($files[$fieldName])) { if (is_array($files[$fieldName])) { foreach ($files[$fieldName] as $file) { - $this->addFile($postBody, $fieldName, $file); + $postFiles[] = $this->addFile($fieldName, $file); } } else { - $this->addFile($postBody, $fieldName, $files[$fieldName]); + $postFiles[] = $this->addFile($fieldName, $files[$fieldName]); } } } - $response = $this->mgClient->send($request); + $postDataMultipart = []; + foreach($postData AS $key => $value) + { + if (is_array($value)) + { + foreach($value AS $subValue) + { + $postDataMultipart[] = [ + 'name' => $key, + 'contents' => $subValue + ]; + } + } + else + { + $postDataMultipart[] = [ + 'name' => $key, + 'contents' => $value + ]; + } + } + $response = $this->mgClient->send($request, [ + 'multipart' => array_merge($postDataMultipart, $postFiles) + ]); return $this->responseHandler($response); } @@ -102,7 +120,6 @@ class RestClient public function get($endpointUrl, $queryString = array()) { $response = $this->mgClient->get($endpointUrl, ['query' => $queryString]); - return $this->responseHandler($response); } @@ -119,7 +136,6 @@ class RestClient public function delete($endpointUrl) { $response = $this->mgClient->delete($endpointUrl); - return $this->responseHandler($response); } @@ -136,18 +152,12 @@ class RestClient */ public function put($endpointUrl, $putData) { - $request = $this->mgClient->createRequest('PUT', $endpointUrl, ['body' => $putData]); - /** @var \GuzzleHttp\Post\PostBodyInterface $postBody */ - $postBody = $request->getBody(); - $postBody->setAggregator(Query::duplicateAggregator()); - - $response = $this->mgClient->send($request); - + $response = $this->mgClient->request('PUT', $endpointUrl, ['body' => $putData]); return $this->responseHandler($response); } /** - * @param ResponseInterface $responseObj + * @param Response $responseObj * * @return \stdClass * @@ -180,11 +190,11 @@ class RestClient } /** - * @param \Guzzle\Http\Message\Response $responseObj + * @param Response $responseObj * * @return string */ - protected function getResponseExceptionMessage(\GuzzleHttp\Message\Response $responseObj) + protected function getResponseExceptionMessage(Response $responseObj) { $body = (string) $responseObj->getBody(); $response = json_decode($body); @@ -200,7 +210,7 @@ class RestClient * * @return string */ - private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) + protected function generateEndpoint($apiEndpoint, $apiVersion, $ssl) { if (!$ssl) { return "http://".$apiEndpoint."/".$apiVersion."/"; @@ -212,11 +222,11 @@ class RestClient /** * Add a file to the postBody. * - * @param PostBodyInterface $postBody + * @param \GuzzleHttp\Psr7\Stream $postBody * @param string $fieldName * @param string|array $filePath */ - private function addFile(PostBodyInterface $postBody, $fieldName, $filePath) + protected function addFile($fieldName, $filePath) { $filename = null; // Backward compatibility code @@ -230,6 +240,10 @@ class RestClient $filePath = substr($filePath, 1); } - $postBody->addFile(new PostFile($fieldName, fopen($filePath, 'r'), $filename)); + return [ + 'name' => $fieldName, + 'contents' => fopen($filePath, 'r'), + 'filename' => $filename + ]; } } From b2dd642235ff674ac6da0fb2154dd24103745d3c Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 3 Oct 2015 20:01:58 +0200 Subject: [PATCH 03/19] working on http adapter --- composer.json | 5 +- src/Mailgun/Connection/RestClient.php | 101 +++++++++++------- .../Tests/Mock/Connection/TestBroker.php | 4 +- 3 files changed, 66 insertions(+), 44 deletions(-) diff --git a/composer.json b/composer.json index d0d55fc..7d6f7ae 100644 --- a/composer.json +++ b/composer.json @@ -2,11 +2,12 @@ "name": "mailgun/mailgun-php", "description": "The Mailgun SDK provides methods for all API functions.", "require": { - "guzzlehttp/guzzle": "~6.0" + "happyr/http-auto-discovery": "dev-master" }, "require-dev": { "php": ">=5.4.0", - "phpunit/phpunit": "~4.6" + "phpunit/phpunit": "~4.6", + "php-http/guzzle6-adapter": "^0.1.0" }, "autoload": { "psr-0": { diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 9ea695d..037eba3 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -2,15 +2,13 @@ namespace Mailgun\Connection; -use GuzzleHttp\Client as Guzzle; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\Psr7\Response; 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,30 +22,55 @@ class RestClient private $apiKey; /** - * @var Guzzle + * @var HttpClient */ - protected $mgClient; + protected $httpClient; + + /** + * @var string + */ + protected $apiEndpoint; /** * @param string $apiKey - * @param string $apiEndpoint + * @param string $apiHost * @param string $apiVersion * @param bool $ssl */ - public function __construct($apiKey, $apiEndpoint, $apiVersion, $ssl) + public function __construct($apiKey, $apiHost, $apiVersion, $ssl) { $this->apiKey = $apiKey; + $this->apiEndpoint = $this->generateEndpoint($apiHost, $apiVersion, $ssl); + + //TODO remove me $this->mgClient = new Guzzle([ - 'base_uri'=>$this->generateEndpoint($apiEndpoint, $apiVersion, $ssl), + 'base_uri' => $this->generateEndpoint($apiHost, $apiVersion, $ssl), 'auth' => array(Api::API_USER, $this->apiKey), 'exceptions' => false, - 'config' => ['curl' => [ CURLOPT_FORBID_REUSE => true ]], + 'config' => ['curl' => [CURLOPT_FORBID_REUSE => true]], 'headers' => [ 'User-Agent' => Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION, ], ]); } + /** + * Get a HttpClient. + * + * @return HttpClient + */ + protected function send($method, $uri, array $headers = [], $body = null) + { + if ($this->httpClient === null) { + $this->httpClient = new HttpClient(); + } + + $headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; + + + return $this->httpClient->send($method, $this->apiEndpoint.$uri, $headers, $body); + } + /** * @param string $endpointUrl * @param array $postData @@ -70,39 +93,35 @@ class RestClient if (isset($files[$fieldName])) { if (is_array($files[$fieldName])) { foreach ($files[$fieldName] as $file) { - $postFiles[] = $this->addFile($fieldName, $file); + $postFiles[] = $this->prepareFile($fieldName, $file); } } else { - $postFiles[] = $this->addFile($fieldName, $files[$fieldName]); + $postFiles[] = $this->prepareFile($fieldName, $files[$fieldName]); } } } $postDataMultipart = []; - foreach($postData AS $key => $value) - { - if (is_array($value)) - { - foreach($value AS $subValue) - { + foreach ($postData as $key => $value) { + if (is_array($value)) { + foreach ($value as $subValue) { $postDataMultipart[] = [ 'name' => $key, - 'contents' => $subValue + 'contents' => $subValue, ]; } - } - else - { + } else { $postDataMultipart[] = [ 'name' => $key, - 'contents' => $value + 'contents' => $value, ]; } } - $response = $this->mgClient->send($request, [ - 'multipart' => array_merge($postDataMultipart, $postFiles) + $response = $this->send('POST', $endpointUrl, [ + 'multipart' => array_merge($postDataMultipart, $postFiles), ]); + return $this->responseHandler($response); } @@ -119,7 +138,8 @@ class RestClient */ public function get($endpointUrl, $queryString = array()) { - $response = $this->mgClient->get($endpointUrl, ['query' => $queryString]); + $response = $this->send('GET', $endpointUrl.'?'.http_build_query($queryString)); + return $this->responseHandler($response); } @@ -135,7 +155,8 @@ class RestClient */ public function delete($endpointUrl) { - $response = $this->mgClient->delete($endpointUrl); + $response = $this->send('DELETE', $endpointUrl); + return $this->responseHandler($response); } @@ -152,12 +173,13 @@ class RestClient */ public function put($endpointUrl, $putData) { - $response = $this->mgClient->request('PUT', $endpointUrl, ['body' => $putData]); + $response = $this->send('PUT', $endpointUrl, [], $putData); + return $this->responseHandler($response); } /** - * @param Response $responseObj + * @param ResponseInterface $responseObj * * @return \stdClass * @@ -166,7 +188,7 @@ class RestClient * @throws MissingEndpoint * @throws MissingRequiredParameters */ - public function responseHandler($responseObj) + public function responseHandler(ResponseInterface $responseObj) { $httpResponseCode = $responseObj->getStatusCode(); if ($httpResponseCode === 200) { @@ -190,16 +212,16 @@ class RestClient } /** - * @param Response $responseObj + * @param ResponseInterface $responseObj * * @return string */ - protected function getResponseExceptionMessage(Response $responseObj) + protected function getResponseExceptionMessage(ResponseInterface $responseObj) { $body = (string) $responseObj->getBody(); $response = json_decode($body); if (json_last_error() == JSON_ERROR_NONE && isset($response->message)) { - return " ".$response->message; + return ' '.$response->message; } } @@ -213,20 +235,19 @@ class RestClient protected function generateEndpoint($apiEndpoint, $apiVersion, $ssl) { if (!$ssl) { - return "http://".$apiEndpoint."/".$apiVersion."/"; + return 'http://'.$apiEndpoint.'/'.$apiVersion.'/'; } else { - return "https://".$apiEndpoint."/".$apiVersion."/"; + return 'https://'.$apiEndpoint.'/'.$apiVersion.'/'; } } /** - * Add a file to the postBody. + * Prepare a file for the postBody. * - * @param \GuzzleHttp\Psr7\Stream $postBody - * @param string $fieldName - * @param string|array $filePath + * @param string $fieldName + * @param string|array $filePath */ - protected function addFile($fieldName, $filePath) + protected function prepareFile($fieldName, $filePath) { $filename = null; // Backward compatibility code @@ -243,7 +264,7 @@ class RestClient return [ 'name' => $fieldName, 'contents' => fopen($filePath, 'r'), - 'filename' => $filename + 'filename' => $filename, ]; } } diff --git a/tests/Mailgun/Tests/Mock/Connection/TestBroker.php b/tests/Mailgun/Tests/Mock/Connection/TestBroker.php index a9ee3b0..6652aa5 100644 --- a/tests/Mailgun/Tests/Mock/Connection/TestBroker.php +++ b/tests/Mailgun/Tests/Mock/Connection/TestBroker.php @@ -13,10 +13,10 @@ class TestBroker extends RestClient protected $apiEndpoint; - public function __construct($apiKey = null, $apiEndpoint = "api.mailgun.net", $apiVersion = "v3") + public function __construct($apiKey = null, $apiHost = "api.mailgun.net", $apiVersion = "v3") { $this->apiKey = $apiKey; - $this->apiEndpoint = $apiEndpoint; + $this->apiEndpoint = $apiHost; } public function post($endpointUrl, $postData = array(), $files = array()) From 5ec7aabfe224ad180b75c92a834c6fdc55331b87 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 3 Oct 2015 22:08:55 +0200 Subject: [PATCH 04/19] make sure we can attach files --- composer.json | 3 ++- src/Mailgun/Connection/RestClient.php | 32 +++++++++++++-------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index 7d6f7ae..091add5 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,8 @@ "name": "mailgun/mailgun-php", "description": "The Mailgun SDK provides methods for all API functions.", "require": { - "happyr/http-auto-discovery": "dev-master" + "guzzlehttp/psr7": "~1.2", + "happyr/http-auto-discovery": "0.1.*" }, "require-dev": { "php": ">=5.4.0", diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 037eba3..75523bf 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -2,6 +2,8 @@ namespace Mailgun\Connection; +use GuzzleHttp\Psr7\MultipartStream; +use GuzzleHttp\Psr7\Request; use Mailgun\Connection\Exceptions\GenericHTTPError; use Mailgun\Connection\Exceptions\InvalidCredentials; use Mailgun\Connection\Exceptions\MissingRequiredParameters; @@ -41,17 +43,6 @@ class RestClient { $this->apiKey = $apiKey; $this->apiEndpoint = $this->generateEndpoint($apiHost, $apiVersion, $ssl); - - //TODO remove me - $this->mgClient = new Guzzle([ - 'base_uri' => $this->generateEndpoint($apiHost, $apiVersion, $ssl), - 'auth' => array(Api::API_USER, $this->apiKey), - 'exceptions' => false, - 'config' => ['curl' => [CURLOPT_FORBID_REUSE => true]], - 'headers' => [ - 'User-Agent' => Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION, - ], - ]); } /** @@ -59,16 +50,22 @@ class RestClient * * @return HttpClient */ - protected function send($method, $uri, array $headers = [], $body = null) + protected function send($method, $uri, array $headers = [], $body = [], $files = []) { if ($this->httpClient === null) { $this->httpClient = new HttpClient(); } $headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; + $headers['Authorization'] = 'Basic '. base64_encode(sprintf("%s:%s",Api::API_USER, $this->apiKey)); + if (!empty($files)) { + $body = new MultipartStream($files); + $headers['Content-Type'] = 'multipart/form-data; boundary='.$body->getBoundary(); + } + $request = new Request($method, $this->apiEndpoint.$uri, $headers, $body); - return $this->httpClient->send($method, $this->apiEndpoint.$uri, $headers, $body); + return $this->httpClient->sendRequest($request); } /** @@ -85,7 +82,6 @@ class RestClient */ public function post($endpointUrl, $postData = array(), $files = array()) { - $request = new Request('post', $endpointUrl); $postFiles = []; $fields = ['message', 'attachment', 'inline']; @@ -118,9 +114,11 @@ class RestClient } } - $response = $this->send('POST', $endpointUrl, [ - 'multipart' => array_merge($postDataMultipart, $postFiles), - ]); + $response = $this->send('POST', $endpointUrl, + [], + [], + array_merge($postDataMultipart, $postFiles) + ); return $this->responseHandler($response); } From b080b181ac3c4f3c3e31dff028af5b03b409fd08 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 3 Oct 2015 20:01:58 +0200 Subject: [PATCH 05/19] Make sure we do not depend on any http transport library --- composer.json | 6 +- src/Mailgun/Connection/RestClient.php | 115 ++++++++++-------- .../Tests/Mock/Connection/TestBroker.php | 4 +- 3 files changed, 73 insertions(+), 52 deletions(-) diff --git a/composer.json b/composer.json index d0d55fc..091add5 100644 --- a/composer.json +++ b/composer.json @@ -2,11 +2,13 @@ "name": "mailgun/mailgun-php", "description": "The Mailgun SDK provides methods for all API functions.", "require": { - "guzzlehttp/guzzle": "~6.0" + "guzzlehttp/psr7": "~1.2", + "happyr/http-auto-discovery": "0.1.*" }, "require-dev": { "php": ">=5.4.0", - "phpunit/phpunit": "~4.6" + "phpunit/phpunit": "~4.6", + "php-http/guzzle6-adapter": "^0.1.0" }, "autoload": { "psr-0": { diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 9ea695d..75523bf 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -2,15 +2,15 @@ namespace Mailgun\Connection; -use GuzzleHttp\Client as Guzzle; +use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; -use GuzzleHttp\Psr7\Response; 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,28 +24,48 @@ class RestClient private $apiKey; /** - * @var Guzzle + * @var HttpClient */ - protected $mgClient; + protected $httpClient; + + /** + * @var string + */ + protected $apiEndpoint; /** * @param string $apiKey - * @param string $apiEndpoint + * @param string $apiHost * @param string $apiVersion * @param bool $ssl */ - public function __construct($apiKey, $apiEndpoint, $apiVersion, $ssl) + public function __construct($apiKey, $apiHost, $apiVersion, $ssl) { $this->apiKey = $apiKey; - $this->mgClient = new Guzzle([ - 'base_uri'=>$this->generateEndpoint($apiEndpoint, $apiVersion, $ssl), - 'auth' => array(Api::API_USER, $this->apiKey), - 'exceptions' => false, - 'config' => ['curl' => [ CURLOPT_FORBID_REUSE => true ]], - 'headers' => [ - 'User-Agent' => Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION, - ], - ]); + $this->apiEndpoint = $this->generateEndpoint($apiHost, $apiVersion, $ssl); + } + + /** + * Get a HttpClient. + * + * @return HttpClient + */ + protected function send($method, $uri, array $headers = [], $body = [], $files = []) + { + if ($this->httpClient === null) { + $this->httpClient = new HttpClient(); + } + + $headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; + $headers['Authorization'] = 'Basic '. base64_encode(sprintf("%s:%s",Api::API_USER, $this->apiKey)); + + if (!empty($files)) { + $body = new MultipartStream($files); + $headers['Content-Type'] = 'multipart/form-data; boundary='.$body->getBoundary(); + } + $request = new Request($method, $this->apiEndpoint.$uri, $headers, $body); + + return $this->httpClient->sendRequest($request); } /** @@ -62,7 +82,6 @@ class RestClient */ public function post($endpointUrl, $postData = array(), $files = array()) { - $request = new Request('post', $endpointUrl); $postFiles = []; $fields = ['message', 'attachment', 'inline']; @@ -70,39 +89,37 @@ class RestClient if (isset($files[$fieldName])) { if (is_array($files[$fieldName])) { foreach ($files[$fieldName] as $file) { - $postFiles[] = $this->addFile($fieldName, $file); + $postFiles[] = $this->prepareFile($fieldName, $file); } } else { - $postFiles[] = $this->addFile($fieldName, $files[$fieldName]); + $postFiles[] = $this->prepareFile($fieldName, $files[$fieldName]); } } } $postDataMultipart = []; - foreach($postData AS $key => $value) - { - if (is_array($value)) - { - foreach($value AS $subValue) - { + foreach ($postData as $key => $value) { + if (is_array($value)) { + foreach ($value as $subValue) { $postDataMultipart[] = [ 'name' => $key, - 'contents' => $subValue + 'contents' => $subValue, ]; } - } - else - { + } else { $postDataMultipart[] = [ 'name' => $key, - 'contents' => $value + 'contents' => $value, ]; } } - $response = $this->mgClient->send($request, [ - 'multipart' => array_merge($postDataMultipart, $postFiles) - ]); + $response = $this->send('POST', $endpointUrl, + [], + [], + array_merge($postDataMultipart, $postFiles) + ); + return $this->responseHandler($response); } @@ -119,7 +136,8 @@ class RestClient */ public function get($endpointUrl, $queryString = array()) { - $response = $this->mgClient->get($endpointUrl, ['query' => $queryString]); + $response = $this->send('GET', $endpointUrl.'?'.http_build_query($queryString)); + return $this->responseHandler($response); } @@ -135,7 +153,8 @@ class RestClient */ public function delete($endpointUrl) { - $response = $this->mgClient->delete($endpointUrl); + $response = $this->send('DELETE', $endpointUrl); + return $this->responseHandler($response); } @@ -152,12 +171,13 @@ class RestClient */ public function put($endpointUrl, $putData) { - $response = $this->mgClient->request('PUT', $endpointUrl, ['body' => $putData]); + $response = $this->send('PUT', $endpointUrl, [], $putData); + return $this->responseHandler($response); } /** - * @param Response $responseObj + * @param ResponseInterface $responseObj * * @return \stdClass * @@ -166,7 +186,7 @@ class RestClient * @throws MissingEndpoint * @throws MissingRequiredParameters */ - public function responseHandler($responseObj) + public function responseHandler(ResponseInterface $responseObj) { $httpResponseCode = $responseObj->getStatusCode(); if ($httpResponseCode === 200) { @@ -190,16 +210,16 @@ class RestClient } /** - * @param Response $responseObj + * @param ResponseInterface $responseObj * * @return string */ - protected function getResponseExceptionMessage(Response $responseObj) + protected function getResponseExceptionMessage(ResponseInterface $responseObj) { $body = (string) $responseObj->getBody(); $response = json_decode($body); if (json_last_error() == JSON_ERROR_NONE && isset($response->message)) { - return " ".$response->message; + return ' '.$response->message; } } @@ -213,20 +233,19 @@ class RestClient protected function generateEndpoint($apiEndpoint, $apiVersion, $ssl) { if (!$ssl) { - return "http://".$apiEndpoint."/".$apiVersion."/"; + return 'http://'.$apiEndpoint.'/'.$apiVersion.'/'; } else { - return "https://".$apiEndpoint."/".$apiVersion."/"; + return 'https://'.$apiEndpoint.'/'.$apiVersion.'/'; } } /** - * Add a file to the postBody. + * Prepare a file for the postBody. * - * @param \GuzzleHttp\Psr7\Stream $postBody - * @param string $fieldName - * @param string|array $filePath + * @param string $fieldName + * @param string|array $filePath */ - protected function addFile($fieldName, $filePath) + protected function prepareFile($fieldName, $filePath) { $filename = null; // Backward compatibility code @@ -243,7 +262,7 @@ class RestClient return [ 'name' => $fieldName, 'contents' => fopen($filePath, 'r'), - 'filename' => $filename + 'filename' => $filename, ]; } } diff --git a/tests/Mailgun/Tests/Mock/Connection/TestBroker.php b/tests/Mailgun/Tests/Mock/Connection/TestBroker.php index a9ee3b0..6652aa5 100644 --- a/tests/Mailgun/Tests/Mock/Connection/TestBroker.php +++ b/tests/Mailgun/Tests/Mock/Connection/TestBroker.php @@ -13,10 +13,10 @@ class TestBroker extends RestClient protected $apiEndpoint; - public function __construct($apiKey = null, $apiEndpoint = "api.mailgun.net", $apiVersion = "v3") + public function __construct($apiKey = null, $apiHost = "api.mailgun.net", $apiVersion = "v3") { $this->apiKey = $apiKey; - $this->apiEndpoint = $apiEndpoint; + $this->apiEndpoint = $apiHost; } public function post($endpointUrl, $postData = array(), $files = array()) From e0c3558e82e5a88d1fe319c16a4c2d72a52ed503 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 3 Oct 2015 22:12:32 +0200 Subject: [PATCH 06/19] Added information about PHP-adapter --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 55ddfda..57ff6c8 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,15 @@ curl -sS https://getcomposer.org/installer | php # Add Mailgun as a dependency php composer.phar require mailgun/mailgun-php:~1.7.2 -``` +``` + +You do also need to choose what library to use when you are sending messages. Consult the +[php-http/adapter-implementation](https://packagist.org/providers/php-http/adapter-implementation) virtual package to +find adapters to use. Example: + +```bash +php composer.phar require php-http/guzzle6-adapter:^0.1.0 +``` **For shared hosts without SSH access, check out our [Shared Host Instructions](SharedHostInstall.md).** From 5fcbb3d37c38cd740098a0bd8618fa5b0ae009eb Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 3 Oct 2015 22:59:31 +0200 Subject: [PATCH 07/19] Use guzzle5 in development --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 091add5..48126df 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require-dev": { "php": ">=5.4.0", "phpunit/phpunit": "~4.6", - "php-http/guzzle6-adapter": "^0.1.0" + "php-http/guzzle5-adapter": "^0.1.0" }, "autoload": { "psr-0": { From ad015ce441e6879ec3f1ea883819719756d80ad0 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 3 Oct 2015 23:27:50 +0200 Subject: [PATCH 08/19] code style --- src/Mailgun/Connection/RestClient.php | 32 +++++++++------------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 75523bf..c3dec70 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -57,15 +57,17 @@ class RestClient } $headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; - $headers['Authorization'] = 'Basic '. base64_encode(sprintf("%s:%s",Api::API_USER, $this->apiKey)); + $headers['Authorization'] = 'Basic '.base64_encode(sprintf('%s:%s', Api::API_USER, $this->apiKey)); if (!empty($files)) { $body = new MultipartStream($files); $headers['Content-Type'] = 'multipart/form-data; boundary='.$body->getBoundary(); } - $request = new Request($method, $this->apiEndpoint.$uri, $headers, $body); - return $this->httpClient->sendRequest($request); + $request = new Request($method, $this->apiEndpoint.$uri, $headers, $body); + $response = $this->httpClient->sendRequest($request); + + return $this->responseHandler($response); } /** @@ -114,13 +116,7 @@ class RestClient } } - $response = $this->send('POST', $endpointUrl, - [], - [], - array_merge($postDataMultipart, $postFiles) - ); - - return $this->responseHandler($response); + return $this->send('POST', $endpointUrl, [], [], array_merge($postDataMultipart, $postFiles)); } /** @@ -136,9 +132,7 @@ class RestClient */ public function get($endpointUrl, $queryString = array()) { - $response = $this->send('GET', $endpointUrl.'?'.http_build_query($queryString)); - - return $this->responseHandler($response); + return $this->send('GET', $endpointUrl.'?'.http_build_query($queryString)); } /** @@ -153,9 +147,7 @@ class RestClient */ public function delete($endpointUrl) { - $response = $this->send('DELETE', $endpointUrl); - - return $this->responseHandler($response); + return $this->send('DELETE', $endpointUrl); } /** @@ -171,9 +163,7 @@ class RestClient */ public function put($endpointUrl, $putData) { - $response = $this->send('PUT', $endpointUrl, [], $putData); - - return $this->responseHandler($response); + return $this->send('PUT', $endpointUrl, [], $putData); } /** @@ -242,8 +232,8 @@ class RestClient /** * Prepare a file for the postBody. * - * @param string $fieldName - * @param string|array $filePath + * @param string $fieldName + * @param string|array $filePath */ protected function prepareFile($fieldName, $filePath) { From 61d03262a996cac4322dff259dea2af14c32f620 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 12 Oct 2015 19:23:58 +0200 Subject: [PATCH 09/19] 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); } /** From 4131802de27ab76aed5aef84ea6978383f53595d Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 12 Oct 2015 22:46:30 +0200 Subject: [PATCH 10/19] Changed according to Rubens suggestions --- src/Mailgun/Connection/RestClient.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 3c5f846..330c6be 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -35,32 +35,34 @@ class RestClient protected $apiEndpoint; /** - * @param string $apiKey - * @param string $apiHost - * @param string $apiVersion - * @param bool $ssl + * @param string $apiKey + * @param string $apiHost + * @param string $apiVersion + * @param bool $ssl + * @param HttpAdapter $adapter */ public function __construct($apiKey, $apiHost, $apiVersion, $ssl, HttpAdapter $adapter = null) { $this->apiKey = $apiKey; - $this->httpAdapter = $httpAdapter; + $this->httpAdapter = $adapter; $this->apiEndpoint = $this->generateEndpoint($apiHost, $apiVersion, $ssl); } /** * @param string $method * @param string $uri - * @param array $headers - * @param array $body - * @param array $files + * @param array $body + * @param array $files + * @param array $headers * * @return \stdClass + * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters */ - protected function send($method, $uri, array $headers = [], $body = [], $files = []) + protected function send($method, $uri, $body = null, $files = [], array $headers = []) { if ($this->httpAdapter === null) { $this->httpAdapter = HttpAdapterDiscovery::find(); @@ -126,7 +128,7 @@ class RestClient } } - return $this->send('POST', $endpointUrl, [], [], array_merge($postDataMultipart, $postFiles)); + return $this->send('POST', $endpointUrl, [], array_merge($postDataMultipart, $postFiles)); } /** @@ -173,7 +175,7 @@ class RestClient */ public function put($endpointUrl, $putData) { - return $this->send('PUT', $endpointUrl, [], $putData); + return $this->send('PUT', $endpointUrl, $putData); } /** From b8420451c7e6f3b3900c36e7658c2d75de3c2387 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 13 Oct 2015 08:59:43 +0200 Subject: [PATCH 11/19] Added docs --- src/Mailgun/Mailgun.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index afc4eaa..1c2f0c5 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -33,6 +33,7 @@ class Mailgun{ * @param string $apiEndpoint * @param string $apiVersion * @param bool $ssl + * @param HttpAdapter $adapter */ public function __construct( $apiKey = null, From 78f252761ea1a81361471617030bbad61166c3ba Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Fri, 30 Oct 2015 14:28:43 +0100 Subject: [PATCH 12/19] Updated the php-http lib --- composer.json | 8 ++++---- src/Mailgun/Connection/RestClient.php | 18 +++++++++--------- src/Mailgun/Mailgun.php | 7 ++++--- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 61f13a2..b7b4338 100644 --- a/composer.json +++ b/composer.json @@ -3,14 +3,14 @@ "description": "The Mailgun SDK provides methods for all API functions.", "require": { "guzzlehttp/psr7": "~1.2", - "php-http/adapter-implementation": "^0.1", - "php-http/adapter": "^0.1", - "php-http/discovery": "^0.1" + "php-http/httplug": "@dev", + "php-http/client-implementation": "@dev", + "php-http/discovery": "@dev" }, "require-dev": { "php": ">=5.4.0", "phpunit/phpunit": "~4.6", - "php-http/guzzle5-adapter": "^0.1.0" + "php-http/guzzle6-adapter": "@dev" }, "autoload": { "psr-0": { diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 330c6be..1ade48f 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -4,7 +4,7 @@ namespace Mailgun\Connection; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; -use Http\Adapter\HttpAdapter; +use Http\Client\HttpClient; use Http\Discovery\HttpAdapterDiscovery; use Mailgun\Connection\Exceptions\GenericHTTPError; use Mailgun\Connection\Exceptions\InvalidCredentials; @@ -25,9 +25,9 @@ class RestClient private $apiKey; /** - * @var HttpAdapter + * @var HttpClient */ - protected $httpAdapter; + protected $httpClient; /** * @var string @@ -39,12 +39,12 @@ class RestClient * @param string $apiHost * @param string $apiVersion * @param bool $ssl - * @param HttpAdapter $adapter + * @param HttpClient $httpClient */ - public function __construct($apiKey, $apiHost, $apiVersion, $ssl, HttpAdapter $adapter = null) + public function __construct($apiKey, $apiHost, $apiVersion, $ssl, HttpClient $httpClient = null) { $this->apiKey = $apiKey; - $this->httpAdapter = $adapter; + $this->httpClient = $httpClient; $this->apiEndpoint = $this->generateEndpoint($apiHost, $apiVersion, $ssl); } @@ -64,8 +64,8 @@ class RestClient */ protected function send($method, $uri, $body = null, $files = [], array $headers = []) { - if ($this->httpAdapter === null) { - $this->httpAdapter = HttpAdapterDiscovery::find(); + if ($this->httpClient === null) { + $this->httpClient = HttpAdapterDiscovery::find(); } $headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; @@ -77,7 +77,7 @@ class RestClient } $request = new Request($method, $this->apiEndpoint.$uri, $headers, $body); - $response = $this->httpAdapter->sendRequest($request); + $response = $this->httpClient->sendRequest($request); return $this->responseHandler($response); } diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 1c2f0c5..6f3d572 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -3,6 +3,7 @@ namespace Mailgun; use Http\Adapter\HttpAdapter; +use Http\Client\HttpClient; use Mailgun\Constants\ExceptionMessages; use Mailgun\Messages\Exceptions; use Mailgun\Connection\RestClient; @@ -33,17 +34,17 @@ class Mailgun{ * @param string $apiEndpoint * @param string $apiVersion * @param bool $ssl - * @param HttpAdapter $adapter + * @param HttpAdapter $httpClient */ public function __construct( $apiKey = null, $apiEndpoint = "api.mailgun.net", $apiVersion = "v3", $ssl = true, - HttpAdapter $adapter = null + HttpClient $httpClient = null ) { $this->apiKey = $apiKey; - $this->restClient = new RestClient($apiKey, $apiEndpoint, $apiVersion, $ssl, $adapter); + $this->restClient = new RestClient($apiKey, $apiEndpoint, $apiVersion, $ssl, $httpClient); } /** From a0edd0eb305966bad73ac90e6aaf11252be1d5bd Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Fri, 30 Oct 2015 14:36:08 +0100 Subject: [PATCH 13/19] Updates to the php-http library --- composer.json | 9 +++++---- src/Mailgun/Connection/RestClient.php | 22 +++++++++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index b7b4338..21ea5d9 100644 --- a/composer.json +++ b/composer.json @@ -2,15 +2,16 @@ "name": "mailgun/mailgun-php", "description": "The Mailgun SDK provides methods for all API functions.", "require": { + "php": ">=5.4.0", "guzzlehttp/psr7": "~1.2", - "php-http/httplug": "@dev", "php-http/client-implementation": "@dev", - "php-http/discovery": "@dev" + "php-http/discovery": "dev-master" }, "require-dev": { - "php": ">=5.4.0", "phpunit/phpunit": "~4.6", - "php-http/guzzle6-adapter": "@dev" + "php-http/guzzle6-adapter": "dev-master", + "php-http/httplug": "dev-master", + "php-http/message-factory": "dev-master" }, "autoload": { "psr-0": { diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 1ade48f..d86d709 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -5,7 +5,7 @@ namespace Mailgun\Connection; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Request; use Http\Client\HttpClient; -use Http\Discovery\HttpAdapterDiscovery; +use Http\Discovery\HttpClientDiscovery; use Mailgun\Connection\Exceptions\GenericHTTPError; use Mailgun\Connection\Exceptions\InvalidCredentials; use Mailgun\Connection\Exceptions\MissingRequiredParameters; @@ -64,9 +64,7 @@ class RestClient */ protected function send($method, $uri, $body = null, $files = [], array $headers = []) { - if ($this->httpClient === null) { - $this->httpClient = HttpAdapterDiscovery::find(); - } + $client = $this->getHttpClient(); $headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; $headers['Authorization'] = 'Basic '.base64_encode(sprintf('%s:%s', Api::API_USER, $this->apiKey)); @@ -77,7 +75,7 @@ class RestClient } $request = new Request($method, $this->apiEndpoint.$uri, $headers, $body); - $response = $this->httpClient->sendRequest($request); + $response = $client->sendRequest($request); return $this->responseHandler($response); } @@ -267,4 +265,18 @@ class RestClient 'filename' => $filename, ]; } + + + /** + * + * @return HttpClient + */ + protected function getHttpClient() + { + if ($this->httpClient === null) { + $this->httpClient = HttpClientDiscovery::find(); + } + + return $this->httpClient; + } } From 7f3d63775639ae313ee613a9ff8a907b15b57208 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 18 Nov 2015 15:46:53 +0100 Subject: [PATCH 14/19] Updated to reflect changes in PHP-HTTP --- README.md | 7 ++++--- composer.json | 5 ++--- src/Mailgun/Mailgun.php | 3 +-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 57ff6c8..caf282d 100644 --- a/README.md +++ b/README.md @@ -25,12 +25,13 @@ curl -sS https://getcomposer.org/installer | php php composer.phar require mailgun/mailgun-php:~1.7.2 ``` -You do also need to choose what library to use when you are sending messages. Consult the +You do also need to choose what library to use when you are sending http messages. Consult the [php-http/adapter-implementation](https://packagist.org/providers/php-http/adapter-implementation) virtual package to -find adapters to use. Example: +find adapters to use. For more information about virtual packages please refer to +[Httplug](http://docs.httplug.io/en/latest/virtual-package/). Example: ```bash -php composer.phar require php-http/guzzle6-adapter:^0.1.0 +php composer.phar require php-http/guzzle6-adapter:dev-master ``` **For shared hosts without SSH access, check out our [Shared Host Instructions](SharedHostInstall.md).** diff --git a/composer.json b/composer.json index 21ea5d9..41399c0 100644 --- a/composer.json +++ b/composer.json @@ -5,13 +5,12 @@ "php": ">=5.4.0", "guzzlehttp/psr7": "~1.2", "php-http/client-implementation": "@dev", - "php-http/discovery": "dev-master" + "php-http/discovery": "~0.2" }, "require-dev": { "phpunit/phpunit": "~4.6", "php-http/guzzle6-adapter": "dev-master", - "php-http/httplug": "dev-master", - "php-http/message-factory": "dev-master" + "php-http/httplug": "dev-master" }, "autoload": { "psr-0": { diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 6f3d572..cee46db 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -2,7 +2,6 @@ namespace Mailgun; -use Http\Adapter\HttpAdapter; use Http\Client\HttpClient; use Mailgun\Constants\ExceptionMessages; use Mailgun\Messages\Exceptions; @@ -34,7 +33,7 @@ class Mailgun{ * @param string $apiEndpoint * @param string $apiVersion * @param bool $ssl - * @param HttpAdapter $httpClient + * @param HttpClient $httpClient */ public function __construct( $apiKey = null, From ac0b85b9e2c9a03a71531c1bd03500ac6577be93 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 21 Dec 2015 00:24:34 +0100 Subject: [PATCH 15/19] Updated to httplug beta --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 41399c0..83ccde4 100644 --- a/composer.json +++ b/composer.json @@ -4,13 +4,13 @@ "require": { "php": ">=5.4.0", "guzzlehttp/psr7": "~1.2", - "php-http/client-implementation": "@dev", - "php-http/discovery": "~0.2" + "php-http/client-implementation": "1.0", + "php-http/discovery": "^0.4" }, "require-dev": { "phpunit/phpunit": "~4.6", - "php-http/guzzle6-adapter": "dev-master", - "php-http/httplug": "dev-master" + "php-http/guzzle5-adapter": "dev-master", + "php-http/httplug": "v1.0.0-beta" }, "autoload": { "psr-0": { From 6ea7e9325f14c78078851362ebbc542dffee07f7 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 23 Dec 2015 17:35:07 +0100 Subject: [PATCH 16/19] updated link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index caf282d..cdbfbb5 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ php composer.phar require mailgun/mailgun-php:~1.7.2 ``` You do also need to choose what library to use when you are sending http messages. Consult the -[php-http/adapter-implementation](https://packagist.org/providers/php-http/adapter-implementation) virtual package to +[php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation) virtual package to find adapters to use. For more information about virtual packages please refer to [Httplug](http://docs.httplug.io/en/latest/virtual-package/). Example: From b961cfb4eb7feefec8bebea3c36482996ca6488a Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Fri, 26 Feb 2016 12:04:54 +0100 Subject: [PATCH 17/19] Updated to latest version of Httplug and made auto discovery optional --- README.md | 13 ++++++++++++- composer.json | 8 ++++---- src/Mailgun/Connection/RestClient.php | 4 +--- src/Mailgun/Mailgun.php | 12 ++++++++++-- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index cdbfbb5..f8c7d46 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,20 @@ find adapters to use. For more information about virtual packages please refer t [Httplug](http://docs.httplug.io/en/latest/virtual-package/). Example: ```bash -php composer.phar require php-http/guzzle6-adapter:dev-master +php composer.phar require php-http/guzzle6-adapter:^1.0 ``` +When creating a new `Mailgun` object you must provide an instance of the `HttpClient`. + +```php +$client = new \Http\Adapter\Guzzle6\Client(); +$mailgun = new \Mailgun\Mailgun('api_key', null, null, true, $client); +``` + +You could also rely on the [auto discovery feature of Httplug](http://docs.php-http.org/en/latest/discovery.html). This +means that you need to install `puli/composer-plugin` and put a puli.phar in your project root. + + **For shared hosts without SSH access, check out our [Shared Host Instructions](SharedHostInstall.md).** **Rather just download the files? [Library Download](https://9f67cbbd1116d8afb399-7760483f5d1e5f28c2d253278a2a5045.ssl.cf2.rackcdn.com/mailgun-php-1.7.2.zip).** diff --git a/composer.json b/composer.json index 83ccde4..8c6fccc 100644 --- a/composer.json +++ b/composer.json @@ -4,13 +4,13 @@ "require": { "php": ">=5.4.0", "guzzlehttp/psr7": "~1.2", - "php-http/client-implementation": "1.0", - "php-http/discovery": "^0.4" + "php-http/httplug": "^1.0", + "php-http/client-implementation": "^1.0", + "php-http/discovery": "^0.8" }, "require-dev": { "phpunit/phpunit": "~4.6", - "php-http/guzzle5-adapter": "dev-master", - "php-http/httplug": "v1.0.0-beta" + "php-http/guzzle6-adapter": "^1.0" }, "autoload": { "psr-0": { diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index d86d709..e35f9da 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -64,8 +64,6 @@ class RestClient */ protected function send($method, $uri, $body = null, $files = [], array $headers = []) { - $client = $this->getHttpClient(); - $headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; $headers['Authorization'] = 'Basic '.base64_encode(sprintf('%s:%s', Api::API_USER, $this->apiKey)); @@ -75,7 +73,7 @@ class RestClient } $request = new Request($method, $this->apiEndpoint.$uri, $headers, $body); - $response = $client->sendRequest($request); + $response = $this->getHttpClient()->sendRequest($request); return $this->responseHandler($response); } diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index cee46db..2b8ebf8 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -37,11 +37,19 @@ class Mailgun{ */ public function __construct( $apiKey = null, - $apiEndpoint = "api.mailgun.net", - $apiVersion = "v3", + $apiEndpoint = null, + $apiVersion = null, $ssl = true, HttpClient $httpClient = null ) { + if (empty($apiEndpoint)) { + $apiEndpoint = "api.mailgun.net"; + } + + if (empty($apiVersion)) { + $apiVersion = "v3"; + } + $this->apiKey = $apiKey; $this->restClient = new RestClient($apiKey, $apiEndpoint, $apiVersion, $ssl, $httpClient); } From b85f1461d4de4e0ecb72618b502401ee72c3d0a8 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 5 Mar 2016 11:13:28 +0100 Subject: [PATCH 18/19] Removed unused tests and some parameters from the constructor --- README.md | 6 +- src/Mailgun/Connection/RestClient.php | 90 ++++++++++++++----- src/Mailgun/Mailgun.php | 44 +++++---- .../Tests/Connection/ConnectionTest.php | 19 ---- .../Tests/Messages/MessageBuilderTest.php | 2 +- 5 files changed, 101 insertions(+), 60 deletions(-) delete mode 100644 tests/Mailgun/Tests/Connection/ConnectionTest.php diff --git a/README.md b/README.md index f8c7d46..8972bfb 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ When creating a new `Mailgun` object you must provide an instance of the `HttpCl ```php $client = new \Http\Adapter\Guzzle6\Client(); -$mailgun = new \Mailgun\Mailgun('api_key', null, null, true, $client); +$mailgun = new \Mailgun\Mailgun('api_key', $client); ``` You could also rely on the [auto discovery feature of Httplug](http://docs.php-http.org/en/latest/discovery.html). This @@ -161,7 +161,9 @@ Go to http://bin.mailgun.net. The Postbin will generate a special URL. Save that ```php # First, instantiate the SDK with your API credentials and define your domain. -$mg = new Mailgun('key-example', 'bin.mailgun.net', 'aecf68de', $ssl = False); +$mg = new Mailgun('key-example', null, 'bin.mailgun.net'); +$mg->setApiVersion('aecf68de'); +$mg->setSslEnabled('false'); $domain = 'example.com'; # Now, compose and send your message. diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index e35f9da..1cdc2ee 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -20,6 +20,7 @@ use Psr\Http\Message\ResponseInterface; class RestClient { /** + * Your API key * @var string */ private $apiKey; @@ -32,20 +33,30 @@ class RestClient /** * @var string */ - protected $apiEndpoint; + protected $apiHost; + + /** + * The version of the API to use + * @var string + */ + protected $apiVersion = 'v2'; + + /** + * If we should use SSL or not + * @var bool + */ + protected $sslEnabled = true; /** * @param string $apiKey * @param string $apiHost - * @param string $apiVersion - * @param bool $ssl * @param HttpClient $httpClient */ - public function __construct($apiKey, $apiHost, $apiVersion, $ssl, HttpClient $httpClient = null) + public function __construct($apiKey, $apiHost, HttpClient $httpClient = null) { $this->apiKey = $apiKey; + $this->apiHost = $apiHost; $this->httpClient = $httpClient; - $this->apiEndpoint = $this->generateEndpoint($apiHost, $apiVersion, $ssl); } /** @@ -72,7 +83,7 @@ class RestClient $headers['Content-Type'] = 'multipart/form-data; boundary='.$body->getBoundary(); } - $request = new Request($method, $this->apiEndpoint.$uri, $headers, $body); + $request = new Request($method, $this->getApiUrl($uri), $headers, $body); $response = $this->getHttpClient()->sendRequest($request); return $this->responseHandler($response); @@ -221,22 +232,6 @@ class RestClient } } - /** - * @param string $apiEndpoint - * @param string $apiVersion - * @param bool $ssl - * - * @return string - */ - protected function generateEndpoint($apiEndpoint, $apiVersion, $ssl) - { - if (!$ssl) { - return 'http://'.$apiEndpoint.'/'.$apiVersion.'/'; - } else { - return 'https://'.$apiEndpoint.'/'.$apiVersion.'/'; - } - } - /** * Prepare a file for the postBody. * @@ -277,4 +272,55 @@ class RestClient return $this->httpClient; } + + /** + * @param $uri + * + * @return string + */ + private function getApiUrl($uri) + { + return $this->generateEndpoint($this->apiHost, $this->apiVersion, $this->sslEnabled).$uri; + } + + + /** + * @param string $apiEndpoint + * @param string $apiVersion + * @param bool $ssl + * + * @return string + */ + private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) + { + if (!$ssl) { + return 'http://'.$apiEndpoint.'/'.$apiVersion.'/'; + } else { + return 'https://'.$apiEndpoint.'/'.$apiVersion.'/'; + } + } + + /** + * @param string $apiVersion + * + * @return RestClient + */ + public function setApiVersion($apiVersion) + { + $this->apiVersion = $apiVersion; + + return $this; + } + + /** + * @param boolean $sslEnabled + * + * @return RestClient + */ + public function setSslEnabled($sslEnabled) + { + $this->sslEnabled = $sslEnabled; + + return $this; + } } diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 2b8ebf8..6777801 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -30,28 +30,16 @@ class Mailgun{ /** * @param string|null $apiKey - * @param string $apiEndpoint - * @param string $apiVersion - * @param bool $ssl * @param HttpClient $httpClient + * @param string $apiEndpoint */ public function __construct( $apiKey = null, - $apiEndpoint = null, - $apiVersion = null, - $ssl = true, - HttpClient $httpClient = null + HttpClient $httpClient = null, + $apiEndpoint = 'api.mailgun.net' ) { - if (empty($apiEndpoint)) { - $apiEndpoint = "api.mailgun.net"; - } - - if (empty($apiVersion)) { - $apiVersion = "v3"; - } - $this->apiKey = $apiKey; - $this->restClient = new RestClient($apiKey, $apiEndpoint, $apiVersion, $ssl, $httpClient); + $this->restClient = new RestClient($apiKey, $apiEndpoint, $httpClient); } /** @@ -148,6 +136,30 @@ class Mailgun{ return $this->restClient->put($endpointUrl, $putData); } + /** + * @param string $apiVersion + * + * @return Mailgun + */ + public function setApiVersion($apiVersion) + { + $this->restClient->setApiVersion($apiVersion); + + return $this; + } + + /** + * @param boolean $sslEnabled + * + * @return Mailgun + */ + public function setSslEnabled($sslEnabled) + { + $this->restClient->setSslEnabled($sslEnabled); + + return $this; + } + /** * @return MessageBuilder */ diff --git a/tests/Mailgun/Tests/Connection/ConnectionTest.php b/tests/Mailgun/Tests/Connection/ConnectionTest.php deleted file mode 100644 index aed26de..0000000 --- a/tests/Mailgun/Tests/Connection/ConnectionTest.php +++ /dev/null @@ -1,19 +0,0 @@ -client = new Mailgun("My-Super-Awesome-API-Key", "samples.mailgun.org", false); - } -} diff --git a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php index 4a46eea..8daf2e3 100644 --- a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php +++ b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php @@ -9,7 +9,7 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function setUp() { - $this->client = new Mailgun("My-Super-Awesome-API-Key", "samples.mailgun.org", false); + $this->client = new Mailgun(); } public function testBlankInstantiation() From 89869402e04d1ff783a99fd654ac10d8e4e4f136 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 5 Mar 2016 11:14:34 +0100 Subject: [PATCH 19/19] removed unsupported PHP versions --- .travis.yml | 1 - composer.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 79178e0..fadbf8c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.4 - 5.5 - 5.6 - 7.0 diff --git a/composer.json b/composer.json index 8c6fccc..6e8618b 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "mailgun/mailgun-php", "description": "The Mailgun SDK provides methods for all API functions.", "require": { - "php": ">=5.4.0", + "php": "^5.5|^7.0", "guzzlehttp/psr7": "~1.2", "php-http/httplug": "^1.0", "php-http/client-implementation": "^1.0",