Mas logs y middlewares en Guzzle
This commit is contained in:
@ -19,15 +19,25 @@ return [
|
|||||||
$container->get('API_PASSWORD')
|
$container->get('API_PASSWORD')
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
GuzzleHttp\HandlerStack::class => function(ContainerInterface $container) {
|
||||||
|
$stack = new GuzzleHttp\HandlerStack();
|
||||||
|
$stack->setHandler($container->get(GuzzleHttp\Handler\CurlHandler::class));
|
||||||
|
$stack->push(GuzzleHttp\Middleware::mapRequest(function(Psr\Http\Message\RequestInterface $request) use ($container) {
|
||||||
|
$login = $container->get(Incoviba\Service\Login::class);
|
||||||
|
return $request->withHeader('Authorization', "Bearer {$login->getKey($container->get('API_KEY'))}");
|
||||||
|
}));
|
||||||
|
$stack->push(GuzzleHttp\Middleware::mapRequest(function(Psr\Http\Message\RequestInterface $request) use ($container) {
|
||||||
|
if (!$request->hasHeader('Authorization')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $request;
|
||||||
|
}));
|
||||||
|
return $stack;
|
||||||
|
},
|
||||||
Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) {
|
Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) {
|
||||||
$login = $container->get(Incoviba\Service\Login::class);
|
|
||||||
return new GuzzleHttp\Client([
|
return new GuzzleHttp\Client([
|
||||||
'base_uri' => $container->get('API_URL'),
|
'base_uri' => $container->get('API_URL'),
|
||||||
'headers' => [
|
'handler' => $container->get(GuzzleHttp\HandlerStack::class),
|
||||||
'Authorization' => [
|
|
||||||
"Bearer {$login->getKey($container->get('API_KEY'))}"
|
|
||||||
]
|
|
||||||
]
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
@ -29,6 +29,7 @@ class BaseLoop extends Console\Command\Command
|
|||||||
|
|
||||||
$this->waitNextTimeout($output);
|
$this->waitNextTimeout($output);
|
||||||
|
|
||||||
|
$this->logger->info("Starting loop...");
|
||||||
$this->write($output, 'Starting loop...');
|
$this->write($output, 'Starting loop...');
|
||||||
while (true) {
|
while (true) {
|
||||||
$commands = $this->scheduleService->getPending();
|
$commands = $this->scheduleService->getPending();
|
||||||
|
@ -33,6 +33,7 @@ class Queue extends Command
|
|||||||
|
|
||||||
$jobs = $this->getJobs($output);
|
$jobs = $this->getJobs($output);
|
||||||
if (count($jobs) === 0) {
|
if (count($jobs) === 0) {
|
||||||
|
$this->logger->debug("No jobs to run");
|
||||||
return Console\Command\Command::SUCCESS;
|
return Console\Command\Command::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,6 +51,7 @@ class Queue extends Command
|
|||||||
$errors = 0;
|
$errors = 0;
|
||||||
foreach ($jobs as $job) {
|
foreach ($jobs as $job) {
|
||||||
if ($this->runJob($output, $job) === Console\Command\Command::FAILURE) {
|
if ($this->runJob($output, $job) === Console\Command\Command::FAILURE) {
|
||||||
|
$this->logger->error("Error running job: {$job}");
|
||||||
$errors ++;
|
$errors ++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ class Login
|
|||||||
|
|
||||||
public function login(): string
|
public function login(): string
|
||||||
{
|
{
|
||||||
|
$this->logger->info('Logging in');
|
||||||
$url = '/api/login';
|
$url = '/api/login';
|
||||||
try {
|
try {
|
||||||
$response = $this->client->request('POST', $url, [
|
$response = $this->client->request('POST', $url, [
|
||||||
@ -39,13 +40,19 @@ class Login
|
|||||||
]);
|
]);
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->logger->info('Logged in');
|
||||||
$body = $response->getBody()->getContents();
|
$body = $response->getBody()->getContents();
|
||||||
$data = json_decode($body, true);
|
$data = json_decode($body, true);
|
||||||
if (!key_exists('token', $data)) {
|
if (!key_exists('token', $data)) {
|
||||||
$this->logger->error('Token not found');
|
$this->logger->error('Token not found');
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
file_put_contents($this->tokenFilename, $data['token']);
|
$result = file_put_contents($this->tokenFilename, $data['token']);
|
||||||
|
if ($result === false) {
|
||||||
|
$this->logger->error('Failed to save token');
|
||||||
|
return '';
|
||||||
|
}
|
||||||
return $data['token'];
|
return $data['token'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user