Rename serializer to deserializer (#201) (#216)

This commit is contained in:
Sean Johnson 2016-11-11 19:50:47 -06:00 committed by Tobias Nyholm
parent 20b9fbfe5d
commit 0da5f410a9
17 changed files with 185 additions and 85 deletions

View File

@ -10,15 +10,15 @@
namespace Mailgun\Api;
use Mailgun\Assert;
use Mailgun\Resource\Api\Domain\ConnectionResponse;
use Mailgun\Resource\Api\Domain\CreateCredentialResponse;
use Mailgun\Resource\Api\Domain\CreateResponse;
use Mailgun\Resource\Api\Domain\CredentialResponse;
use Mailgun\Resource\Api\Domain\DeleteCredentialResponse;
use Mailgun\Resource\Api\Domain\DeleteResponse;
use Mailgun\Resource\Api\Domain\ShowResponse;
use Mailgun\Resource\Api\Domain\CredentialResponse;
use Mailgun\Resource\Api\Domain\ConnectionResponse;
use Mailgun\Resource\Api\Domain\UpdateConnectionResponse;
use Mailgun\Resource\Api\Domain\IndexResponse;
use Mailgun\Resource\Api\Domain\ShowResponse;
use Mailgun\Resource\Api\Domain\UpdateConnectionResponse;
use Mailgun\Resource\Api\Domain\UpdateCredentialResponse;
use Psr\Http\Message\ResponseInterface;
@ -49,7 +49,7 @@ class Domain extends HttpApi
$response = $this->httpGet('/v3/domains', $params);
return $this->serializer->deserialize($response, IndexResponse::class);
return $this->deserializer->deserialize($response, IndexResponse::class);
}
/**
@ -65,7 +65,7 @@ class Domain extends HttpApi
$response = $this->httpGet(sprintf('/v3/domains/%s', $domain));
return $this->serializer->deserialize($response, ShowResponse::class);
return $this->deserializer->deserialize($response, ShowResponse::class);
}
/**
@ -95,7 +95,7 @@ class Domain extends HttpApi
'wildcard' => $wildcard,
];
$response = $this->postMultipart('/v3/domains', $params);
$response = $this->httpPostMultipart('/v3/domains', $params);
return $this->safeDeserialize($response, CreateResponse::class);
}
@ -114,7 +114,7 @@ class Domain extends HttpApi
$response = $this->httpDelete(sprintf('/v3/domains/%s', $domain));
return $this->serializer->deserialize($response, DeleteResponse::class);
return $this->deserializer->deserialize($response, DeleteResponse::class);
}
/**
@ -163,9 +163,9 @@ class Domain extends HttpApi
'password' => $password,
];
$response = $this->postMultipart(sprintf('/v3/domains/%s/credentials', $domain), $params);
$response = $this->httpPostMultipart(sprintf('/v3/domains/%s/credentials', $domain), $params);
return $this->serializer->deserialize($response, CreateCredentialResponse::class);
return $this->deserializer->deserialize($response, CreateCredentialResponse::class);
}
/**
@ -188,7 +188,7 @@ class Domain extends HttpApi
'password' => $pass,
];
$response = $this->putMultipart(
$response = $this->httpPutMultipart(
sprintf(
'/v3/domains/%s/credentials/%s',
$domain,
@ -197,7 +197,7 @@ class Domain extends HttpApi
$params
);
return $this->serializer->deserialize($response, UpdateCredentialResponse::class);
return $this->deserializer->deserialize($response, UpdateCredentialResponse::class);
}
/**
@ -213,7 +213,7 @@ class Domain extends HttpApi
Assert::stringNotEmpty($domain);
Assert::stringNotEmpty($login);
$response = $this->delete(
$response = $this->httpDelete(
sprintf(
'/v3/domains/%s/credentials/%s',
$domain,
@ -221,7 +221,7 @@ class Domain extends HttpApi
)
);
return $this->serializer->deserialize($response, DeleteCredentialResponse::class);
return $this->deserializer->deserialize($response, DeleteCredentialResponse::class);
}
/**
@ -237,7 +237,7 @@ class Domain extends HttpApi
$response = $this->httpGet(sprintf('/v3/domains/%s/connection', $domain));
return $this->serializer->deserialize($response, ConnectionResponse::class);
return $this->deserializer->deserialize($response, ConnectionResponse::class);
}
/**
@ -266,8 +266,8 @@ class Domain extends HttpApi
$params['skip_verification'] = $noVerify ? 'true' : 'false';
}
$response = $this->putMultipart(sprintf('/v3/domains/%s/connection', $domain), $params);
$response = $this->httpPutMultipart(sprintf('/v3/domains/%s/connection', $domain), $params);
return $this->serializer->deserialize($response, UpdateConnectionResponse::class);
return $this->deserializer->deserialize($response, UpdateConnectionResponse::class);
}
}

View File

@ -3,16 +3,16 @@
namespace Mailgun\Api;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\HttpClient;
use Http\Client\Exception as HttplugException;
use Http\Client\HttpClient;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
use Http\Message\RequestFactory;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Http\Message\RequestFactory;
use Mailgun\Assert;
use Mailgun\Deserializer\ResponseDeserializer;
use Mailgun\Exception\HttpServerException;
use Mailgun\Serializer\ResponseDeserializer;
use Mailgun\Resource\Api\SimpleResponse;
use Mailgun\Resource\Api\ErrorResponse;
use Psr\Http\Message\ResponseInterface;
/**
@ -37,10 +37,10 @@ abstract class HttpApi
* @param RequestFactory $requestFactory
* @param ResponseDeserializer $serializer
*/
public function __construct(HttpClient $httpClient, RequestFactory $requestFactory, ResponseDeserializer $serializer)
public function __construct(HttpClient $httpClient, RequestFactory $requestFactory, ResponseDeserializer $deserializer)
{
$this->httpClient = new HttpMethodsClient($httpClient, $requestFactory);
$this->serializer = $serializer;
$this->deserializer = $deserializer;
}
/**
@ -56,9 +56,9 @@ abstract class HttpApi
protected function safeDeserialize(ResponseInterface $response, $className)
{
if ($response->getStatusCode() !== 200) {
return $this->serializer->deserialize($response, SimpleResponse::class);
return $this->deserializer->deserialize($response, ErrorResponse::class);
} else {
return $this->serializer->deserialize($response, $className);
return $this->deserializer->deserialize($response, $className);
}
}
@ -109,7 +109,7 @@ abstract class HttpApi
*
* @return ResponseInterface
*/
protected function postMultipart($path, array $parameters = [], array $requestHeaders = [])
protected function httpPostMultipart($path, array $parameters = [], array $requestHeaders = [])
{
return $this->doMultipart('POST', $path, $parameters, $requestHeaders);
}
@ -163,7 +163,7 @@ abstract class HttpApi
*
* @return ResponseInterface
*/
protected function putMultipart($path, array $parameters = [], array $requestHeaders = [])
protected function httpPutMultipart($path, array $parameters = [], array $requestHeaders = [])
{
return $this->doMultipart('PUT', $path, $parameters, $requestHeaders);
}
@ -197,7 +197,7 @@ abstract class HttpApi
*
* @return ResponseInterface
*/
protected function deleteMultipart($path, array $parameters = [], array $requestHeaders = [])
protected function httpDeleteMultipart($path, array $parameters = [], array $requestHeaders = [])
{
return $this->doMultipart('DELETE', $path, $parameters, $requestHeaders);
}

View File

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

View File

@ -1,8 +1,8 @@
<?php
namespace Mailgun\Serializer;
namespace Mailgun\Deserializer;
use Mailgun\Exception\SerializeException;
use Mailgun\Exception\DeserializeException;
use Psr\Http\Message\ResponseInterface;
/**
@ -10,7 +10,7 @@ use Psr\Http\Message\ResponseInterface;
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ArraySerializer implements ResponseDeserializer
class ArrayDeserializer implements ResponseDeserializer
{
/**
* @param ResponseInterface $response
@ -22,12 +22,12 @@ class ArraySerializer implements ResponseDeserializer
{
$body = $response->getBody()->__toString();
if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
throw new SerializeException('The ArraySerializer cannot deserialize response with Content-Type:'.$response->getHeaderLine('Content-Type'));
throw new DeserializeException('The ArrayDeserializer cannot deserialize response with Content-Type:'.$response->getHeaderLine('Content-Type'));
}
$content = json_decode($body, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new SerializeException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
throw new DeserializeException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
}
return $content;

View File

@ -1,8 +1,8 @@
<?php
namespace Mailgun\Serializer;
namespace Mailgun\Deserializer;
use Mailgun\Exception\SerializeException;
use Mailgun\Exception\DeserializeException;
use Mailgun\Resource\ApiResponse;
use Psr\Http\Message\ResponseInterface;
@ -11,7 +11,7 @@ use Psr\Http\Message\ResponseInterface;
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ModelSerializer implements ResponseDeserializer
class ModelDeserializer implements ResponseDeserializer
{
/**
* @param ResponseInterface $response
@ -23,12 +23,12 @@ class ModelSerializer implements ResponseDeserializer
{
$body = $response->getBody()->__toString();
if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
throw new SerializeException('The ModelSerializer cannot deserialize response with Content-Type:'.$response->getHeaderLine('Content-Type'));
throw new DeserializeException('The ModelDeserializer cannot deserialize response with Content-Type:'.$response->getHeaderLine('Content-Type'));
}
$data = json_decode($body, true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new SerializeException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
throw new DeserializeException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
}
if (is_subclass_of($class, ApiResponse::class)) {

View File

@ -1,6 +1,6 @@
<?php
namespace Mailgun\Serializer;
namespace Mailgun\Deserializer;
use Psr\Http\Message\ResponseInterface;
@ -9,7 +9,7 @@ use Psr\Http\Message\ResponseInterface;
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class PSR7Serializer implements ResponseDeserializer
class PSR7Deserializer implements ResponseDeserializer
{
/**
* @param ResponseInterface $response

View File

@ -1,6 +1,6 @@
<?php
namespace Mailgun\Serializer;
namespace Mailgun\Deserializer;
use Psr\Http\Message\ResponseInterface;

View File

@ -0,0 +1,9 @@
<?php
namespace Mailgun\Exception;
use Mailgun\Exception;
class DeserializeException extends \RuntimeException implements Exception
{
}

View File

@ -1,9 +0,0 @@
<?php
namespace Mailgun\Exception;
use Mailgun\Exception;
class SerializeException extends \RuntimeException implements Exception
{
}

View File

@ -19,8 +19,8 @@ use Mailgun\Lists\OptInHandler;
use Mailgun\Messages\BatchMessage;
use Mailgun\Messages\Exceptions;
use Mailgun\Messages\MessageBuilder;
use Mailgun\Serializer\ModelSerializer;
use Mailgun\Serializer\ResponseDeserializer;
use Mailgun\Deserializer\ModelDeserializer;
use Mailgun\Deserializer\ResponseDeserializer;
/**
* This class is the base class for the Mailgun SDK.
@ -47,7 +47,7 @@ class Mailgun
/**
* @var ResponseDeserializer
*/
private $serializer;
private $deserializer;
/**
* @var RequestFactory
@ -58,14 +58,14 @@ class Mailgun
* @param string|null $apiKey
* @param HttpClient|null $httpClient
* @param string $apiEndpoint
* @param ResponseDeserializer|null $serializer
* @param ResponseDeserializer|null $deserializer
* @param HttpClientConfigurator|null $clientConfigurator
*/
public function __construct(
$apiKey = null,
HttpClient $httpClient = null, /* Deprecated, will be removed in 3.0 */
$apiEndpoint = 'api.mailgun.net', /* Deprecated, will be removed in 3.0 */
ResponseDeserializer $serializer = null,
ResponseDeserializer $deserializer = null,
HttpClientConfigurator $clientConfigurator = null
) {
$this->apiKey = $apiKey;
@ -89,7 +89,7 @@ class Mailgun
$this->httpClient = $clientConfigurator->createConfiguredClient();
$this->requestFactory = MessageFactoryDiscovery::find();
$this->serializer = $serializer ?: new ModelSerializer();
$this->deserializer = $deserializer ?: new ModelDeserializer();
}
/**
@ -258,7 +258,7 @@ class Mailgun
*/
public function getStatsApi()
{
return new Api\Stats($this->httpClient, $this->requestFactory, $this->serializer);
return new Api\Stats($this->httpClient, $this->requestFactory, $this->deserializer);
}
/**
@ -266,6 +266,6 @@ class Mailgun
*/
public function getDomainApi()
{
return new Api\Domain($this->httpClient, $this->requestFactory, $this->serializer);
return new Api\Domain($this->httpClient, $this->requestFactory, $this->deserializer);
}
}

View File

@ -11,6 +11,7 @@ namespace Mailgun\Resource\Api\Domain;
use Mailgun\Assert;
use Mailgun\Resource\ApiResponse;
use Mailgun\Resource\Api\ErrorResponse;
/**
* @author Sean Johnson <sean@mailgun.com>
@ -34,16 +35,17 @@ final class CredentialResponse implements ApiResponse
*/
public static function create(array $data)
{
$items = [];
if (array_key_exists('items', $data) && array_key_exists('total_count', $data)) {
$items = [];
Assert::keyExists($data, 'total_count');
Assert::keyExists($data, 'items');
foreach ($data['items'] as $item) {
$items[] = CredentialResponseItem::create($item);
}
foreach ($data['items'] as $item) {
$items[] = CredentialResponseItem::create($item);
return new self($data['total_count'], $items);
} else {
return ErrorResponse::create($data);
}
return new self($data['total_count'], $items);
}
/**
@ -54,7 +56,7 @@ final class CredentialResponse implements ApiResponse
{
Assert::integer($totalCount);
Assert::isArray($items);
Assert::allIsInstanceOf($items, 'Mailgun\Resource\Api\Domain\Credential');
Assert::allIsInstanceOf($items, 'Mailgun\Resource\Api\Domain\CredentialResponseItem');
$this->totalCount = $totalCount;
$this->items = $items;

View File

@ -14,6 +14,11 @@ final class DeleteCredentialResponse implements ApiResponse
*/
private $message;
/**
* @var error
*/
private $error;
/**
* @var string
*/
@ -22,9 +27,10 @@ final class DeleteCredentialResponse implements ApiResponse
/**
* @param string $message
*/
private function __construct($message, $spec)
private function __construct($message, $error, $spec)
{
$this->message = $message;
$this->error = $error;
$this->spec = $spec;
}
@ -35,7 +41,11 @@ final class DeleteCredentialResponse implements ApiResponse
*/
public static function create(array $data)
{
return new self($data['message'], $data['spec']);
return new self(
isset($data['message']) ? $data['message'] : null,
isset($data['error']) ? $data['error'] : null,
isset($data['spec']) ? $data['spec'] : null
);
}
/**
@ -46,6 +56,14 @@ final class DeleteCredentialResponse implements ApiResponse
return $this->message;
}
/**
* @return string
*/
public function getError()
{
return $this->error;
}
/**
* @return string
*/

View File

@ -14,12 +14,18 @@ final class DeleteResponse implements ApiResponse
*/
private $message;
/**
* @var string
*/
private $error;
/**
* @param string $message
*/
private function __construct($message)
private function __construct($message, $error)
{
$this->message = $message;
$this->error = $error;
}
/**
@ -29,7 +35,10 @@ final class DeleteResponse implements ApiResponse
*/
public static function create(array $data)
{
return new self($data['message']);
return new self(
isset($data['message']) ? $data['message'] : null,
isset($data['error']) ? $data['error'] : null
);
}
/**
@ -39,4 +48,12 @@ final class DeleteResponse implements ApiResponse
{
return $this->message;
}
/**
* @return string
*/
public function getError()
{
return $this->error;
}
}

View File

@ -54,7 +54,7 @@ final class IndexResponse implements ApiResponse
{
Assert::integer($totalCount);
Assert::isArray($items);
Assert::allIsInstanceOf($items, 'Mailgun\Resource\Api\Domain\SimpleDomain');
Assert::allIsInstanceOf($items, 'Mailgun\Resource\Api\Domain\Domain');
$this->totalCount = $totalCount;
$this->items = $items;

View File

@ -0,0 +1,62 @@
<?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;
use Mailgun\Resource\ApiResponse;
final class ErrorResponse implements ApiResponse
{
/**
* @var string
*/
private $message;
/**
* @var string
*/
private $error;
/**
* @param string $error
*/
private function __construct($message, $error)
{
$this->message = $message;
$this->error = $error;
}
/**
* @param array data
*
* @return self
*/
public static function create(array $data)
{
return new self(
isset($data['message']) ? $data['message'] : null,
isset($data['error']) ? $data['error'] : null
);
}
/**
* @return string
*/
public function getMessage()
{
return $this->message;
}
/**
* @return string
*/
public function getError()
{
return $this->error;
}
}

View File

@ -51,7 +51,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
->setMethods(['createRequest', 'createResponse'])
->getMock();
$serializer = $this->getMockBuilder('Mailgun\Serializer\ResponseDeserializer')
$deserializer = $this->getMockBuilder('Mailgun\Deserializer\ResponseDeserializer')
->setMethods(['deserialize'])
->getMock();
@ -64,7 +64,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
'httPut', 'putMultipart',
]
)
->setConstructorArgs([$httpClient, $requestClient, $serializer])
->setConstructorArgs([$httpClient, $requestClient, $deserializer])
->getMock();
}

View File

@ -10,16 +10,17 @@
namespace Mailgun\Tests\Integration;
use Mailgun\Api\Domain;
use Mailgun\Resource\Api\ErrorResponse;
use Mailgun\Resource\Api\Domain\CreateCredentialResponse;
use Mailgun\Resource\Api\Domain\CreateResponse;
use Mailgun\Resource\Api\Domain\DeleteCredentialResponse;
use Mailgun\Resource\Api\Domain\DeleteResponse;
use Mailgun\Resource\Api\Domain\UpdateCredentialResponse;
use Mailgun\Tests\Api\TestCase;
use Mailgun\Resource\Api\Domain\Domain as DomainObject;
use Mailgun\Resource\Api\Domain\CredentialResponseItem;
use Mailgun\Resource\Api\Domain\CredentialResponse;
use Mailgun\Resource\Api\Domain\ConnectionResponse;
use Mailgun\Resource\Api\Domain\UpdateConnectionResponse;
use Mailgun\Resource\Api\Domain\UpdateCredentialResponse;
use Mailgun\Tests\Api\TestCase;
/**
* @author Sean Johnson <sean@mailgun.com>
@ -28,7 +29,7 @@ class DomainApiTest extends TestCase
{
protected function getApiClass()
{
return 'Mailgun\Api\Domains';
return 'Mailgun\Api\Domain';
}
/**
@ -47,7 +48,7 @@ class DomainApiTest extends TestCase
}
}
$this->assertContainsOnlyInstancesOf(Domain::class, $domainList->getDomains());
$this->assertContainsOnlyInstancesOf(DomainObject::class, $domainList->getDomains());
$this->assertTrue($found);
}
@ -70,7 +71,7 @@ class DomainApiTest extends TestCase
/**
* Performs `DELETE /v3/domains/<domain>` on a non-existent domain.
*/
public function testRemoveDomain_NoExist()
public function testRemoveDomainNoExist()
{
$mg = $this->getMailgunClient();
@ -104,7 +105,7 @@ class DomainApiTest extends TestCase
* Performs `POST /v3/domains` to attempt to create a domain with duplicate
* values.
*/
public function testDomainCreate_DuplicateValues()
public function testDomainCreateDuplicateValues()
{
$mg = $this->getMailgunClient();
@ -115,7 +116,7 @@ class DomainApiTest extends TestCase
false // wildcard domain?
);
$this->assertNotNull($domain);
$this->assertInstanceOf(CreateResponse::class, $domain);
$this->assertInstanceOf(ErrorResponse::class, $domain);
$this->assertEquals('This domain name is already taken', $domain->getMessage());
}
@ -146,7 +147,7 @@ class DomainApiTest extends TestCase
'Password.01!'
);
$this->assertNotNull($ret);
$this->assertInstanceOf(CreateResponse::class, $ret);
$this->assertInstanceOf(CreateCredentialResponse::class, $ret);
$this->assertEquals('Created 1 credentials pair(s)', $ret->getMessage());
}
@ -220,7 +221,7 @@ class DomainApiTest extends TestCase
$ret = $mg->getDomainApi()->credentials('mailgun.org');
$this->assertNotNull($ret);
$this->assertInstanceOf(CredentialResponse::class, $ret);
$this->assertInstanceOf(ErrorResponse::class, $ret);
$this->assertEquals('Domain not found: mailgun.org', $ret->getMessage());
}