Add methods to change base url

This commit is contained in:
René Preuß
2019-12-02 14:18:58 +01:00
parent 2bd19efb3c
commit 918bcd4644
7 changed files with 69 additions and 41 deletions

View File

@@ -198,7 +198,7 @@ composer test
``` ```
```shell ```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 #### Generate Documentation

View File

@@ -162,7 +162,7 @@ composer test
``` ```
```shell ```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 #### Generate Documentation

View File

@@ -4,4 +4,5 @@ return [
'client_id' => env('BITINFLOW_ACCOUNTS_KEY', ''), 'client_id' => env('BITINFLOW_ACCOUNTS_KEY', ''),
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET', ''), 'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET', ''),
'redirect_url' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI', ''), 'redirect_url' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI', ''),
'base_url' => env('BITINFLOW_ACCOUNTS_BASE_URI', ''),
]; ];

View File

@@ -2,6 +2,7 @@
namespace GhostZero\BitinflowAccounts; namespace GhostZero\BitinflowAccounts;
use GhostZero\BitinflowAccounts\ApiOperations;
use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresAuthenticationException; use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresAuthenticationException;
use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresClientIdException; use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresClientIdException;
use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresRedirectUriException; use GhostZero\BitinflowAccounts\Exceptions\RequestRequiresRedirectUriException;
@@ -23,8 +24,12 @@ class BitinflowAccounts
use Traits\SshKeysTrait; use Traits\SshKeysTrait;
use Traits\UsersTrait; use Traits\UsersTrait;
private const BASE_URI = 'https://accounts.bitinflow.com/api/'; use ApiOperations\Delete;
private const OAUTH_BASE_URI = 'https://accounts.bitinflow.com/api/'; use ApiOperations\Get;
use ApiOperations\Post;
use ApiOperations\Put;
private static $baseUrl = 'https://accounts.bitinflow.com/api/';
/** /**
* Guzzle is used to make http requests. * Guzzle is used to make http requests.
@@ -76,11 +81,23 @@ class BitinflowAccounts
if ($redirectUri = config('bitinflow-accounts-api.redirect_url')) { if ($redirectUri = config('bitinflow-accounts-api.redirect_url')) {
$this->setRedirectUri($redirectUri); $this->setRedirectUri($redirectUri);
} }
if ($redirectUri = config('bitinflow-accounts-api.base_url')) {
self::setBaseUrl($redirectUri);
}
$this->client = new Client([ $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. * Get client id.
* @return string * @return string
@@ -98,7 +115,7 @@ class BitinflowAccounts
/** /**
* Set client id. * Set client id.
* *
* @param string $clientId bitinflow Accounts client id * @param string $clientId bitinflow Accounts client id
* *
* @return void * @return void
*/ */
@@ -110,7 +127,7 @@ class BitinflowAccounts
/** /**
* Fluid client id setter. * Fluid client id setter.
* *
* @param string $clientId bitinflow Accounts client id. * @param string $clientId bitinflow Accounts client id.
* *
* @return self * @return self
*/ */
@@ -138,7 +155,7 @@ class BitinflowAccounts
/** /**
* Set client secret. * Set client secret.
* *
* @param string $clientSecret bitinflow Accounts client secret * @param string $clientSecret bitinflow Accounts client secret
* *
* @return void * @return void
*/ */
@@ -150,7 +167,7 @@ class BitinflowAccounts
/** /**
* Fluid client secret setter. * Fluid client secret setter.
* *
* @param string $clientSecret bitinflow Accounts client secret * @param string $clientSecret bitinflow Accounts client secret
* *
* @return self * @return self
*/ */
@@ -178,7 +195,7 @@ class BitinflowAccounts
/** /**
* Set redirect url. * Set redirect url.
* *
* @param string $redirectUri * @param string $redirectUri
* *
* @return void * @return void
*/ */
@@ -190,7 +207,7 @@ class BitinflowAccounts
/** /**
* Fluid redirect url setter. * Fluid redirect url setter.
* *
* @param string $redirectUri * @param string $redirectUri
* *
* @return self * @return self
*/ */
@@ -219,7 +236,7 @@ class BitinflowAccounts
/** /**
* Set OAuth token. * Set OAuth token.
* *
* @param string $token bitinflow Accounts OAuth token * @param string $token bitinflow Accounts OAuth token
* *
* @return void * @return void
*/ */
@@ -231,7 +248,7 @@ class BitinflowAccounts
/** /**
* Fluid OAuth token setter. * Fluid OAuth token setter.
* *
* @param string $token bitinflow Accounts OAuth token * @param string $token bitinflow Accounts OAuth token
* *
* @return self * @return self
*/ */
@@ -251,7 +268,7 @@ class BitinflowAccounts
* @throws GuzzleException * @throws GuzzleException
* @throws RequestRequiresClientIdException * @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); return $this->query('GET', $path, $parameters, $paginator);
} }
@@ -265,7 +282,7 @@ class BitinflowAccounts
* @throws GuzzleException * @throws GuzzleException
* @throws RequestRequiresClientIdException * @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); return $this->query('POST', $path, $parameters, $paginator);
} }
@@ -279,7 +296,7 @@ class BitinflowAccounts
* @throws GuzzleException * @throws GuzzleException
* @throws RequestRequiresClientIdException * @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); return $this->query('DELETE', $path, $parameters, $paginator);
} }
@@ -293,7 +310,7 @@ class BitinflowAccounts
* @throws GuzzleException * @throws GuzzleException
* @throws RequestRequiresClientIdException * @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); return $this->query('PUT', $path, $parameters, $paginator);
} }
@@ -307,7 +324,7 @@ class BitinflowAccounts
* @throws GuzzleException * @throws GuzzleException
* @throws RequestRequiresClientIdException * @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) { if ($body) {
$body = json_encode(['data' => $body]); $body = json_encode(['data' => $body]);
@@ -319,11 +336,11 @@ class BitinflowAccounts
/** /**
* Build query & execute. * Build query & execute.
* *
* @param string $method HTTP method * @param string $method HTTP method
* @param string $path Query path * @param string $path Query path
* @param array $parameters Query parameters * @param array $parameters Query parameters
* @param Paginator $paginator Paginator object * @param Paginator $paginator Paginator object
* @param mixed|null $jsonBody JSON data * @param mixed|null $jsonBody JSON data
* *
* @return Result Result object * @return Result Result object
* @throws GuzzleException * @throws GuzzleException
@@ -338,7 +355,7 @@ class BitinflowAccounts
$response = $this->client->request($method, $path, [ $response = $this->client->request($method, $path, [
'headers' => $this->buildHeaders($jsonBody ? true : false), 'headers' => $this->buildHeaders($jsonBody ? true : false),
'query' => $this->buildQuery($parameters), 'query' => $this->buildQuery($parameters),
'json' => $jsonBody ? $jsonBody : null, 'json' => $jsonBody ?: null,
]); ]);
$result = new Result($response, null, $paginator); $result = new Result($response, null, $paginator);
} catch (RequestException $exception) { } catch (RequestException $exception) {
@@ -352,7 +369,7 @@ class BitinflowAccounts
/** /**
* Build query with support for multiple smae first-dimension keys. * Build query with support for multiple smae first-dimension keys.
* *
* @param array $query * @param array $query
* *
* @return string * @return string
*/ */
@@ -372,7 +389,7 @@ class BitinflowAccounts
/** /**
* Build headers for request. * Build headers for request.
* *
* @param bool $json Body is JSON * @param bool $json Body is JSON
* *
* @return array * @return array
* @throws RequestRequiresClientIdException * @throws RequestRequiresClientIdException

View File

@@ -60,7 +60,7 @@ class Result
/** /**
* Original Guzzle HTTP Response. * Original Guzzle HTTP Response.
* @var Response * @var Response|null
*/ */
public $response; public $response;
@@ -73,17 +73,17 @@ class Result
/** /**
* Constructor, * Constructor,
* *
* @param Response $response HTTP response * @param Response|null $response HTTP response
* @param Exception|mixed $exception Exception, if present * @param Exception|mixed $exception Exception, if present
* @param null|Paginator $paginator Paginator, 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->response = $response;
$this->success = $exception === null; $this->success = $exception === null;
$this->exception = $exception; $this->exception = $exception;
$this->status = $response->getStatusCode(); $this->status = $response ? $response->getStatusCode() : 500;
$jsonResponse = @json_decode($response->getBody()->getContents()); $jsonResponse = $response ? @json_decode($response->getBody()->getContents(), false) : null;
if ($jsonResponse !== null) { if ($jsonResponse !== null) {
$this->setProperty($jsonResponse, 'data'); $this->setProperty($jsonResponse, 'data');
$this->setProperty($jsonResponse, 'total'); $this->setProperty($jsonResponse, 'total');
@@ -99,10 +99,10 @@ class Result
* @param string $responseProperty Response property name * @param string $responseProperty Response property name
* @param string|null $attribute Class 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; $classAttribute = $attribute ?? $responseProperty;
if (!empty($jsonResponse) && property_exists($jsonResponse, $responseProperty)) { if ($jsonResponse !== null && property_exists($jsonResponse, $responseProperty)) {
$this->{$classAttribute} = $jsonResponse->{$responseProperty}; $this->{$classAttribute} = $jsonResponse->{$responseProperty};
} elseif ($responseProperty === 'data') { } elseif ($responseProperty === 'data') {
$this->{$classAttribute} = $jsonResponse; $this->{$classAttribute} = $jsonResponse;
@@ -174,7 +174,7 @@ class Result
* Set the Paginator to fetch the first set of results. * Set the Paginator to fetch the first set of results.
* @return null|Paginator * @return null|Paginator
*/ */
public function first() public function first(): ?Paginator
{ {
return $this->paginator !== null ? $this->paginator->first() : null; return $this->paginator !== null ? $this->paginator->first() : null;
} }
@@ -183,7 +183,7 @@ class Result
* Set the Paginator to fetch the next set of results. * Set the Paginator to fetch the next set of results.
* @return null|Paginator * @return null|Paginator
*/ */
public function next() public function next(): ?Paginator
{ {
return $this->paginator !== null ? $this->paginator->next() : null; return $this->paginator !== null ? $this->paginator->next() : null;
} }
@@ -192,7 +192,7 @@ class Result
* Set the Paginator to fetch the last set of results. * Set the Paginator to fetch the last set of results.
* @return null|Paginator * @return null|Paginator
*/ */
public function back() public function back(): ?Paginator
{ {
return $this->paginator !== null ? $this->paginator->back() : null; return $this->paginator !== null ? $this->paginator->back() : null;
} }
@@ -200,7 +200,7 @@ class Result
/** /**
* Get rate limit information. * Get rate limit information.
* *
* @param string|null $key Get defined index * @param string|null $key Get defined index
* *
* @return string|array|null * @return string|array|null
*/ */
@@ -224,8 +224,8 @@ class Result
/** /**
* Insert users in data response. * Insert users in data response.
* *
* @param string $identifierAttribute Attribute to identify the users * @param string $identifierAttribute Attribute to identify the users
* @param string $insertTo Data index to insert user data * @param string $insertTo Data index to insert user data
* *
* @return self * @return self
*/ */
@@ -235,7 +235,7 @@ class Result
$userIds = collect($data)->map(function ($item) use ($identifierAttribute) { $userIds = collect($data)->map(function ($item) use ($identifierAttribute) {
return $item->{$identifierAttribute}; return $item->{$identifierAttribute};
})->toArray(); })->toArray();
if (count($userIds) == 0) { if (count($userIds) === 0) {
return $this; return $this;
} }
$users = collect($this->bitinflow->getUsersByIds($userIds)->data); $users = collect($this->bitinflow->getUsersByIds($userIds)->data);

View File

@@ -45,7 +45,7 @@ class ApiDocumentsTest extends ApiTestCase
$expiresAt = now()->addHours(2); $expiresAt = now()->addHours(2);
$result = $this->getClient()->createDocumentDownloadUrl(1, $expiresAt); $result = $this->getClient()->createDocumentDownloadUrl('1', $expiresAt);
$this->registerResult($result); $this->registerResult($result);
$this->assertTrue($result->success()); $this->assertTrue($result->success());

View File

@@ -18,6 +18,11 @@ abstract class ApiTestCase extends TestCase
protected function setUp(): void protected function setUp(): void
{ {
parent::setUp(); parent::setUp();
if ($this->getBaseUrl()) {
BitinflowAccounts::setBaseUrl($this->getBaseUrl());
}
if (!$this->getClientId()) { if (!$this->getClientId()) {
$this->markTestSkipped('No Client-ID given'); $this->markTestSkipped('No Client-ID given');
} }
@@ -34,6 +39,11 @@ abstract class ApiTestCase extends TestCase
return $result; return $result;
} }
protected function getBaseUrl()
{
return getenv('BASE_URL');
}
protected function getClientId() protected function getClientId()
{ {
return getenv('CLIENT_ID'); return getenv('CLIENT_ID');