Make sure we can get the last response from the API again. (#305)

* Make sure we can get the last response from the API again.

* cs
This commit is contained in:
Tobias Nyholm 2017-03-26 10:17:10 +02:00 committed by GitHub
parent 0f4fe2bf9c
commit 64d1c40604
3 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,45 @@
<?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\HttpClient\Plugin;
use Http\Client\Common\Plugin\Journal;
use Http\Client\Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* A plugin to remember the last response.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class History implements Journal
{
/**
* @var ResponseInterface
*/
private $lastResponse;
/**
* @return ResponseInterface|null
*/
public function getLastResponse()
{
return $this->lastResponse;
}
public function addSuccess(RequestInterface $request, ResponseInterface $response)
{
$this->lastResponse = $response;
}
public function addFailure(RequestInterface $request, Exception $exception)
{
}
}

View File

@ -15,6 +15,7 @@ use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\UriFactoryDiscovery; use Http\Discovery\UriFactoryDiscovery;
use Http\Message\UriFactory; use Http\Message\UriFactory;
use Http\Client\Common\Plugin; use Http\Client\Common\Plugin;
use Mailgun\HttpClient\Plugin\History;
/** /**
* Configure a HTTP client. * Configure a HTTP client.
@ -43,6 +44,16 @@ class HttpClientConfigurator
*/ */
private $httpClient; private $httpClient;
/**
* @var History
*/
private $responseHistory;
public function __construct()
{
$this->responseHistory = new History();
}
/** /**
* @return PluginClient * @return PluginClient
*/ */
@ -54,6 +65,7 @@ class HttpClientConfigurator
'User-Agent' => 'mailgun-sdk-php/v2 (https://github.com/mailgun/mailgun-php)', 'User-Agent' => 'mailgun-sdk-php/v2 (https://github.com/mailgun/mailgun-php)',
'Authorization' => 'Basic '.base64_encode(sprintf('api:%s', $this->getApiKey())), 'Authorization' => 'Basic '.base64_encode(sprintf('api:%s', $this->getApiKey())),
]), ]),
new Plugin\HistoryPlugin($this->responseHistory),
]; ];
return new PluginClient($this->getHttpClient(), $plugins); return new PluginClient($this->getHttpClient(), $plugins);
@ -146,4 +158,12 @@ class HttpClientConfigurator
return $this; return $this;
} }
/**
* @return History
*/
public function getResponseHistory()
{
return $this->responseHistory;
}
} }

View File

@ -13,12 +13,14 @@ use Http\Client\Common\HttpMethodsClient;
use Http\Client\HttpClient; use Http\Client\HttpClient;
use Mailgun\Connection\RestClient; use Mailgun\Connection\RestClient;
use Mailgun\Constants\ExceptionMessages; use Mailgun\Constants\ExceptionMessages;
use Mailgun\HttpClient\Plugin\History;
use Mailgun\Lists\OptInHandler; use Mailgun\Lists\OptInHandler;
use Mailgun\Messages\BatchMessage; use Mailgun\Messages\BatchMessage;
use Mailgun\Messages\Exceptions; use Mailgun\Messages\Exceptions;
use Mailgun\Messages\MessageBuilder; use Mailgun\Messages\MessageBuilder;
use Mailgun\Hydrator\ModelHydrator; use Mailgun\Hydrator\ModelHydrator;
use Mailgun\Hydrator\Hydrator; use Mailgun\Hydrator\Hydrator;
use Psr\Http\Message\ResponseInterface;
/** /**
* This class is the base class for the Mailgun SDK. * This class is the base class for the Mailgun SDK.
@ -52,6 +54,13 @@ class Mailgun
*/ */
private $requestBuilder; private $requestBuilder;
/**
* This is a object that holds the last response from the API.
*
* @var History
*/
private $responseHistory = null;
/** /**
* @param string|null $apiKey * @param string|null $apiKey
* @param HttpClient|null $httpClient * @param HttpClient|null $httpClient
@ -189,6 +198,14 @@ class Mailgun
} }
} }
/**
* @return ResponseInterface|null
*/
public function getLastResponse()
{
return $this->responseHistory->getLastResponse();
}
/** /**
* @param string $endpointUrl * @param string $endpointUrl
* @param array $postData * @param array $postData