Add command for testing api client #164
This commit is contained in:
parent
3ac8b8e53d
commit
005f5091af
4 changed files with 154 additions and 6 deletions
40
catalyst/api-bundle/Command/TestCommand.php
Normal file
40
catalyst/api-bundle/Command/TestCommand.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Catalyst\APIBundle\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
||||
|
||||
use Catalyst\APIBundle\Connector\Client as APIClient;
|
||||
|
||||
|
||||
class TestCommand extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('api:test-connector')
|
||||
->setDescription('Test API connector.')
|
||||
->setHelp('Test API Connector.')
|
||||
->addArgument('protocol', InputArgument::REQUIRED, 'protocol')
|
||||
->addArgument('server', InputArgument::REQUIRED, 'server')
|
||||
->addArgument('api_key', InputArgument::REQUIRED, 'api_key')
|
||||
->addArgument('secret_key', InputArgument::REQUIRED, 'secret_key');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$protocol = $input->getArgument('protocol');
|
||||
$server = $input->getArgument('server');
|
||||
$api_key = $input->getArgument('api_key');
|
||||
$secret_key = $input->getArgument('secret_key');
|
||||
|
||||
$api = new APIClient($server, $api_key, $secret_key);
|
||||
$api->setProtocol($protocol);
|
||||
|
||||
$api->get('/capi/test');
|
||||
}
|
||||
}
|
||||
109
catalyst/api-bundle/Connector/Client.php
Normal file
109
catalyst/api-bundle/Connector/Client.php
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
namespace Catalyst\APIBundle\Connector;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class Client
|
||||
{
|
||||
const HEADER_API_KEY = 'X-Cata-API-Key';
|
||||
const HEADER_SIGNATURE = 'X-Cata-Signature';
|
||||
const HEADER_DATE = 'X-Cata-Date';
|
||||
|
||||
const DATE_FORMAT = 'D, d M Y H:i:s T';
|
||||
|
||||
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36';
|
||||
|
||||
protected $protocol;
|
||||
protected $server;
|
||||
protected $port;
|
||||
protected $api_key;
|
||||
protected $secret_key;
|
||||
|
||||
protected $curl;
|
||||
|
||||
public function __construct($server, $api_key, $secret_key)
|
||||
{
|
||||
$this->protocol = 'https';
|
||||
$this->port = null;
|
||||
|
||||
$this->server = $server;
|
||||
$this->api_key = $api_key;
|
||||
$this->secret_key = $secret_key;
|
||||
$this->curl = curl_init();
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
curl_close($this->curl);
|
||||
}
|
||||
|
||||
public function setProtocol($protocol)
|
||||
{
|
||||
if ($protocol != 'http' && $protocol != 'https')
|
||||
return $this;
|
||||
|
||||
$this->protocol = $protocol;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get($url, $params = [])
|
||||
{
|
||||
$date = new DateTime();
|
||||
$date_string = $date->format(self::DATE_FORMAT);
|
||||
|
||||
$headers = $this->generateHeaders('GET', $url, $date_string);
|
||||
|
||||
// build url
|
||||
if ($this->port == null)
|
||||
$full_url = $this->protocol . '://' . $this->server . $url;
|
||||
else
|
||||
$full_url = $this->protocol . '://' . $this->server . ':' . $this->port . $url;
|
||||
|
||||
error_log($full_url);
|
||||
|
||||
// curl
|
||||
curl_setopt($this->curl, CURLOPT_VERBOSE, true);
|
||||
curl_setopt($this->curl, CURLOPT_URL, $full_url);
|
||||
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($this->curl, CURLOPT_USERAGENT, self::USER_AGENT);
|
||||
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($this->curl, CURLOPT_TIMEOUT, 0);
|
||||
|
||||
$res = curl_exec($this->curl);
|
||||
error_log($res);
|
||||
}
|
||||
|
||||
public function post($params)
|
||||
{
|
||||
}
|
||||
|
||||
protected function generateSignature($method, $url, $date_string)
|
||||
{
|
||||
$creds = [
|
||||
$method,
|
||||
$url,
|
||||
$date_string,
|
||||
$this->secret_key,
|
||||
];
|
||||
$sig_source = implode('|', $creds);
|
||||
|
||||
$raw_sig = hash_hmac('sha1', $sig_source, $this->secret_key, true);
|
||||
$enc_sig = base64_encode($raw_sig);
|
||||
|
||||
return $enc_sig;
|
||||
}
|
||||
|
||||
protected function generateHeaders($method, $url, $date_string)
|
||||
{
|
||||
$sig = $this->generateSignature($method, $url, $date_string);
|
||||
|
||||
$headers = [
|
||||
self::HEADER_API_KEY . ': ' . $this->api_key,
|
||||
self::HEADER_SIGNATURE . ': ' . $sig,
|
||||
self::HEADER_DATE . ': ' . $date_string,
|
||||
];
|
||||
|
||||
return $headers;
|
||||
}
|
||||
}
|
||||
|
|
@ -35,11 +35,6 @@ class APIKeyAuthenticator implements SimplePreAuthenticatorInterface, Authentica
|
|||
$this->em = $em;
|
||||
}
|
||||
|
||||
protected function getSecretKey($api_key)
|
||||
{
|
||||
return 'sldkfjlksdjflksdjflksdjflsjf';
|
||||
}
|
||||
|
||||
protected function validateSignature($creds, $secret_key)
|
||||
{
|
||||
$elements = [
|
||||
|
|
|
|||
|
|
@ -87,4 +87,8 @@ services:
|
|||
Catalyst\APIBundle\Command\UserCreateCommand:
|
||||
arguments:
|
||||
$em: "@doctrine.orm.entity_manager"
|
||||
tags: ['console.command']
|
||||
tags: ['console.command']
|
||||
|
||||
Catalyst\APIBundle\Command\TestCommand:
|
||||
tags: ['console.command']
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue