-
Notifications
You must be signed in to change notification settings - Fork 0
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
Crypto monitor josantonius #9
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,54 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
declare(strict_types=1); | ||
namespace PayCrypto; | ||
|
||
use PayCrypto\PayCrypto; | ||
use ScriptFUSION\Porter\Connector\Connector; | ||
use ScriptFUSION\Porter\Provider\Provider; | ||
|
||
class CryptoMonitor implements Provider | ||
{ | ||
public const EXCHANGE_API_ENDPOINT = 'https://rest.coinapi.io/'; | ||
|
||
private $connector; | ||
|
||
private static $providers = [ | ||
'coinapi' => [ | ||
'url' => 'https://rest.coinapi.io/v1/', | ||
'specific' => 'exchangerate/%s/%s/', | ||
'all' => 'exchangerate/%s/', | ||
'assets' => 'assets/' | ||
] | ||
]; | ||
|
||
public function __construct(Connector $connector) | ||
{ | ||
$this->connector = $connector; | ||
} | ||
|
||
public static function buildExchangeApiUrl(string $url): string | ||
public static function buildExchangeApiUrl(PayCrypto $config) : string | ||
{ | ||
return self::EXCHANGE_API_ENDPOINT . $url; | ||
$url = self::$providers[$config->provider]['url']; | ||
|
||
$params = self::$providers[$config->provider][$config->type]; | ||
|
||
switch ($config->type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about enhancing this I think using the If we have the many types to do the different action, the |
||
case 'specific': | ||
$url .= sprintf($params, $config->base, $config->quote); | ||
break; | ||
case 'all': | ||
$url .= sprintf($params, $config->base); | ||
break; | ||
case 'assets': | ||
$url .= $params; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return $url; | ||
} | ||
|
||
public function getConnector(): Connector | ||
public function getConnector() : Connector | ||
{ | ||
return $this->connector; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
namespace PayCrypto; | ||
|
||
use Joomla\DI\Container; | ||
use PayCrypto\Connector\CryptoConnector; | ||
use PayCrypto\CryptoMonitor; | ||
use PayCrypto\Resource\GetAllRate; | ||
use PayCrypto\Resource\GetAssets; | ||
use PayCrypto\Resource\GetSpecificRate; | ||
use ScriptFUSION\Porter\Porter; | ||
use ScriptFUSION\Porter\Specification\ImportSpecification; | ||
|
||
class PayCrypto | ||
{ | ||
public $provider; | ||
|
||
public $apiKey; | ||
|
||
public $porter; | ||
|
||
public $base; | ||
|
||
public $quote; | ||
|
||
public function __construct(string $provider, string $apiKey) | ||
{ | ||
$this->provider = $provider; | ||
$this->apiKey = $apiKey; | ||
|
||
$this->porter = new Porter( | ||
$this->setContainer() | ||
); | ||
} | ||
|
||
public function getSpecificRate(string $base = 'BTC', string $quote = 'USD') : array | ||
{ | ||
$this->base = $base; | ||
$this->quote = $quote; | ||
$this->type = 'specific'; | ||
|
||
return $this->porter->importOne( | ||
new ImportSpecification( | ||
new GetSpecificRate($this) | ||
) | ||
); | ||
} | ||
|
||
public function getAllRate(string $base = 'BTC') : object | ||
{ | ||
$this->base = $base; | ||
$this->type = 'all'; | ||
|
||
return $this->porter->import( | ||
new ImportSpecification( | ||
new GetAllRate($this) | ||
) | ||
); | ||
} | ||
|
||
public function getAssets() : object | ||
{ | ||
$this->type = 'assets'; | ||
|
||
return $this->porter->import( | ||
new ImportSpecification( | ||
new GetAssets($this) | ||
) | ||
); | ||
} | ||
|
||
public function getConnector() : Connector | ||
{ | ||
return $this->connector; | ||
} | ||
|
||
private function setContainer() | ||
{ | ||
$container = new Container; | ||
|
||
return $container->set( | ||
CryptoMonitor::class, | ||
new CryptoMonitor( | ||
new CryptoConnector($this->apiKey) | ||
) | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
namespace PayCrypto\Resource; | ||
|
||
use PayCrypto\PayCrypto; | ||
use PayCrypto\CryptoMonitor; | ||
use PayCrypto\Collection\AllRatesRecord; | ||
use ScriptFUSION\Porter\Connector\ImportConnector; | ||
|
@@ -12,14 +13,11 @@ | |
|
||
class GetAllRate implements ProviderResource | ||
{ | ||
private $apiKey; | ||
public $config; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let How about using the private/protected And we can also add the |
||
|
||
private $base; | ||
|
||
public function __construct(string $apiKey, string $base = 'BTC') | ||
public function __construct(PayCrypto $config) | ||
{ | ||
$this->apiKey = $apiKey; | ||
$this->base = $base; | ||
$this->config = $config; | ||
} | ||
|
||
public function getProviderClassName(): string | ||
|
@@ -29,17 +27,14 @@ public function getProviderClassName(): string | |
|
||
public function fetch(ImportConnector $connector): \Iterator | ||
{ | ||
$response = \json_decode( | ||
(string) $connector->fetch( | ||
CryptoMonitor::buildExchangeApiUrl( | ||
sprintf("v1/exchangerate/%s", $this->base) | ||
) | ||
), | ||
true | ||
$response = (string) $connector->fetch( | ||
CryptoMonitor::buildExchangeApiUrl($this->config) | ||
); | ||
|
||
$rates = new \ArrayIterator($response['rates']); | ||
$data = json_decode($response, true); | ||
|
||
$rates = new \ArrayIterator($data['rates']); | ||
|
||
return new AllRatesRecord($rates, $this->base, count($rates), $this); | ||
return new AllRatesRecord($rates, $this->config->base, count($rates), $this); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this API key free-trial? If it's, I think it's fine to expose this key here.