From 718547a273a46e7be76d210c1278c0782aa87363 Mon Sep 17 00:00:00 2001 From: Sergei Beregov Date: Tue, 28 Feb 2017 09:10:04 +0200 Subject: [PATCH] #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 --- src/Mailgun/Connection/RestClient.php | 20 ++++++++++++++++++++ src/Mailgun/Mailgun.php | 10 ++++++++++ tests/MailgunTest.php | 18 ++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 1c18d12..bc44e15 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -108,6 +108,26 @@ class RestClient 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 array $postData diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 7d8161c..2fae224 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -179,6 +179,16 @@ class Mailgun return $this->restClient->get($endpointUrl, $queryString); } + /** + * @param string $url + * + * @return \stdClass + */ + public function getAttachment($url) + { + return $this->restClient->getAttachment($url); + } + /** * @param string $endpointUrl * diff --git a/tests/MailgunTest.php b/tests/MailgunTest.php index 5e3ad19..23d4a1e 100644 --- a/tests/MailgunTest.php +++ b/tests/MailgunTest.php @@ -49,4 +49,22 @@ class MailgunTest extends \Mailgun\Tests\MailgunTestCase $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); + } }