forked from deblan/t411-console
Client token
This commit is contained in:
parent
553e8e871c
commit
2016a31c34
|
@ -1,3 +0,0 @@
|
|||
auth:
|
||||
uid:
|
||||
token:
|
|
@ -9,6 +9,8 @@ class Client
|
|||
{
|
||||
protected $client;
|
||||
|
||||
protected $token;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new GuzzleClient(array('base_url' => 'https://api.t411.me'));
|
||||
|
@ -17,6 +19,7 @@ class Client
|
|||
public function getAuthorization($username, $password)
|
||||
{
|
||||
return $this->post(
|
||||
false,
|
||||
'/auth',
|
||||
array(
|
||||
'body' => array(
|
||||
|
@ -27,23 +30,39 @@ class Client
|
|||
);
|
||||
}
|
||||
|
||||
public function setAuthorization($token)
|
||||
{
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
public function getCategoriesTree()
|
||||
{
|
||||
return $this->get('/categories/tree');
|
||||
return $this->get(true, '/categories/tree');
|
||||
}
|
||||
|
||||
public function get($uri, array $options = array())
|
||||
public function get($needAuthorization, $uri, array $options = array())
|
||||
{
|
||||
return $this->send('get', $uri, $options);
|
||||
return $this->send($needAuthorization, 'get', $uri, $options);
|
||||
}
|
||||
|
||||
public function post($uri, array $options = array())
|
||||
public function post($needAuthorization, $uri, array $options = array())
|
||||
{
|
||||
return $this->send('post', $uri, $options);
|
||||
return $this->send($needAuthorization, 'post', $uri, $options);
|
||||
}
|
||||
|
||||
protected function send($method, $uri, $options)
|
||||
protected function send($needAuthorization, $method, $uri, $options)
|
||||
{
|
||||
if ($needAuthorization) {
|
||||
$options = array_merge(
|
||||
$options,
|
||||
array(
|
||||
'headers' => array(
|
||||
'Authorization' => $this->token,
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
return new ClientResponse($this->client->{$method}($uri, $options));
|
||||
} catch (RequestException $e) {
|
||||
|
|
Binary file not shown.
|
@ -1,41 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Console\Command;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
||||
class AuthCommand extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('auth:login')
|
||||
->setDescription('Login on t411')
|
||||
->setHelp("The <info>%command.name%</info> ");
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$configLoader = new ConfigLoader();
|
||||
|
||||
if ($configLoader->configExists()) {
|
||||
$continue = $dialog->askConfirmation(
|
||||
$output,
|
||||
$dialog->getQuestion(
|
||||
'The configuration file already exists. Do you want to continue',
|
||||
'yes',
|
||||
'?'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
if (!$continue) {
|
||||
$output->writeln('Aborded.');
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue