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
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

View File

@@ -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

View File

@@ -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', ''),
];

View File

@@ -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
@@ -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]);
@@ -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) {

View File

@@ -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;
}
@@ -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);

View File

@@ -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());

View File

@@ -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');