Messages api (#247)

* Added message API

* Still use json

* code style
This commit is contained in:
Tobias Nyholm 2016-12-07 23:29:08 +01:00 committed by Sean Johnson
parent b1f949a925
commit cc82355e50
8 changed files with 643 additions and 18 deletions

View File

@ -49,7 +49,7 @@ class Domain extends HttpApi
$response = $this->httpGet('/v3/domains', $params);
return $this->deserializer->deserialize($response, IndexResponse::class);
return $this->safeDeserialize($response, IndexResponse::class);
}
/**
@ -65,7 +65,7 @@ class Domain extends HttpApi
$response = $this->httpGet(sprintf('/v3/domains/%s', $domain));
return $this->deserializer->deserialize($response, ShowResponse::class);
return $this->safeDeserialize($response, ShowResponse::class);
}
/**
@ -114,7 +114,7 @@ class Domain extends HttpApi
$response = $this->httpDelete(sprintf('/v3/domains/%s', $domain));
return $this->deserializer->deserialize($response, DeleteResponse::class);
return $this->safeDeserialize($response, DeleteResponse::class);
}
/**
@ -165,7 +165,7 @@ class Domain extends HttpApi
$response = $this->httpPost(sprintf('/v3/domains/%s/credentials', $domain), $params);
return $this->deserializer->deserialize($response, CreateCredentialResponse::class);
return $this->safeDeserialize($response, CreateCredentialResponse::class);
}
/**
@ -190,7 +190,7 @@ class Domain extends HttpApi
$response = $this->httpPut(sprintf('/v3/domains/%s/credentials/%s', $domain, $login), $params);
return $this->deserializer->deserialize($response, UpdateCredentialResponse::class);
return $this->safeDeserialize($response, UpdateCredentialResponse::class);
}
/**
@ -214,7 +214,7 @@ class Domain extends HttpApi
)
);
return $this->deserializer->deserialize($response, DeleteCredentialResponse::class);
return $this->safeDeserialize($response, DeleteCredentialResponse::class);
}
/**
@ -230,7 +230,7 @@ class Domain extends HttpApi
$response = $this->httpGet(sprintf('/v3/domains/%s/connection', $domain));
return $this->deserializer->deserialize($response, ConnectionResponse::class);
return $this->safeDeserialize($response, ConnectionResponse::class);
}
/**
@ -261,6 +261,6 @@ class Domain extends HttpApi
$response = $this->httpPut(sprintf('/v3/domains/%s/connection', $domain), $params);
return $this->deserializer->deserialize($response, UpdateConnectionResponse::class);
return $this->safeDeserialize($response, UpdateConnectionResponse::class);
}
}

138
src/Mailgun/Api/Message.php Normal file
View File

@ -0,0 +1,138 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Api;
use Mailgun\Assert;
use Mailgun\Exception\InvalidArgumentException;
use Mailgun\Resource\Api\Message\SendResponse;
use Mailgun\Resource\Api\Message\ShowResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class Message extends HttpApi
{
/**
* @param $domain
* @param array $params
*
* @return SendResponse
*/
public function send($domain, array $params)
{
Assert::notEmpty($domain);
Assert::notEmpty($params);
$postDataMultipart = [];
$fields = ['message', 'attachment', 'inline'];
foreach ($fields as $fieldName) {
if (!isset($params[$fieldName])) {
continue;
}
if (!is_array($params[$fieldName])) {
$postDataMultipart[] = $this->prepareFile($fieldName, $params[$fieldName]);
} else {
$fileIndex = 0;
foreach ($params[$fieldName] as $file) {
$postDataMultipart[] = $this->prepareFile($fieldName, $file, $fileIndex);
++$fileIndex;
}
}
unset($params[$fieldName]);
}
foreach ($params as $key => $value) {
if (is_array($value)) {
$index = 0;
foreach ($value as $subValue) {
$postDataMultipart[] = [
'name' => sprintf('%s[%d]', $key, $index++),
'content' => $subValue,
];
}
} else {
$postDataMultipart[] = [
'name' => $key,
'content' => $value,
];
}
}
$response = $this->httpPostRaw(sprintf('/v3/%s/messages', $domain), $postDataMultipart);
return $this->safeDeserialize($response, SendResponse::class);
}
/**
* Get stored message.
*
* @param string $url
* @param bool $rawMessage if true we will use "Accept: message/rfc2822" header.
*
* @return ShowResponse
*/
public function show($url, $rawMessage = false)
{
Assert::notEmpty($url);
$headers = [];
if ($rawMessage) {
$headers['Accept'] = 'message/rfc2822';
}
$response = $this->httpGet($url, [], $headers);
return $this->safeDeserialize($response, ShowResponse::class);
}
/**
* Prepare a file.
*
* @param string $fieldName
* @param array $filePath array('fileContent' => 'content') or array('filePath' => '/foo/bar')
* @param int $fileIndex
*
* @return array
*
* @throws InvalidArgumentException
*/
private function prepareFile($fieldName, array $filePath, $fileIndex = 0)
{
// Add index for multiple file support
$fieldName .= '['.$fileIndex.']';
$filename = isset($filePath['filename']) ? $filePath['filename'] : null;
if (isset($filePath['fileContent'])) {
// File from memory
$resource = fopen('php://temp', 'r+');
fwrite($resource, $filePath['fileContent']);
rewind($resource);
} elseif (isset($filePath['filePath'])) {
// File form path
$path = $filePath['filePath'];
// Remove leading @ symbol
if (strpos($path, '@') === 0) {
$path = substr($path, 1);
}
$resource = fopen($path, 'r');
} else {
throw new InvalidArgumentException('When using a file you need to specify parameter "fileContent" or "filePath"');
}
return [
'name' => $fieldName,
'content' => $resource,
'filename' => $filename,
];
}
}

View File

@ -32,7 +32,7 @@ class Stats extends HttpApi
$response = $this->httpGet(sprintf('/v3/%s/stats/total', rawurlencode($domain)), $params);
return $this->deserializer->deserialize($response, TotalResponse::class);
return $this->safeDeserialize($response, TotalResponse::class);
}
/**
@ -47,6 +47,6 @@ class Stats extends HttpApi
$response = $this->httpGet(sprintf('/v3/%s/stats', rawurlencode($domain)), $params);
return $this->deserializer->deserialize($response, AllResponse::class);
return $this->safeDeserialize($response, AllResponse::class);
}
}

View File

@ -31,7 +31,7 @@ class Webhook extends HttpApi
Assert::notEmpty($domain);
$response = $this->httpGet(sprintf('/v3/domains/%s/webhooks', $domain));
return $this->deserializer->deserialize($response, IndexResponse::class);
return $this->safeDeserialize($response, IndexResponse::class);
}
/**
@ -46,7 +46,7 @@ class Webhook extends HttpApi
Assert::notEmpty($webhook);
$response = $this->httpGet(sprintf('/v3/domains/%s/webhooks/%s', $domain, $webhook));
return $this->deserializer->deserialize($response, ShowResponse::class);
return $this->safeDeserialize($response, ShowResponse::class);
}
/**
@ -69,7 +69,7 @@ class Webhook extends HttpApi
$response = $this->httpPost(sprintf('/v3/domains/%s/webhooks', $domain), $params);
return $this->deserializer->deserialize($response, CreateResponse::class);
return $this->safeDeserialize($response, CreateResponse::class);
}
/**
@ -91,7 +91,7 @@ class Webhook extends HttpApi
$response = $this->httpPut(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id), $params);
return $this->deserializer->deserialize($response, UpdateResponse::class);
return $this->safeDeserialize($response, UpdateResponse::class);
}
/**
@ -107,6 +107,6 @@ class Webhook extends HttpApi
$response = $this->httpDelete(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id));
return $this->deserializer->deserialize($response, DeleteResponse::class);
return $this->safeDeserialize($response, DeleteResponse::class);
}
}

View File

@ -29,8 +29,9 @@ class ModelDeserializer implements ResponseDeserializer
public function deserialize(ResponseInterface $response, $class)
{
$body = $response->getBody()->__toString();
if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
throw new DeserializeException('The ModelDeserializer cannot deserialize response with Content-Type:'.$response->getHeaderLine('Content-Type'));
$contentType = $response->getHeaderLine('Content-Type');
if (strpos($contentType, 'application/json') !== 0 && strpos($contentType, 'application/octet-stream') !== 0) {
throw new DeserializeException('The ModelDeserializer cannot deserialize response with Content-Type: '.$contentType);
}
$data = json_decode($body, true);

View File

@ -272,8 +272,24 @@ class Mailgun
/**
* @return Api\Event
*/
public function event()
public function events()
{
return new Api\Event($this->httpClient, $this->requestBuilder, $this->deserializer);
}
/**
* @return Api\Webhook
*/
public function webhooks()
{
return new Api\Webhook($this->httpClient, $this->requestBuilder, $this->deserializer);
}
/**
* @return Api\Message
*/
public function messages()
{
return new Api\Message($this->httpClient, $this->requestBuilder, $this->deserializer);
}
}

View File

@ -0,0 +1,74 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Message;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class SendResponse implements ApiResponse
{
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $message;
/**
* @param string $id
* @param string $message
*/
private function __construct($id, $message)
{
$this->id = $id;
$this->message = $message;
}
/**
* @param array $data
*
* @return SendResponse
*/
public static function create(array $data)
{
$id = '';
$message = '';
if (isset($data['id'])) {
$id = $data['id'];
}
if (isset($data['message'])) {
$message = $data['message'];
}
return new self($id, $message);
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
}

View File

@ -0,0 +1,396 @@
<?php
/*
* Copyright (C) 2013-2016 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Resource\Api\Message;
use Mailgun\Resource\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ShowResponse implements ApiResponse
{
/**
* Only available with message/rfc2822.
*
* @var string
*/
private $recipient;
/**
* Only available with message/rfc2822.
*
* @var string
*/
private $bodyMime;
/**
* @var string
*/
private $recipients;
/**
* @var string
*/
private $sender;
/**
* @var string
*/
private $from;
/**
* @var string
*/
private $subject;
/**
* @var string
*/
private $bodyPlain;
/**
* @var string
*/
private $strippedText;
/**
* @var string
*/
private $strippedSignature;
/**
* @var string
*/
private $bodyHtml;
/**
* @var string
*/
private $strippedHtml;
/**
* @var array
*/
private $attachments;
/**
* @var string
*/
private $messageUrl;
/**
* @var string
*/
private $contentIdMap;
/**
* @var array
*/
private $messageHeaders;
/**
* Do not let this object be creted without the ::create.
*/
private function __construct()
{
}
/**
* @param array $data
*
* @return SendResponse
*/
public static function create(array $data)
{
$response = new self();
if (isset($data['recipients'])) {
$response->setRecipients($data['recipients']);
}
if (isset($data['sender'])) {
$response->setSender($data['sender']);
}
if (isset($data['from'])) {
$response->setFrom($data['from']);
}
if (isset($data['subject'])) {
$response->setSubject($data['subject']);
}
if (isset($data['body-plain'])) {
$response->setBodyPlain($data['body-plain']);
}
if (isset($data['stripped-text'])) {
$response->setStrippedText($data['stripped-text']);
}
if (isset($data['stripped-signature'])) {
$response->setStrippedSignature($data['stripped-signature']);
}
if (isset($data['body-html'])) {
$response->setBodyHtml($data['body-html']);
}
if (isset($data['stripped-html'])) {
$response->setStrippedHtml($data['stripped-html']);
}
if (isset($data['message-url'])) {
$response->setMessageUrl($data['message-url']);
}
if (isset($data['message-headers'])) {
$response->setMessageHeaders($data['message-headers']);
}
if (isset($data['recipient'])) {
$response->setRecipient($data['recipient']);
}
if (isset($data['body-mime'])) {
$response->setBodyMime($data['body-mime']);
}
return $response;
}
/**
* @return string
*/
public function getRecipient()
{
return $this->recipient;
}
/**
* @param string $recipient
*/
private function setRecipient($recipient)
{
$this->recipient = $recipient;
}
/**
* @return string
*/
public function getBodyMime()
{
return $this->bodyMime;
}
/**
* @param string $bodyMime
*/
private function setBodyMime($bodyMime)
{
$this->bodyMime = $bodyMime;
}
/**
* @return string
*/
public function getRecipients()
{
return $this->recipients;
}
/**
* @param string $recipients
*/
private function setRecipients($recipients)
{
$this->recipients = $recipients;
}
/**
* @return string
*/
public function getSender()
{
return $this->sender;
}
/**
* @param string $sender
*/
private function setSender($sender)
{
$this->sender = $sender;
}
/**
* @return string
*/
public function getFrom()
{
return $this->from;
}
/**
* @param string $from
*/
private function setFrom($from)
{
$this->from = $from;
}
/**
* @return string
*/
public function getSubject()
{
return $this->subject;
}
/**
* @param string $subject
*/
private function setSubject($subject)
{
$this->subject = $subject;
}
/**
* @return string
*/
public function getBodyPlain()
{
return $this->bodyPlain;
}
/**
* @param string $bodyPlain
*/
private function setBodyPlain($bodyPlain)
{
$this->bodyPlain = $bodyPlain;
}
/**
* @return string
*/
public function getStrippedText()
{
return $this->strippedText;
}
/**
* @param string $strippedText
*/
private function setStrippedText($strippedText)
{
$this->strippedText = $strippedText;
}
/**
* @return string
*/
public function getStrippedSignature()
{
return $this->strippedSignature;
}
/**
* @param string $strippedSignature
*/
private function setStrippedSignature($strippedSignature)
{
$this->strippedSignature = $strippedSignature;
}
/**
* @return string
*/
public function getBodyHtml()
{
return $this->bodyHtml;
}
/**
* @param string $bodyHtml
*/
private function setBodyHtml($bodyHtml)
{
$this->bodyHtml = $bodyHtml;
}
/**
* @return string
*/
public function getStrippedHtml()
{
return $this->strippedHtml;
}
/**
* @param string $strippedHtml
*/
private function setStrippedHtml($strippedHtml)
{
$this->strippedHtml = $strippedHtml;
}
/**
* @return array
*/
public function getAttachments()
{
return $this->attachments;
}
/**
* @param array $attachments
*/
private function setAttachments(array $attachments)
{
$this->attachments = $attachments;
}
/**
* @return string
*/
public function getMessageUrl()
{
return $this->messageUrl;
}
/**
* @param string $messageUrl
*/
private function setMessageUrl($messageUrl)
{
$this->messageUrl = $messageUrl;
}
/**
* @return string
*/
public function getContentIdMap()
{
return $this->contentIdMap;
}
/**
* @param string $contentIdMap
*/
private function setContentIdMap($contentIdMap)
{
$this->contentIdMap = $contentIdMap;
}
/**
* @return array
*/
public function getMessageHeaders()
{
return $this->messageHeaders;
}
/**
* @param array $messageHeaders
*/
private function setMessageHeaders(array $messageHeaders)
{
$this->messageHeaders = $messageHeaders;
}
}