From 918bcd464463cd9593c49ccce27eb47405ae53b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Preu=C3=9F?= Date: Mon, 2 Dec 2019 14:18:58 +0100 Subject: [PATCH] Add methods to change base url --- README.md | 2 +- README.stub | 2 +- config/bitinflow-accounts-api.php | 1 + .../BitinflowAccounts/BitinflowAccounts.php | 65 ++++++++++++------- src/GhostZero/BitinflowAccounts/Result.php | 28 ++++---- .../BitinflowAccounts/ApiDocumentsTest.php | 2 +- .../TestCases/ApiTestCase.php | 10 +++ 7 files changed, 69 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 10b6c44..dc6217b 100644 --- a/README.md +++ b/README.md @@ -198,7 +198,7 @@ composer test ``` ```shell -CLIENT_ID=xxxx CLIENT_KEY=yyyy CLIENT_ACCESS_TOKEN=zzzz composer test +BASE_URL=xxxx CLIENT_ID=xxxx CLIENT_KEY=yyyy CLIENT_ACCESS_TOKEN=zzzz composer test ``` #### Generate Documentation diff --git a/README.stub b/README.stub index 51d1f12..656bb38 100644 --- a/README.stub +++ b/README.stub @@ -162,7 +162,7 @@ composer test ``` ```shell -CLIENT_ID=xxxx CLIENT_KEY=yyyy CLIENT_ACCESS_TOKEN=zzzz composer test +BASE_URL=xxxx CLIENT_ID=xxxx CLIENT_KEY=yyyy CLIENT_ACCESS_TOKEN=zzzz composer test ``` #### Generate Documentation diff --git a/config/bitinflow-accounts-api.php b/config/bitinflow-accounts-api.php index 4c509c4..ab5c57f 100644 --- a/config/bitinflow-accounts-api.php +++ b/config/bitinflow-accounts-api.php @@ -4,4 +4,5 @@ return [ 'client_id' => env('BITINFLOW_ACCOUNTS_KEY', ''), 'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET', ''), 'redirect_url' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI', ''), + 'base_url' => env('BITINFLOW_ACCOUNTS_BASE_URI', ''), ]; \ No newline at end of file diff --git a/src/GhostZero/BitinflowAccounts/BitinflowAccounts.php b/src/GhostZero/BitinflowAccounts/BitinflowAccounts.php index 1ea695d..035bf0f 100644 --- a/src/GhostZero/BitinflowAccounts/BitinflowAccounts.php +++ b/src/GhostZero/BitinflowAccounts/BitinflowAccounts.php @@ -2,6 +2,7 @@ namespace GhostZero\BitinflowAccounts; +use GhostZero\BitinflowAccounts\ApiOperations; use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresAuthenticationException; use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresClientIdException; use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresRedirectUriException; @@ -23,8 +24,12 @@ class BitinflowAccounts use Traits\SshKeysTrait; use Traits\UsersTrait; - private const BASE_URI = 'https://accounts.bitinflow.com/api/'; - private const OAUTH_BASE_URI = 'https://accounts.bitinflow.com/api/'; + use ApiOperations\Delete; + use ApiOperations\Get; + use ApiOperations\Post; + use ApiOperations\Put; + + private static $baseUrl = 'https://accounts.bitinflow.com/api/'; /** * Guzzle is used to make http requests. @@ -76,11 +81,23 @@ class BitinflowAccounts if ($redirectUri = config('bitinflow-accounts-api.redirect_url')) { $this->setRedirectUri($redirectUri); } + if ($redirectUri = config('bitinflow-accounts-api.base_url')) { + self::setBaseUrl($redirectUri); + } $this->client = new Client([ - 'base_uri' => self::BASE_URI, + 'base_uri' => self::$baseUrl, ]); } + /** + * @internal only for internal and debug purposes. + * @param string $baseUrl + */ + public static function setBaseUrl(string $baseUrl): void + { + self::$baseUrl = $baseUrl; + } + /** * Get client id. * @return string @@ -98,7 +115,7 @@ class BitinflowAccounts /** * Set client id. * - * @param string $clientId bitinflow Accounts client id + * @param string $clientId bitinflow Accounts client id * * @return void */ @@ -110,7 +127,7 @@ class BitinflowAccounts /** * Fluid client id setter. * - * @param string $clientId bitinflow Accounts client id. + * @param string $clientId bitinflow Accounts client id. * * @return self */ @@ -138,7 +155,7 @@ class BitinflowAccounts /** * Set client secret. * - * @param string $clientSecret bitinflow Accounts client secret + * @param string $clientSecret bitinflow Accounts client secret * * @return void */ @@ -150,7 +167,7 @@ class BitinflowAccounts /** * Fluid client secret setter. * - * @param string $clientSecret bitinflow Accounts client secret + * @param string $clientSecret bitinflow Accounts client secret * * @return self */ @@ -178,7 +195,7 @@ class BitinflowAccounts /** * Set redirect url. * - * @param string $redirectUri + * @param string $redirectUri * * @return void */ @@ -190,7 +207,7 @@ class BitinflowAccounts /** * Fluid redirect url setter. * - * @param string $redirectUri + * @param string $redirectUri * * @return self */ @@ -219,7 +236,7 @@ class BitinflowAccounts /** * Set OAuth token. * - * @param string $token bitinflow Accounts OAuth token + * @param string $token bitinflow Accounts OAuth token * * @return void */ @@ -231,7 +248,7 @@ class BitinflowAccounts /** * Fluid OAuth token setter. * - * @param string $token bitinflow Accounts OAuth token + * @param string $token bitinflow Accounts OAuth token * * @return self */ @@ -251,7 +268,7 @@ class BitinflowAccounts * @throws GuzzleException * @throws RequestRequiresClientIdException */ - public function get(string $path = '', array $parameters = [], Paginator $paginator = null) + public function get(string $path = '', array $parameters = [], Paginator $paginator = null): Result { return $this->query('GET', $path, $parameters, $paginator); } @@ -265,7 +282,7 @@ class BitinflowAccounts * @throws GuzzleException * @throws RequestRequiresClientIdException */ - public function post(string $path = '', array $parameters = [], Paginator $paginator = null) + public function post(string $path = '', array $parameters = [], Paginator $paginator = null): Result { return $this->query('POST', $path, $parameters, $paginator); } @@ -279,7 +296,7 @@ class BitinflowAccounts * @throws GuzzleException * @throws RequestRequiresClientIdException */ - public function delete(string $path = '', array $parameters = [], Paginator $paginator = null) + public function delete(string $path = '', array $parameters = [], Paginator $paginator = null): Result { return $this->query('DELETE', $path, $parameters, $paginator); } @@ -293,7 +310,7 @@ class BitinflowAccounts * @throws GuzzleException * @throws RequestRequiresClientIdException */ - public function put(string $path = '', array $parameters = [], Paginator $paginator = null) + public function put(string $path = '', array $parameters = [], Paginator $paginator = null): Result { return $this->query('PUT', $path, $parameters, $paginator); } @@ -307,7 +324,7 @@ class BitinflowAccounts * @throws GuzzleException * @throws RequestRequiresClientIdException */ - public function json(string $method, string $path = '', array $body = null) + public function json(string $method, string $path = '', array $body = null): Result { if ($body) { $body = json_encode(['data' => $body]); @@ -319,11 +336,11 @@ class BitinflowAccounts /** * Build query & execute. * - * @param string $method HTTP method - * @param string $path Query path - * @param array $parameters Query parameters - * @param Paginator $paginator Paginator object - * @param mixed|null $jsonBody JSON data + * @param string $method HTTP method + * @param string $path Query path + * @param array $parameters Query parameters + * @param Paginator $paginator Paginator object + * @param mixed|null $jsonBody JSON data * * @return Result Result object * @throws GuzzleException @@ -338,7 +355,7 @@ class BitinflowAccounts $response = $this->client->request($method, $path, [ 'headers' => $this->buildHeaders($jsonBody ? true : false), 'query' => $this->buildQuery($parameters), - 'json' => $jsonBody ? $jsonBody : null, + 'json' => $jsonBody ?: null, ]); $result = new Result($response, null, $paginator); } catch (RequestException $exception) { @@ -352,7 +369,7 @@ class BitinflowAccounts /** * Build query with support for multiple smae first-dimension keys. * - * @param array $query + * @param array $query * * @return string */ @@ -372,7 +389,7 @@ class BitinflowAccounts /** * Build headers for request. * - * @param bool $json Body is JSON + * @param bool $json Body is JSON * * @return array * @throws RequestRequiresClientIdException diff --git a/src/GhostZero/BitinflowAccounts/Result.php b/src/GhostZero/BitinflowAccounts/Result.php index 0be4819..37ed4d6 100644 --- a/src/GhostZero/BitinflowAccounts/Result.php +++ b/src/GhostZero/BitinflowAccounts/Result.php @@ -60,7 +60,7 @@ class Result /** * Original Guzzle HTTP Response. - * @var Response + * @var Response|null */ public $response; @@ -73,17 +73,17 @@ class Result /** * Constructor, * - * @param Response $response HTTP response + * @param Response|null $response HTTP response * @param Exception|mixed $exception Exception, if present * @param null|Paginator $paginator Paginator, if present */ - public function __construct(Response $response, Exception $exception = null, Paginator $paginator = null) + public function __construct(?Response $response, Exception $exception = null, Paginator $paginator = null) { $this->response = $response; $this->success = $exception === null; $this->exception = $exception; - $this->status = $response->getStatusCode(); - $jsonResponse = @json_decode($response->getBody()->getContents()); + $this->status = $response ? $response->getStatusCode() : 500; + $jsonResponse = $response ? @json_decode($response->getBody()->getContents(), false) : null; if ($jsonResponse !== null) { $this->setProperty($jsonResponse, 'data'); $this->setProperty($jsonResponse, 'total'); @@ -99,10 +99,10 @@ class Result * @param string $responseProperty Response property name * @param string|null $attribute Class property name */ - private function setProperty(stdClass $jsonResponse, string $responseProperty, string $attribute = null) + private function setProperty(stdClass $jsonResponse, string $responseProperty, string $attribute = null): void { $classAttribute = $attribute ?? $responseProperty; - if (!empty($jsonResponse) && property_exists($jsonResponse, $responseProperty)) { + if ($jsonResponse !== null && property_exists($jsonResponse, $responseProperty)) { $this->{$classAttribute} = $jsonResponse->{$responseProperty}; } elseif ($responseProperty === 'data') { $this->{$classAttribute} = $jsonResponse; @@ -174,7 +174,7 @@ class Result * Set the Paginator to fetch the first set of results. * @return null|Paginator */ - public function first() + public function first(): ?Paginator { return $this->paginator !== null ? $this->paginator->first() : null; } @@ -183,7 +183,7 @@ class Result * Set the Paginator to fetch the next set of results. * @return null|Paginator */ - public function next() + public function next(): ?Paginator { return $this->paginator !== null ? $this->paginator->next() : null; } @@ -192,7 +192,7 @@ class Result * Set the Paginator to fetch the last set of results. * @return null|Paginator */ - public function back() + public function back(): ?Paginator { return $this->paginator !== null ? $this->paginator->back() : null; } @@ -200,7 +200,7 @@ class Result /** * Get rate limit information. * - * @param string|null $key Get defined index + * @param string|null $key Get defined index * * @return string|array|null */ @@ -224,8 +224,8 @@ class Result /** * Insert users in data response. * - * @param string $identifierAttribute Attribute to identify the users - * @param string $insertTo Data index to insert user data + * @param string $identifierAttribute Attribute to identify the users + * @param string $insertTo Data index to insert user data * * @return self */ @@ -235,7 +235,7 @@ class Result $userIds = collect($data)->map(function ($item) use ($identifierAttribute) { return $item->{$identifierAttribute}; })->toArray(); - if (count($userIds) == 0) { + if (count($userIds) === 0) { return $this; } $users = collect($this->bitinflow->getUsersByIds($userIds)->data); diff --git a/tests/GhostZero/BitinflowAccounts/ApiDocumentsTest.php b/tests/GhostZero/BitinflowAccounts/ApiDocumentsTest.php index 630c127..e4fe037 100644 --- a/tests/GhostZero/BitinflowAccounts/ApiDocumentsTest.php +++ b/tests/GhostZero/BitinflowAccounts/ApiDocumentsTest.php @@ -45,7 +45,7 @@ class ApiDocumentsTest extends ApiTestCase $expiresAt = now()->addHours(2); - $result = $this->getClient()->createDocumentDownloadUrl(1, $expiresAt); + $result = $this->getClient()->createDocumentDownloadUrl('1', $expiresAt); $this->registerResult($result); $this->assertTrue($result->success()); diff --git a/tests/GhostZero/BitinflowAccounts/TestCases/ApiTestCase.php b/tests/GhostZero/BitinflowAccounts/TestCases/ApiTestCase.php index 3a058ef..332330f 100644 --- a/tests/GhostZero/BitinflowAccounts/TestCases/ApiTestCase.php +++ b/tests/GhostZero/BitinflowAccounts/TestCases/ApiTestCase.php @@ -18,6 +18,11 @@ abstract class ApiTestCase extends TestCase protected function setUp(): void { parent::setUp(); + + if ($this->getBaseUrl()) { + BitinflowAccounts::setBaseUrl($this->getBaseUrl()); + } + if (!$this->getClientId()) { $this->markTestSkipped('No Client-ID given'); } @@ -34,6 +39,11 @@ abstract class ApiTestCase extends TestCase return $result; } + protected function getBaseUrl() + { + return getenv('BASE_URL'); + } + protected function getClientId() { return getenv('CLIENT_ID');