-
Notifications
You must be signed in to change notification settings - Fork 2
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
0 parents
commit 15ee1c8
Showing
15 changed files
with
623 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Rifkat Adeev | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,36 @@ | ||
# PHP unofficial client to CryptoPanic.com API | ||
|
||
[CryptoPanic.com](https://cryptopanic.com) is a news aggregator platform indicating impact on price and market for traders and cryptocurrency enthusiasts. Users can vote to mark important, bullish or bearish price signals. | ||
|
||
## Install | ||
|
||
composer require adrifkat/cryptopanic | ||
|
||
## Usage | ||
|
||
> Get your <AUTH_TOKEN> from the [cryptopanic API page](https://cryptopanic.com/about/api/). | ||
```php | ||
|
||
$client = new \Adrifkat\Cryptopanic\Client('<AUTH_TOKEN>'); | ||
$postsResponse = $client->getPostsRequest() | ||
->setCurrencies(['BTC', 'XRP']) | ||
->setFilter('bearish') | ||
->send(); | ||
|
||
$portfolioResponse = $client->send(); | ||
|
||
``` | ||
|
||
### Posts Request Methods | ||
|
||
- `setFilter(string $value)`: You can use any of UI filters using filter. Available values: rising|hot|bullish|bearish|important|saved|lol | ||
- `setCurrencies(array $currencies)`: Filter by currencies. Example: ['CURRENCY_CODE1', 'CURRENCY_CODE2'] | ||
- `setRegions(array $regions)`: Filter by region. Available regions: en (English), de (Deutsch), nl (Dutch), es (Español), fr (Français), it (Italiano), pt (Português), ru (Русский). Example: ['en', 'nl'] | ||
- `setKind(string $value)`: Filter by kind. Available values: news|media | ||
- `setFollowing(bool $following)`: Filter only "Following" feed - based on currencies you follow | ||
- `setPublic(bool $public))`: Enable public API | ||
|
||
# License | ||
|
||
MIT |
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,37 @@ | ||
{ | ||
"name": "adrifkat/cryptopanic", | ||
"description": "PHP unofficial client to cryptopanic.com API", | ||
"version": "1.0.0", | ||
"keywords": [ | ||
"cryptopanic", | ||
"crypto", | ||
"client", | ||
"api", | ||
"news", | ||
"aggregator", | ||
"cryptocurrencies" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Rifkat Adeev", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.4|^8.0", | ||
"netresearch/jsonmapper": "^4.0", | ||
"guzzlehttp/guzzle": "^7.2", | ||
"ext-json": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Adrifkat\\Cryptopanic\\": "src/" | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace Adrifkat\Cryptopanic; | ||
|
||
use Adrifkat\Cryptopanic\Http\Request\PortfolioRequest; | ||
use Adrifkat\Cryptopanic\Http\Request\PostsRequest; | ||
|
||
class Client | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private string $authToken; | ||
|
||
/** | ||
* @param string $authToken | ||
*/ | ||
public function __construct(string $authToken) | ||
{ | ||
$this->authToken = $authToken; | ||
} | ||
|
||
/** | ||
* @return PostsRequest | ||
*/ | ||
public function getPostsRequest(): PostsRequest | ||
{ | ||
return new PostsRequest($this->authToken); | ||
} | ||
|
||
/** | ||
* @return PortfolioRequest | ||
*/ | ||
public function getPortfolioRequest(): PortfolioRequest | ||
{ | ||
return new PortfolioRequest($this->authToken); | ||
} | ||
} |
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,141 @@ | ||
<?php | ||
|
||
namespace Adrifkat\Cryptopanic\Http\Request; | ||
|
||
use GuzzleHttp\Client; | ||
use Adrifkat\Cryptopanic\ResponseException; | ||
use Exception; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use JsonMapper; | ||
use JsonMapper_Exception; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
abstract class AbstractRequest | ||
{ | ||
const BASE_URL = 'https://cryptopanic.com/api/v1/'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected string $endpoint; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected array $params = []; | ||
|
||
/** | ||
* @param string $authToken | ||
*/ | ||
public function __construct(string $authToken) | ||
{ | ||
$this->addParam('auth_token', $authToken); | ||
} | ||
|
||
/** | ||
* @return mixed|object | ||
* @throws ResponseException | ||
* @throws GuzzleException | ||
* @throws JsonMapper_Exception | ||
*/ | ||
public function send() | ||
{ | ||
$mapper = new JsonMapper(); | ||
$mapper->bEnforceMapType = false; | ||
|
||
$response = $this->sendRequest(); | ||
|
||
if ($this->successful($response->getStatusCode())) { | ||
return $mapper->map(json_decode($response->getBody(), true), $this->getResponseInstance()); | ||
} | ||
|
||
$exception = new ResponseException(); | ||
$exception->response = $response; | ||
throw $exception; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getParams(): array | ||
{ | ||
return $this->params; | ||
} | ||
|
||
/** | ||
* @param $body | ||
*/ | ||
public function setParams($body) | ||
{ | ||
$this->params = $body; | ||
} | ||
|
||
/** | ||
* @param $key | ||
* @param $data | ||
*/ | ||
public function addParam($key, $data) | ||
{ | ||
$this->params[$key] = $data; | ||
} | ||
|
||
/** | ||
* @return ResponseInterface | ||
* @throws GuzzleException | ||
*/ | ||
protected function sendRequest(): ResponseInterface | ||
{ | ||
$client = new Client([ | ||
'base_uri' => $this->getRequestBaseUrl() | ||
]); | ||
|
||
return $client->request('GET', $this->getRequestUrl(), ['query' => $this->getParams()]); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getRequestUrl(): string | ||
{ | ||
$url = $this->getRequestBaseUrl() . $this->getEndpoint(); | ||
|
||
if ($this->getParams()) { | ||
$params = $this->getParams(); | ||
|
||
foreach ($params as $key => $param) { | ||
unset($params[$key]); | ||
$params['{' . $key . '}'] = $param; | ||
} | ||
|
||
$url = strtr($url, $params); | ||
} | ||
|
||
return $url; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getRequestBaseUrl(): string | ||
{ | ||
return self::BASE_URL; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getEndpoint(): string | ||
{ | ||
return $this->endpoint; | ||
} | ||
|
||
/** | ||
* Determine if the request was successful. | ||
* | ||
* @return bool | ||
*/ | ||
private function successful($status): bool | ||
{ | ||
return $status >= 200 && $status < 300; | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Adrifkat\Cryptopanic\Http\Request; | ||
|
||
use Adrifkat\Cryptopanic\Http\Response\PortfolioResponse; | ||
|
||
class PortfolioRequest extends AbstractRequest | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected string $endpoint = 'portfolio/'; | ||
|
||
/** | ||
* @return PortfolioResponse | ||
*/ | ||
public function getResponseInstance(): PortfolioResponse | ||
{ | ||
return new PortfolioResponse(); | ||
} | ||
} |
Oops, something went wrong.