Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pro plan option #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ The preferred way to install this extension is through [composer](http://getcomp

Either run

```bash
$ composer require codenix-sv/coingecko-api
Add in repositories section
```json
{
"type": "vcs",
"url": "https://github.com/LborV/coingecko-api"
}
```
or add

And in require section
```json
"codenix-sv/coingecko-api": "^1.0"
"codenix-sv/coingecko-api": "dev-pro-plan"
```
## Basic usage

Expand All @@ -54,6 +57,14 @@ $response = $client->getLastResponse();
$headers = $response->getHeaders();
```

## Using PRO Plan
```php
use Codenixsv\CoinGeckoApi\CoinGeckoClient;

$client = new CoinGeckoClient(null, 'YOUR API KEY');
$data = $client->ping();
```

## Available methods

### Ping
Expand Down
5 changes: 5 additions & 0 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function __construct(CoinGeckoClient $client)
*/
public function get(string $uri, array $query = []): array
{
$apiKey = $this->client->getApiKey();
if(!empty($apiKey)) {
$query['x_cg_pro_api_key'] = $apiKey;
}

$response = $this->client->getHttpClient()->request('GET', '/api/' . $this->version
. $uri, ['query' => $query]);
$this->client->setLastResponse($response);
Expand Down
21 changes: 19 additions & 2 deletions src/CoinGeckoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,33 @@
class CoinGeckoClient
{
protected const BASE_URI = 'https://api.coingecko.com';
protected const BASE_URI_PRO_PLAN = 'https://pro-api.coingecko.com';

/** @var Client */
private $httpClient;

/** @var null|string */
private $apiKey = null;

/** @var ResponseInterface|null */
protected $lastResponse;

public function __construct(?Client $client = null)
public function __construct(?Client $client = null, ?string $apiKey = null)
{
$this->httpClient = $client ?: new Client(['base_uri' => self::BASE_URI]);
if(!empty($apiKey)) {
$this->setApiKey($apiKey);
}

$this->httpClient = $client ?: new Client(['base_uri' => $apiKey ? self::BASE_URI_PRO_PLAN : self::BASE_URI]);
}

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

public function getApiKey(): ?string {
return $this->apiKey;
}

public function getHttpClient(): Client
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/TransformResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Codenixsv\CoinGeckoApi\Exception;
namespace Codenixsv\CoinGeckoApi\Exceptions;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/Message/ResponseTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Codenixsv\CoinGeckoApi\Message;

use Codenixsv\CoinGeckoApi\Exception\TransformResponseException;
use Codenixsv\CoinGeckoApi\Exceptions\TransformResponseException;
use Psr\Http\Message\ResponseInterface;
use Exception;

Expand Down
46 changes: 46 additions & 0 deletions tests/Api/ProPlanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Codenixsv\CoinGeckoApi\Tests\Api;

use Codenixsv\CoinGeckoApi\Api\Coins;
use Codenixsv\CoinGeckoApi\CoinGeckoClient;

class ProPlanTest extends ApiTestCase
{
public function testGetApiKey()
{
$client = $this->createClient();
$this->assertEquals($client->getApiKey(), 'test');
}

public function testSetApiKey()
{
$client = $this->createClient();
$client->setApiKey('test2');

$this->assertEquals($client->getApiKey(), 'test2');
}

public function testGetStatusUpdates()
{
$this->createApi()->getStatusUpdates('0x');

$request = $this->getLastRequest();
$this->assertEquals(
'/api/v3/coins/0x/status_updates?x_cg_pro_api_key=test',
$request->getUri()->__toString()
);
}

private function createApi(): Coins
{
return new Coins($this->createClient());
}

private function createClient(): CoinGeckoClient
{
return new CoinGeckoClient($this->getMockClient(), 'test');
}
}
2 changes: 1 addition & 1 deletion tests/Message/ResponseTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Codenixsv\CoinGeckoApi\Tests\Message;

use Codenixsv\CoinGeckoApi\Exception\TransformResponseException;
use Codenixsv\CoinGeckoApi\Exceptions\TransformResponseException;
use Codenixsv\CoinGeckoApi\Message\ResponseTransformer;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
Expand Down