make sure we can attach files

This commit is contained in:
Tobias Nyholm 2015-10-03 22:08:55 +02:00
parent b2dd642235
commit 5ec7aabfe2
2 changed files with 17 additions and 18 deletions

View File

@ -2,7 +2,8 @@
"name": "mailgun/mailgun-php", "name": "mailgun/mailgun-php",
"description": "The Mailgun SDK provides methods for all API functions.", "description": "The Mailgun SDK provides methods for all API functions.",
"require": { "require": {
"happyr/http-auto-discovery": "dev-master" "guzzlehttp/psr7": "~1.2",
"happyr/http-auto-discovery": "0.1.*"
}, },
"require-dev": { "require-dev": {
"php": ">=5.4.0", "php": ">=5.4.0",

View File

@ -2,6 +2,8 @@
namespace Mailgun\Connection; namespace Mailgun\Connection;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Request;
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;
@ -41,17 +43,6 @@ class RestClient
{ {
$this->apiKey = $apiKey; $this->apiKey = $apiKey;
$this->apiEndpoint = $this->generateEndpoint($apiHost, $apiVersion, $ssl); $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 * @return HttpClient
*/ */
protected function send($method, $uri, array $headers = [], $body = null) protected function send($method, $uri, array $headers = [], $body = [], $files = [])
{ {
if ($this->httpClient === null) { if ($this->httpClient === null) {
$this->httpClient = new HttpClient(); $this->httpClient = new HttpClient();
} }
$headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION; $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()) public function post($endpointUrl, $postData = array(), $files = array())
{ {
$request = new Request('post', $endpointUrl);
$postFiles = []; $postFiles = [];
$fields = ['message', 'attachment', 'inline']; $fields = ['message', 'attachment', 'inline'];
@ -118,9 +114,11 @@ class RestClient
} }
} }
$response = $this->send('POST', $endpointUrl, [ $response = $this->send('POST', $endpointUrl,
'multipart' => array_merge($postDataMultipart, $postFiles), [],
]); [],
array_merge($postDataMultipart, $postFiles)
);
return $this->responseHandler($response); return $this->responseHandler($response);
} }