Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adrifkat committed Aug 24, 2021
0 parents commit 15ee1c8
Show file tree
Hide file tree
Showing 15 changed files with 623 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
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.
36 changes: 36 additions & 0 deletions README.md
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
37 changes: 37 additions & 0 deletions composer.json
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
}
38 changes: 38 additions & 0 deletions src/Client.php
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);
}
}
141 changes: 141 additions & 0 deletions src/Http/Request/AbstractRequest.php
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;
}
}
21 changes: 21 additions & 0 deletions src/Http/Request/PortfolioRequest.php
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();
}
}
Loading

0 comments on commit 15ee1c8

Please sign in to comment.