Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Messages api #247

Merged
merged 3 commits into from Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Mailgun/Api/Domain.php
Expand Up @@ -49,7 +49,7 @@ public function index($limit = 100, $skip = 0)

$response = $this->httpGet('/v3/domains', $params);

return $this->deserializer->deserialize($response, IndexResponse::class);
return $this->safeDeserialize($response, IndexResponse::class);
}

/**
Expand All @@ -65,7 +65,7 @@ public function show($domain)

$response = $this->httpGet(sprintf('/v3/domains/%s', $domain));

return $this->deserializer->deserialize($response, ShowResponse::class);
return $this->safeDeserialize($response, ShowResponse::class);
}

/**
Expand Down Expand Up @@ -114,7 +114,7 @@ public function delete($domain)

$response = $this->httpDelete(sprintf('/v3/domains/%s', $domain));

return $this->deserializer->deserialize($response, DeleteResponse::class);
return $this->safeDeserialize($response, DeleteResponse::class);
}

/**
Expand Down Expand Up @@ -165,7 +165,7 @@ public function createCredential($domain, $login, $password)

$response = $this->httpPost(sprintf('/v3/domains/%s/credentials', $domain), $params);

return $this->deserializer->deserialize($response, CreateCredentialResponse::class);
return $this->safeDeserialize($response, CreateCredentialResponse::class);
}

/**
Expand All @@ -190,7 +190,7 @@ public function updateCredential($domain, $login, $pass)

$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);
}

/**
Expand All @@ -214,7 +214,7 @@ public function deleteCredential($domain, $login)
)
);

return $this->deserializer->deserialize($response, DeleteCredentialResponse::class);
return $this->safeDeserialize($response, DeleteCredentialResponse::class);
}

/**
Expand All @@ -230,7 +230,7 @@ public function connection($domain)

$response = $this->httpGet(sprintf('/v3/domains/%s/connection', $domain));

return $this->deserializer->deserialize($response, ConnectionResponse::class);
return $this->safeDeserialize($response, ConnectionResponse::class);
}

/**
Expand Down Expand Up @@ -261,6 +261,6 @@ public function updateConnection($domain, $requireTLS, $noVerify)

$response = $this->httpPut(sprintf('/v3/domains/%s/connection', $domain), $params);

return $this->deserializer->deserialize($response, UpdateConnectionResponse::class);
return $this->safeDeserialize($response, UpdateConnectionResponse::class);
}
}
138 changes: 138 additions & 0 deletions src/Mailgun/Api/Message.php
@@ -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,
];
}
}
4 changes: 2 additions & 2 deletions src/Mailgun/Api/Stats.php
Expand Up @@ -32,7 +32,7 @@ public function total($domain, array $params = [])

$response = $this->httpGet(sprintf('/v3/%s/stats/total', rawurlencode($domain)), $params);

return $this->deserializer->deserialize($response, TotalResponse::class);
return $this->safeDeserialize($response, TotalResponse::class);
}

/**
Expand All @@ -47,6 +47,6 @@ public function all($domain, array $params = [])

$response = $this->httpGet(sprintf('/v3/%s/stats', rawurlencode($domain)), $params);

return $this->deserializer->deserialize($response, AllResponse::class);
return $this->safeDeserialize($response, AllResponse::class);
}
}
10 changes: 5 additions & 5 deletions src/Mailgun/Api/Webhook.php
Expand Up @@ -31,7 +31,7 @@ public function index($domain)
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);
}

/**
Expand All @@ -46,7 +46,7 @@ public function show($domain, $webhook)
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);
}

/**
Expand All @@ -69,7 +69,7 @@ public function create($domain, $id, $url)

$response = $this->httpPost(sprintf('/v3/domains/%s/webhooks', $domain), $params);

return $this->deserializer->deserialize($response, CreateResponse::class);
return $this->safeDeserialize($response, CreateResponse::class);
}

/**
Expand All @@ -91,7 +91,7 @@ public function update($domain, $id, $url)

$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);
}

/**
Expand All @@ -107,6 +107,6 @@ public function delete($domain, $id)

$response = $this->httpDelete(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id));

return $this->deserializer->deserialize($response, DeleteResponse::class);
return $this->safeDeserialize($response, DeleteResponse::class);
}
}
5 changes: 3 additions & 2 deletions src/Mailgun/Deserializer/ModelDeserializer.php
Expand Up @@ -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);
Expand Down
18 changes: 17 additions & 1 deletion src/Mailgun/Mailgun.php
Expand Up @@ -272,8 +272,24 @@ public function domains()
/**
* @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);
}
}
74 changes: 74 additions & 0 deletions src/Mailgun/Resource/Api/Message/SendResponse.php
@@ -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;
}
}