Skip to content

Commit

Permalink
add multi-region support
Browse files Browse the repository at this point in the history
  • Loading branch information
lanlin committed Mar 19, 2021
1 parent 07c8e73 commit 27b87d5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use Nylas\Client;
$options =
[
'debug' => true,
'region' => 'oregon', // server region, can be oregon, ireland or canada, default is oregon
'log_file' => dirname(__FILE__) . '/test.log', // a file path or a resource handler
'account_id' => 'your account id',
'access_token' => 'your access token',
Expand Down
3 changes: 2 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @method Webhooks\Abs Webhooks()
*
* @author lanlin
* @change 2020/06/28
* @change 2021/03/18
*/
class Client
{
Expand All @@ -44,6 +44,7 @@ class Client
* @param array $options
* [
* 'debug' => bool,
* 'region' => 'oregon',
* 'log_file' => 'log file path',
* 'account_id' => '',
* 'access_token' => '',
Expand Down
6 changes: 3 additions & 3 deletions src/Request/AbsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* ----------------------------------------------------------------------------------
*
* @author lanlin
* @change 2020/09/30
* @change 2021/03/18
*/
trait AbsBase
{
Expand Down Expand Up @@ -50,12 +50,12 @@ trait AbsBase
* @param string|NULL $server
* @param bool|resource $debug
*/
public function __construct(string $server = null, $debug = false)
public function __construct(?string $server = null, $debug = false)
{
$option =
[
'verify' => true,
'base_uri' => trim($server ?? API::LIST['server']),
'base_uri' => trim($server ?? API::SERVER['oregon']),
];

$this->debug = $debug;
Expand Down
15 changes: 12 additions & 3 deletions src/Utilities/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,28 @@
* @version 2.1 (2020/09/30)
*
* @author lanlin
* @change 2020/09/30
* @change 2021/03/18
*/
class API
{
// ------------------------------------------------------------------------------

/**
* nylas server list array
*/
public const SERVER = [
'oregon' => 'https://api.nylas.com',
'canada' => 'https://canada.api.nylas.com',
'ireland' => 'https://ireland.api.nylas.com',
];

// ------------------------------------------------------------------------------

/**
* nylas api list array
*/
public const LIST =
[
'server' => 'https://api.nylas.com',

// Authentication
'oAuthToken' => '/oauth/token',
'oAuthRevoke' => '/oauth/revoke',
Expand Down
37 changes: 28 additions & 9 deletions src/Utilities/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* ----------------------------------------------------------------------------------
*
* @author lanlin
* @change 2020/06/18
* @change 2021/03/18
*/
class Options
{
Expand All @@ -29,6 +29,11 @@ class Options
*/
private bool $debug = false;

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

/**
* @var string
*/
Expand Down Expand Up @@ -65,6 +70,7 @@ public function __construct(array $options)
{
$rules = V::keySet(
V::key('debug', V::boolType(), false),
V::key('region', V::in(['oregon', 'canada', 'ireland']), false),
V::key('log_file', $this->getLogFileRule(), false),
V::key('account_id', V::stringType()->notEmpty(), false),
V::key('access_token', V::stringType()->notEmpty(), false),
Expand All @@ -79,6 +85,7 @@ public function __construct(array $options)

// optional
$this->setDebug($options['debug'] ?? false);
$this->setServer($options['region'] ?? 'oregon');
$this->setLogFile($options['log_file'] ?? null);
$this->setAccountId($options['account_id'] ?? '');
$this->setAccessToken($options['access_token'] ?? '');
Expand Down Expand Up @@ -141,14 +148,26 @@ public function getAccountId(): ?string

// ------------------------------------------------------------------------------

/**
* @param null|string $region
*/
public function setServer(?string $region = null): void
{
$region = $region ?? 'oregon';

$this->server = API::SERVER[$region] ?? API::SERVER['oregon'];
}

// ------------------------------------------------------------------------------

/**
* get server
*
* @return string
*/
public function getServer(): string
{
return API::LIST['server'];
return $this->server;
}

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -221,13 +240,13 @@ public function getAllOptions(): array
{
return
[
'debug' => $this->debug,
'log_file' => $this->logFile,
'server' => API::LIST['server'],
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'account_id' => $this->accountId,
'access_token' => $this->accessToken,
'debug' => $this->debug,
'log_file' => $this->logFile,
'server' => $this->server,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'account_id' => $this->accountId,
'access_token' => $this->accessToken,
];
}

Expand Down
3 changes: 2 additions & 1 deletion tests/AbsCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* ----------------------------------------------------------------------------------
*
* @update lanlin
* @change 2020/06/27
* @change 2021/03/18
*
* @internal
*/
Expand All @@ -36,6 +36,7 @@ public function setUp(): void
$options =
[
'debug' => true,
'region' => 'oregon',
'log_file' => __DIR__.'/test.log',
'account_id' => 'your account id',
'access_token' => 'your access token',
Expand Down

0 comments on commit 27b87d5

Please sign in to comment.