#276 additional method to get an attachment (#277)

* #276 additional method to get an attachment

* #276 fix code style

* Add Mailgun::getAttachment + test

* code style fix
This commit is contained in:
Sergei Beregov 2017-02-28 09:10:04 +02:00 committed by Tobias Nyholm
parent 74929b6518
commit 718547a273
3 changed files with 48 additions and 0 deletions

View File

@ -108,6 +108,26 @@ class RestClient
return $this->responseHandler($response); return $this->responseHandler($response);
} }
/**
* @param string $url
*
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*
* @return \stdClass
*/
public function getAttachment($url)
{
$headers['User-Agent'] = Api::SDK_USER_AGENT.'/'.Api::SDK_VERSION;
$headers['Authorization'] = 'Basic '.base64_encode(sprintf('%s:%s', Api::API_USER, $this->apiKey));
$request = MessageFactoryDiscovery::find()->createRequest('get', $url, $headers);
$response = HttpClientDiscovery::find()->sendRequest($request);
return $this->responseHandler($response);
}
/** /**
* @param string $endpointUrl * @param string $endpointUrl
* @param array $postData * @param array $postData

View File

@ -179,6 +179,16 @@ class Mailgun
return $this->restClient->get($endpointUrl, $queryString); return $this->restClient->get($endpointUrl, $queryString);
} }
/**
* @param string $url
*
* @return \stdClass
*/
public function getAttachment($url)
{
return $this->restClient->getAttachment($url);
}
/** /**
* @param string $endpointUrl * @param string $endpointUrl
* *

View File

@ -49,4 +49,22 @@ class MailgunTest extends \Mailgun\Tests\MailgunTestCase
$postData = []; $postData = [];
assert(!$client->verifyWebhookSignature($postData)); assert(!$client->verifyWebhookSignature($postData));
} }
public function testGetAttachmentOk()
{
$attachmentUrl = 'http://example.com';
$client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
$response = $client->getAttachment($attachmentUrl);
$this->assertInstanceOf('stdClass', $response);
$this->assertEquals($response->http_response_code, 200);
}
public function testGetAttachmentFail()
{
$this->setExpectedException('\\Mailgun\\Connection\\Exceptions\\GenericHTTPError');
$attachmentUrl = 'https://api.mailgun.net/non.existing.uri/1/2/3';
$client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
$client->getAttachment($attachmentUrl);
}
} }