Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Support for API token authentication. Username and password can still be used
as a fallback.
### Changed
- Deprecate `getConfig()` method. Packages can maintain their config internally.

## [5.1.1] - 2021-08-10
### Fixed
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ $ composer require maxemail/api-php
```php
// Instantiate Client:
$config = [
'username' => 'api@user.com',
'password' => 'apipass'
'token' => 'apitoken',
];
$api = new \Maxemail\Api\Client($config);

Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
<!-- Configuration to enable functional tests -->
<env name="FUNC_ENABLED" value="false" />
<env name="FUNC_API_URI" value="https://mxm.xtremepush.com/" />
<env name="FUNC_API_USERNAME" value="api@user.com" />
<env name="FUNC_API_PASSWORD" value="apipass" />
<env name="FUNC_API_TOKEN" value="apitoken" />
</php>

<testsuites>
Expand Down
77 changes: 57 additions & 20 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Maxemail\Api;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use GuzzleHttp\HandlerStack;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -65,6 +66,11 @@ class Client implements \Psr\Log\LoggerAwareInterface
*/
private $uri = 'https://mxm.xtremepush.com/';

/**
* @var string
*/
private $token;

/**
* @var string
*/
Expand All @@ -91,7 +97,7 @@ class Client implements \Psr\Log\LoggerAwareInterface
private $logger;

/**
* @var GuzzleClient
* @var GuzzleClientInterface
*/
private $httpClient;

Expand All @@ -100,6 +106,11 @@ class Client implements \Psr\Log\LoggerAwareInterface
*/
private $debugLoggingEnabled = false;

/**
* @var \Closure(array):GuzzleClientInterface
*/
private $httpClientFactory;

/**
* @param array $config {
* @var string $username Required
Expand All @@ -112,20 +123,25 @@ class Client implements \Psr\Log\LoggerAwareInterface
*/
public function __construct(array $config)
{
// Support deprecated key names from v3
if (!isset($config['username']) && isset($config['user'])) {
$config['username'] = $config['user'];
}
if (!isset($config['password']) && isset($config['pass'])) {
$config['password'] = $config['pass'];
}
// Must have API token
if (!isset($config['token'])) {
// Support deprecated key names from v3
if (!isset($config['username']) && isset($config['user'])) {
$config['username'] = $config['user'];
}
if (!isset($config['password']) && isset($config['pass'])) {
$config['password'] = $config['pass'];
}

// Must have user/pass
if (!isset($config['username']) || !isset($config['password'])) {
throw new Exception\InvalidArgumentException('API config requires username & password');
// Must have user/pass
if (!isset($config['username']) || !isset($config['password'])) {
throw new Exception\InvalidArgumentException('API config requires token OR username & password');
}
$this->username = $config['username'];
$this->password = $config['password'];
} else {
$this->token = $config['token'];
}
$this->username = $config['username'];
$this->password = $config['password'];

if (isset($config['uri'])) {
$parsed = parse_url($config['uri']);
Expand Down Expand Up @@ -157,7 +173,7 @@ private function getInstance(string $serviceName): Service
return $this->services[$serviceName];
}

private function getClient(): GuzzleClient
private function getClient(): GuzzleClientInterface
{
if ($this->httpClient === null) {
$stack = HandlerStack::create();
Expand All @@ -166,19 +182,31 @@ private function getClient(): GuzzleClient
if ($this->debugLoggingEnabled) {
Middleware::addLogging($stack, $this->getLogger());
}
$this->httpClient = new GuzzleClient([

$clientConfig = [
'base_uri' => $this->uri . 'api/json/',
'auth' => [
$this->username,
$this->password,
],
'headers' => [
'User-Agent' => 'MxmApiClient/' . self::VERSION . ' PHP/' . PHP_VERSION,
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/json',
],
'handler' => $stack,
]);
];

if (isset($this->token)) {
$clientConfig['headers']['Authorization'] = 'Bearer ' . $this->token;
} else {
$clientConfig['auth'] = [
$this->username,
$this->password,
];
}

if (!isset($this->httpClientFactory)) {
$this->httpClient = new GuzzleClient($clientConfig);
} else {
$this->httpClient = ($this->httpClientFactory)($clientConfig);
}
}

return $this->httpClient;
Expand All @@ -187,6 +215,7 @@ private function getClient(): GuzzleClient
/**
* Get API connection config
*
* @deprecated v5.2 No replacement; packages can maintain their own config; to be removed in v7.
* @return array {
* @var string $uri
* @var string $username
Expand Down Expand Up @@ -224,4 +253,12 @@ public function getLogger(): LoggerInterface

return $this->logger;
}

/**
* @internal This method is not part of the BC promise. Used for DI for unit tests only.
*/
public function setHttpClientFactory(\Closure $httpClientFactory): void
{
$this->httpClientFactory = $httpClientFactory;
}
}
2 changes: 1 addition & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Maxemail\Api;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface as GuzzleClient;
use Psr\Log\LogLevel;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Maxemail\Api;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface as GuzzleClient;

/**
* Maxemail API Client
Expand Down
Loading