generated from farzai/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
187 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Farzai\Bitkub\Responses; | ||
|
||
use Farzai\Transport\Contracts\ResponseInterface; | ||
use Farzai\Transport\Traits\PsrResponseTrait; | ||
use Psr\Http\Message\RequestInterface as PsrRequestInterface; | ||
|
||
abstract class ResponseDecorator implements ResponseInterface | ||
{ | ||
use PsrResponseTrait; | ||
|
||
protected ResponseInterface $response; | ||
|
||
/** | ||
* Create a new response instance. | ||
*/ | ||
public function __construct(ResponseInterface $response) | ||
{ | ||
$this->response = $response; | ||
} | ||
|
||
/** | ||
* Return the response status code. | ||
*/ | ||
public function statusCode(): int | ||
{ | ||
return $this->response->statusCode(); | ||
} | ||
|
||
/** | ||
* Return the response body. | ||
*/ | ||
public function body(): string | ||
{ | ||
return $this->response->body(); | ||
} | ||
|
||
/** | ||
* Return the response headers. | ||
*/ | ||
public function headers(): array | ||
{ | ||
return $this->response->headers(); | ||
} | ||
|
||
/** | ||
* Check if the response is successfull. | ||
*/ | ||
public function isSuccessfull(): bool | ||
{ | ||
return $this->response->isSuccessfull(); | ||
} | ||
|
||
/** | ||
* Return the json decoded response. | ||
*/ | ||
public function json(string $key = null): mixed | ||
{ | ||
return $this->response->json($key); | ||
} | ||
|
||
/** | ||
* Throw an exception if the response is not successfull. | ||
* | ||
* @param callable<\Farzai\Transport\Contracts\ResponseInterface> $callback Custom callback to throw an exception. | ||
* | ||
* @throws \Psr\Http\Client\ClientExceptionInterface | ||
*/ | ||
public function throw(callable $callback = null) | ||
{ | ||
return $this->response->throw($callback); | ||
} | ||
|
||
/** | ||
* Return the psr request. | ||
*/ | ||
public function getPsrRequest(): PsrRequestInterface | ||
{ | ||
return $this->response->getPsrRequest(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Farzai\Bitkub\Responses; | ||
|
||
use Farzai\Bitkub\Constants\ErrorCodes; | ||
use Farzai\Transport\Contracts\ResponseInterface; | ||
|
||
class ResponseWithValidateErrorCode extends ResponseDecorator | ||
{ | ||
/** | ||
* Throw an exception if the response is not successfull. | ||
* | ||
* @param callable<\Farzai\Transport\Contracts\ResponseInterface> $callback Custom callback to throw an exception. | ||
* | ||
* @throws \Psr\Http\Client\ClientExceptionInterface | ||
*/ | ||
public function throw(callable $callback = null) | ||
{ | ||
$callback = $callback ?? function (ResponseInterface $response, ?\Exception $e) use ($callback) { | ||
if ($this->json('error') !== 0) { | ||
throw new \Exception(ErrorCodes::getDescription($this->json('error'))); | ||
} | ||
|
||
return $callback ? $callback($response, $e) : $response; | ||
}; | ||
|
||
return parent::throw($callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
<?php | ||
|
||
use Farzai\Bitkub\Authorizer; | ||
|
||
it('can generate signature success', function () { | ||
// | ||
$secret = 'test-secret'; | ||
$timestamp = 1630483200000; | ||
|
||
$method = 'POST'; | ||
$path = '/api/v3/market/balances'; | ||
$query = ''; | ||
$payload = ''; | ||
|
||
$authorizer = new Authorizer(); | ||
|
||
$signature = $authorizer->generateSignature($secret, $timestamp, $method, $path, $query, $payload); | ||
|
||
expect($signature)->toBe('b8403c345ce41b25b47885254fb8aeed9ad7ceb9e30425b86a9a151dd6ac2e35'); | ||
}); |