From 389b972ec97e7c6497860ef3d0688249cf6fb9c5 Mon Sep 17 00:00:00 2001 From: German Lena Date: Thu, 10 Mar 2016 12:02:24 -0300 Subject: [PATCH 1/2] added a way to send guzzle options + changed base_uri to guzzle options --- src/API/Blacklists.php | 8 +------ src/API/Clients.php | 8 +------ src/API/Connections.php | 8 +------ src/API/DeviceCredentials.php | 8 +------ src/API/Emails.php | 8 +------ src/API/GenericResource.php | 17 +++++++++++++++ src/API/Helpers/ApiClient.php | 7 ++++-- src/API/Helpers/RequestBuilder.php | 18 ++++++++++++++-- src/API/Jobs.php | 8 +------ src/API/Rules.php | 8 +------ src/API/Stats.php | 8 +------ src/API/Tenants.php | 8 +------ src/API/Tickets.php | 8 +------ src/API/UserBlocks.php | 8 +------ src/API/Users.php | 8 +------ src/Auth0Api.php | 7 ++++-- tests/BasicCrudTest.php | 7 ++++++ tests/BlacklistsTest.php | 2 ++ tests/ClientsTest.php | 2 ++ tests/ConnectionsTest.php | 2 ++ tests/RequestBuilderTest.php | 34 ++++++++++++++++++++++++++---- tests/RulesTest.php | 2 ++ tests/UsersTest.php | 4 +++- 23 files changed, 103 insertions(+), 95 deletions(-) create mode 100644 src/API/GenericResource.php diff --git a/src/API/Blacklists.php b/src/API/Blacklists.php index 2b0b1326..aa28c52d 100644 --- a/src/API/Blacklists.php +++ b/src/API/Blacklists.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class Blacklists { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class Blacklists extends GenericResource { public function getAll($aud) { diff --git a/src/API/Clients.php b/src/API/Clients.php index c05c2e98..2bc9b6c6 100644 --- a/src/API/Clients.php +++ b/src/API/Clients.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class Clients { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class Clients extends GenericResource { public function getAll($fields = null, $include_fields = null) { diff --git a/src/API/Connections.php b/src/API/Connections.php index 0aa338b4..c56f4670 100644 --- a/src/API/Connections.php +++ b/src/API/Connections.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class Connections { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class Connections extends GenericResource { public function getAll($strategy = null, $fields = null, $include_fields = null) { diff --git a/src/API/DeviceCredentials.php b/src/API/DeviceCredentials.php index 7e33c45d..d5890492 100644 --- a/src/API/DeviceCredentials.php +++ b/src/API/DeviceCredentials.php @@ -5,17 +5,11 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class DeviceCredentials { +class DeviceCredentials extends GenericResource { const TYPE_PUBLIC_KEY = 'public_key'; const TYPE_REFESH_TOKEN = 'refresh_token'; - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } - public function getAll($user_id = null, $client_id = null, $type = null, $fields = null, $include_fields = null) { $request = $this->apiClient->get() diff --git a/src/API/Emails.php b/src/API/Emails.php index 29c421a1..3fdad3eb 100644 --- a/src/API/Emails.php +++ b/src/API/Emails.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class Emails { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class Emails extends GenericResource { public function getEmailProvider($fields = null, $include_fields = null) { diff --git a/src/API/GenericResource.php b/src/API/GenericResource.php new file mode 100644 index 00000000..b99dc4f3 --- /dev/null +++ b/src/API/GenericResource.php @@ -0,0 +1,17 @@ +apiClient = $apiClient; + } + + public function getApiClient() { + return $this->apiClient; + } + +} \ No newline at end of file diff --git a/src/API/Helpers/ApiClient.php b/src/API/Helpers/ApiClient.php index aeedb8fc..b13519e2 100644 --- a/src/API/Helpers/ApiClient.php +++ b/src/API/Helpers/ApiClient.php @@ -42,11 +42,13 @@ public static function disableInfoHeaders(){ protected $domain; protected $basePath; protected $headers; + protected $guzzleOptions; public function __construct($config) { $this->basePath = $config['basePath']; $this->domain = $config['domain']; - $this->headers = isset($config['headers']) ? $config['headers'] : array(); + $this->headers = isset($config['headers']) ? $config['headers'] : []; + $this->guzzleOptions = isset($config['guzzleOptions']) ? $config['guzzleOptions'] : []; if (self::$infoHeadersDataEnabled) { $this->headers[] = new Header('Auth0-Client', self::getInfoHeadersData()->build()); @@ -56,8 +58,9 @@ public function __construct($config) { public function __call($name, $arguments) { $builder = new RequestBuilder(array( 'domain' => $this->domain, + 'basePath' => $this->basePath, 'method' => $name, - 'path' => array( $this->basePath ), + 'guzzleOptions' => $this->guzzleOptions )); return $builder->withHeaders($this->headers); diff --git a/src/API/Helpers/RequestBuilder.php b/src/API/Helpers/RequestBuilder.php index 34a793f4..de629782 100644 --- a/src/API/Helpers/RequestBuilder.php +++ b/src/API/Helpers/RequestBuilder.php @@ -13,18 +13,24 @@ class RequestBuilder { + protected $domain; + protected $basePath; + protected $path = []; protected $method = []; protected $headers = []; protected $params = []; protected $form_params = []; protected $files = []; + protected $guzzleOptions = []; protected $body; public function __construct( $config ) { $this->method = $config['method']; $this->domain = $config['domain']; + $this->basePath = isset($config['basePath']) ? $config['basePath'] : ''; + $this->guzzleOptions = isset($config['guzzleOptions']) ? $config['guzzleOptions'] : []; $this->headers = isset($config['headers']) ? $config['headers'] : array(); if (array_key_exists('path', $config)) $this->path = $config['path']; @@ -57,7 +63,7 @@ public function addPathVariable($variable) { } public function getUrl() { - return $this->domain . '/' . trim(implode('/',$this->path), '/') . $this->getParams(); + return trim(implode('/',$this->path), '/') . $this->getParams(); } public function getParams() { @@ -96,8 +102,16 @@ public function addFormParam($key, $value) { return $this; } + public function getGuzzleOptions() { + return array_merge( + ["base_uri" => $this->domain . $this->basePath], + $this->guzzleOptions + ); + } + public function call() { - $client = new Client(); + + $client = new Client( $this->getGuzzleOptions() ); try { diff --git a/src/API/Jobs.php b/src/API/Jobs.php index 19970971..cfd4fa31 100644 --- a/src/API/Jobs.php +++ b/src/API/Jobs.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class Jobs { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class Jobs extends GenericResource { public function get($id) { diff --git a/src/API/Rules.php b/src/API/Rules.php index 2a6b8def..91498bb6 100644 --- a/src/API/Rules.php +++ b/src/API/Rules.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class Rules { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class Rules extends GenericResource { public function getAll($enabled = null, $fields = null, $include_fields = null) { diff --git a/src/API/Stats.php b/src/API/Stats.php index 8542e424..17f568ce 100644 --- a/src/API/Stats.php +++ b/src/API/Stats.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class Stats { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class Stats extends GenericResource { public function getActiveUsersCount() { diff --git a/src/API/Tenants.php b/src/API/Tenants.php index da6e3b1f..7580cd7f 100644 --- a/src/API/Tenants.php +++ b/src/API/Tenants.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class Tenants { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class Tenants extends GenericResource { public function get($fields = null, $include_fields = null) { diff --git a/src/API/Tickets.php b/src/API/Tickets.php index f2032945..cb7f7111 100644 --- a/src/API/Tickets.php +++ b/src/API/Tickets.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class Tickets { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class Tickets extends GenericResource { public function createEmailVerificationTicket($user_id, $result_url = null) { diff --git a/src/API/UserBlocks.php b/src/API/UserBlocks.php index 03f5cb67..bfc4dd81 100644 --- a/src/API/UserBlocks.php +++ b/src/API/UserBlocks.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class UserBlocks { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class UserBlocks extends GenericResource { public function get($user_id) { diff --git a/src/API/Users.php b/src/API/Users.php index 37e8084f..9c8c482b 100644 --- a/src/API/Users.php +++ b/src/API/Users.php @@ -5,13 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Header\ContentType; -class Users { - - protected $apiClient; - - public function __construct(ApiClient $apiClient) { - $this->apiClient = $apiClient; - } +class Users extends GenericResource { public function get($user_id) { diff --git a/src/Auth0Api.php b/src/Auth0Api.php index a7aec1b5..8b95750f 100644 --- a/src/Auth0Api.php +++ b/src/Auth0Api.php @@ -22,6 +22,7 @@ class Auth0Api { private $token; private $domain; private $apiClient; + private $guzzleOptions; public $blacklists; public $clients; @@ -36,9 +37,10 @@ class Auth0Api { public $userBlocks; public $users; - public function __construct($token, $domain) { + public function __construct($token, $domain, $guzzleOptions = []) { $this->token = $token; $this->domain = $domain; + $this->guzzleOptions = $guzzleOptions; $this->setApiClient(); @@ -61,7 +63,8 @@ protected function setApiClient() { $client = new ApiClient([ 'domain' => $apiDomain, - 'basePath' => '/api/v2', + 'basePath' => '/api/v2/', + 'guzzleOptions' => $this->guzzleOptions, 'headers' => [ new AuthorizationBearer($this->token) ] diff --git a/tests/BasicCrudTest.php b/tests/BasicCrudTest.php index 660bf2f1..ddb73006 100644 --- a/tests/BasicCrudTest.php +++ b/tests/BasicCrudTest.php @@ -5,6 +5,8 @@ abstract class BasicCrudTest extends ApiTests { + protected $domain; + protected abstract function getApiClient(); protected abstract function getCreateBody(); protected abstract function getUpdateBody(); @@ -19,6 +21,11 @@ public function testAll() { $client = $this->getApiClient(); + $options = $client->getApiClient()->get()->getGuzzleOptions(); + + $this->assertArrayHasKey('base_uri', $options); + $this->assertEquals("https://$this->domain/api/v2/", $options['base_uri']); + $created = $client->create($this->getCreateBody()); $all = $client->getAll(); diff --git a/tests/BlacklistsTest.php b/tests/BlacklistsTest.php index c6f545c5..972d4138 100644 --- a/tests/BlacklistsTest.php +++ b/tests/BlacklistsTest.php @@ -13,6 +13,8 @@ public function testBlacklistAndGet() { ] ]); + $this->domain = $env['DOMAIN']; + $api = new Auth0Api($token, $env['DOMAIN']); $aud = $env["GLOBAL_CLIENT_ID"]; diff --git a/tests/ClientsTest.php b/tests/ClientsTest.php index bd0c6df3..93a4c82d 100644 --- a/tests/ClientsTest.php +++ b/tests/ClientsTest.php @@ -17,6 +17,8 @@ protected function getApiClient() { ] ]); + $this->domain = $env['DOMAIN']; + $api = new Auth0Api($token, $env['DOMAIN']); return $api->clients; diff --git a/tests/ConnectionsTest.php b/tests/ConnectionsTest.php index 71bcfa8c..7435aa97 100644 --- a/tests/ConnectionsTest.php +++ b/tests/ConnectionsTest.php @@ -13,6 +13,8 @@ protected function getApiClient() { ] ]); + $this->domain = $env['DOMAIN']; + $api = new Auth0Api($token, $env['DOMAIN']); return $api->connections; diff --git a/tests/RequestBuilderTest.php b/tests/RequestBuilderTest.php index d7ce2041..b8774c78 100644 --- a/tests/RequestBuilderTest.php +++ b/tests/RequestBuilderTest.php @@ -9,18 +9,19 @@ public function testUrl() { $builder = new RequestBuilder([ 'domain' => 'www.domain.com', + 'basePath' => '/api', 'method' => 'get', ]); - $this->assertEquals('www.domain.com/', $builder->getUrl()); + $this->assertEquals('', $builder->getUrl()); $builder->path1(); - $this->assertEquals('www.domain.com/path1', $builder->getUrl()); + $this->assertEquals('path1', $builder->getUrl()); $builder->path2(3); - $this->assertEquals('www.domain.com/path1/path2/3', $builder->getUrl()); + $this->assertEquals('path1/path2/3', $builder->getUrl()); } @@ -60,7 +61,32 @@ public function testFullUrl() { ['key' => 'param2', 'value' => 'value2'], ]); - $this->assertEquals('www.domain.com/path/2/subpath?param1=value1¶m2=value2', $builder->getUrl()); + $this->assertEquals('path/2/subpath?param1=value1¶m2=value2', $builder->getUrl()); + } + + public function testGetGuzzleOptions() { + $builder = new RequestBuilder([ + 'domain' => 'www.domain.com', + 'method' => 'get', + ]); + + $options = $builder->getGuzzleOptions(); + + $this->assertArrayHasKey('base_uri', $options); + $this->assertEquals('www.domain.com', $options['base_uri']); + } + + public function testgGetGuzzleOptionsWithBasePath() { + $builder = new RequestBuilder([ + 'domain' => 'www.domain.com', + 'basePath' => '/api', + 'method' => 'get', + ]); + + $options = $builder->getGuzzleOptions(); + + $this->assertArrayHasKey('base_uri', $options); + $this->assertEquals('www.domain.com/api', $options['base_uri']); } } \ No newline at end of file diff --git a/tests/RulesTest.php b/tests/RulesTest.php index db30c8ea..5356b22e 100644 --- a/tests/RulesTest.php +++ b/tests/RulesTest.php @@ -13,6 +13,8 @@ protected function getApiClient() { ] ]); + $this->domain = $env['DOMAIN']; + $api = new Auth0Api($token, $env['DOMAIN']); return $api->rules; diff --git a/tests/UsersTest.php b/tests/UsersTest.php index 0e1e86f1..2a5faa8f 100644 --- a/tests/UsersTest.php +++ b/tests/UsersTest.php @@ -24,7 +24,9 @@ protected function getApiClient($scopes = null) { ] ]); - $api = new Auth0Api($token, $env['DOMAIN']); + $this->domain = $env['DOMAIN']; + + $api = new Auth0Api($token, $this->domain); return $api->users; } From ed6aa11005c3ddecf1f51e8c2b3ecf67c7986088 Mon Sep 17 00:00:00 2001 From: German Lena Date: Thu, 10 Mar 2016 12:16:42 -0300 Subject: [PATCH 2/2] updated doc --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 58516ea6..c18fa6b3 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,9 @@ This is a common issue with latest PHP versions under windows (related to a inco ``` $token = "eyJhbGciO....eyJhdWQiOiI....1ZVDisdL..."; $domain = "account.auth0.com"; +$guzzleOptions = [ ... ]; -$auth0Api = new \Auth0\SDK\Auth0Api($token, $domain); +$auth0Api = new \Auth0\SDK\Auth0Api($token, $domain, $guzzleOptions); /* $guzzleOptions is optional */ $usersList = $auth0Api->users->search([ "q" => "email@test.com" ]); ```