Added docs of how to debug (#315)

* Added debug options

* Added docs

* cs
This commit is contained in:
Tobias Nyholm 2017-03-26 16:12:17 +02:00 committed by GitHub
parent 7c83da9246
commit 9e19f12a3d
3 changed files with 77 additions and 14 deletions

View File

@ -125,20 +125,22 @@ Go to http://bin.mailgun.net. The Postbin will generate a special URL. Save that
**Step 2 - Instantiate the Mailgun client using Postbin.**
*Tip: The bin id will be the URL part after bin.mailgun.net. It will be random generated letters and numbers. For example, the bin id in this URL, http://bin.mailgun.net/aecf68de, is "aecf68de".*
*Tip: The bin id will be the URL part after bin.mailgun.net. It will be random generated letters and numbers.
For example, the bin id in this URL, http://bin.mailgun.net/aecf68de, is "aecf68de".*
```php
# First, instantiate the SDK with your API credentials and define your domain.
$mg = new Mailgun('key-example', null, 'bin.mailgun.net');
$mg->setApiVersion('aecf68de');
$mg->setSslEnabled(false);
$domain = 'example.com';
$configurator = new HttpClientConfigurator();
$configurator->setEndpoint('http://bin.mailgun.net/aecf68de');
$configurator->setDebug(true);
$mg = Mailgun::configure($configurator);
# Now, compose and send your message.
$mg->sendMessage($domain, array('from' => 'bob@example.com',
'to' => 'sally@example.com',
'subject' => 'The PHP SDK is awesome!',
'text' => 'It is so simple to send a message.'));
$mg->sendMessage('example.com', [
'from' => 'bob@example.com',
'to' => 'sally@example.com',
'subject' => 'The PHP SDK is awesome!',
'text' => 'It is so simple to send a message.']
);
```
### Additional Info

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;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
/**
* Replaces a URI with a new one. Good for debugging.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class ReplaceUriPlugin implements Plugin
{
/**
* @var UriInterface
*/
private $uri;
/**
* @param UriInterface $uri
*/
public function __construct(UriInterface $uri)
{
$this->uri = $uri;
}
/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$request = $request->withUri($this->uri);
return $next($request);
}
}

View File

@ -16,6 +16,7 @@ use Http\Discovery\UriFactoryDiscovery;
use Http\Message\UriFactory;
use Http\Client\Common\Plugin;
use Mailgun\HttpClient\Plugin\History;
use Mailgun\HttpClient\Plugin\ReplaceUriPlugin;
/**
* Configure a HTTP client.
@ -29,6 +30,13 @@ final class HttpClientConfigurator
*/
private $endpoint = 'https://api.mailgun.net';
/**
* If debug is true we will send all the request to the endpoint without appending any path.
*
* @var bool
*/
private $debug = false;
/**
* @var string
*/
@ -60,7 +68,7 @@ final class HttpClientConfigurator
public function createConfiguredClient()
{
$plugins = [
new Plugin\AddHostPlugin($this->getUriFactory()->createUri($this->getEndpoint())),
new Plugin\AddHostPlugin($this->getUriFactory()->createUri($this->endpoint)),
new Plugin\HeaderDefaultsPlugin([
'User-Agent' => 'mailgun-sdk-php/v2 (https://github.com/mailgun/mailgun-php)',
'Authorization' => 'Basic '.base64_encode(sprintf('api:%s', $this->getApiKey())),
@ -68,15 +76,23 @@ final class HttpClientConfigurator
new Plugin\HistoryPlugin($this->responseHistory),
];
if ($this->debug) {
$plugins[] = new ReplaceUriPlugin($this->getUriFactory()->createUri($this->endpoint));
}
return new PluginClient($this->getHttpClient(), $plugins);
}
/**
* @return string
* @param bool $debug
*
* @return HttpClientConfigurator
*/
private function getEndpoint()
public function setDebug($debug)
{
return $this->endpoint;
$this->debug = $debug;
return $this;
}
/**