Completly removed depenency on Guzzle and made discovery easier

This commit is contained in:
Tobias Nyholm 2016-07-19 15:15:09 +02:00
parent b8a0abc075
commit 2cc6ee8c77
2 changed files with 12 additions and 8 deletions

View File

@ -3,9 +3,9 @@
"description": "The Mailgun SDK provides methods for all API functions.", "description": "The Mailgun SDK provides methods for all API functions.",
"require": { "require": {
"php": "^5.5|^7.0", "php": "^5.5|^7.0",
"guzzlehttp/psr7": "~1.2",
"php-http/httplug": "^1.0", "php-http/httplug": "^1.0",
"php-http/discovery": "^0.8" "php-http/multipart-stream-builder": "^0.1",
"php-http/discovery": "^1.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "~4.6", "phpunit/phpunit": "~4.6",

View File

@ -2,10 +2,10 @@
namespace Mailgun\Connection; namespace Mailgun\Connection;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Request;
use Http\Client\HttpClient; use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery; use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Mailgun\Connection\Exceptions\GenericHTTPError; use Mailgun\Connection\Exceptions\GenericHTTPError;
use Mailgun\Connection\Exceptions\InvalidCredentials; use Mailgun\Connection\Exceptions\InvalidCredentials;
use Mailgun\Connection\Exceptions\MissingRequiredParameters; use Mailgun\Connection\Exceptions\MissingRequiredParameters;
@ -15,7 +15,7 @@ use Mailgun\Constants\ExceptionMessages;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
/** /**
* This class is a wrapper for the Guzzle (HTTP Client Library). * This class is a wrapper for the HTTP client.
*/ */
class RestClient class RestClient
{ {
@ -79,14 +79,18 @@ class RestClient
$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)) { if (!empty($files)) {
$body = new MultipartStream($files); $builder = new MultipartStreamBuilder();
$headers['Content-Type'] = 'multipart/form-data; boundary='.$body->getBoundary(); foreach ($files as $file) {
$builder->addResource($file['name'], $file['contents'], $file);
}
$body = $builder->build();
$headers['Content-Type'] = 'multipart/form-data; boundary='.$builder->getBoundary();
} elseif (is_array($body)) { } elseif (is_array($body)) {
$body = http_build_query($body); $body = http_build_query($body);
$headers['Content-Type'] = 'application/x-www-form-urlencoded'; $headers['Content-Type'] = 'application/x-www-form-urlencoded';
} }
$request = new Request($method, $this->getApiUrl($uri), $headers, $body); $request = MessageFactoryDiscovery::find()->createRequest($method, $this->getApiUrl($uri), $headers, $body);
$response = $this->getHttpClient()->sendRequest($request); $response = $this->getHttpClient()->sendRequest($request);
return $this->responseHandler($response); return $this->responseHandler($response);