Skip to content

Commit

Permalink
Merge pull request #14 from coinpaprika/feature/CPW-4284
Browse files Browse the repository at this point in the history
Feature/cpw 4284
  • Loading branch information
beaumind authored Jun 10, 2022
2 parents 43f5be1 + 7e9442d commit 0dfd8ec
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ For deserialization process it is advised to initiate the `$client` with `$cache
$client = new \Coinpaprika\Client('my/cache/directory');
$coins = $client->getTickers();
```

## Paid API
To use paid version set the api key
```php
<?php

// This file is generated by Composer
require_once __DIR__ . '/vendor/autoload.php';

$client = new \Coinpaprika\Client('my/cache/directory');
$client->setApiKey('PROVIDED_API_KEY');
$coins = $client->getCoins();
```

## Examples

Check out the [`./examples`](./examples) directory.
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "coinpaprika/coinpaprika-api-php-client",
"version": "1.3.0",
"description": "coinpaprika.com API v1 client",
"type": "library",
"license": "MIT",
Expand All @@ -13,14 +14,14 @@
],
"minimum-stability": "dev",
"require": {
"php": ">=7.1",
"php": ">=7.2.5",
"jms/serializer": "^1.13.0",
"guzzlehttp/guzzle": "^6.3.3",
"symfony/yaml": "^4.1.4",
"doctrine/cache": "^1.8@dev"
"guzzlehttp/guzzle": "^7.4.3",
"symfony/yaml": "^6.1.0",
"doctrine/cache": "^2.2"
},
"require-dev": {
"phpunit/phpunit": "^5.5 || ^6.0"
"phpunit/phpunit": "^8.2.3"
},
"autoload": {
"psr-4": { "Coinpaprika\\": "src/Coinpaprika/" }
Expand Down
28 changes: 22 additions & 6 deletions src/Coinpaprika/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Coinpaprika;

use Coinpaprika\Exception\InvalidResponseException;
use Coinpaprika\Exception\PaymentRequiredException;
use Coinpaprika\Exception\RateLimitExceededException;
use Coinpaprika\Exception\ResponseErrorException;
use Coinpaprika\Http\Request;
Expand All @@ -28,6 +29,7 @@ class Client
{
const BASE_URL = 'https://api.coinpaprika.com/%ver%/';

const PRO_BASE_URL = 'https://api-pro.coinpaprika.com/%ver%/';

/**
* @var string
Expand All @@ -47,7 +49,8 @@ class Client
/**
* @var string
*/
private $apiBaseUrl = 'https://api.coinpaprika.com';
private $apiKey;


/**
* Client constructor.
Expand All @@ -59,7 +62,7 @@ class Client
public function __construct(
string $cacheDir = null,
\GuzzleHttp\Client $httpClient = null,
string $apiBaseUrl = null
?string $apiKey = null
) {
$serializerBuilder = SerializerBuilder::create()
->addMetadataDir(__DIR__.'/Resource/config/serializer');
Expand All @@ -77,9 +80,7 @@ public function __construct(

$this->httpClient = $httpClient;

if ($apiBaseUrl) {
$this->apiBaseUrl = $apiBaseUrl;
}
$this->apiKey = $apiKey;
}

/**
Expand Down Expand Up @@ -226,6 +227,11 @@ public function getApiVersion(): string
return $this->apiVersion;
}

public function setApiKey(string $apiKey)
{
$this->apiKey = $apiKey;
}

/**
* Get the endpoint URL.
*
Expand All @@ -235,7 +241,9 @@ public function getApiVersion(): string
*/
protected function getEndpointUrl(string $endpoint): string
{
return str_replace('%ver%', $this->getApiVersion(), $this->apiBaseUrl.'/%ver%/').$endpoint;
$baseUrl = $this->apiKey ? static::PRO_BASE_URL : static::BASE_URL;

return str_replace('%ver%', $this->getApiVersion(), $baseUrl).$endpoint;
}

/**
Expand All @@ -258,6 +266,10 @@ protected function sendRequest(
'User-Agent' => 'Coinpaprika API Client - PHP'
];

if ($this->apiKey) {
$defaultHeaders['Authorization'] = $this->apiKey;
}

if (Request::METHOD_GET === $method) {
$params = http_build_query($params);

Expand Down Expand Up @@ -291,6 +303,10 @@ protected function validateResponse(ResponseInterface $response): void
throw new RateLimitExceededException('Response code from API 429. Rate limit exceeded.');
}

if ($statusCode === 403) {
throw new PaymentRequiredException('Payment required for this api key.');
}

// check for errors
if ($statusCode >= 400 && $statusCode <= 500) {

Expand Down
8 changes: 8 additions & 0 deletions src/Coinpaprika/Exception/PaymentRequiredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Coinpaprika\Exception;

class PaymentRequiredException extends \Exception
{
protected $code = 402;
}

0 comments on commit 0dfd8ec

Please sign in to comment.