<?php
namespace App\Services;
use App\Utils\EAS256CBC;
use Psr\Cache\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class ApiConsumer
{
protected HttpClientInterface $client;
protected CacheInterface $cache;
protected EAS256CBC $EAS256CBC;
private LoggerInterface $logger;
private LoggerInterface $errorLogger;
private $cliente = 'tomatsa';
private $pass = 'ETCHEBARNE';
protected static string $apiUrl = 'http://10.10.10.8/whMainWebApi/';
public static string $key = "hUf3eof71VCa8IIjFlNewZ73yBWjckdX";
public function __construct(CacheInterface $cache, EAS256CBC $EAS256CBC, HttpClientInterface $client, LoggerInterface $logger, LoggerInterface $errorLogger)
{
$this->client = $client;
$this->cache = $cache;
$this->EAS256CBC = $EAS256CBC;
$this->logger = $logger;
$this->errorLogger = $errorLogger;
}
public function getDataApi($service, $parameters, $json = false, $expire = 3600)
{
if ($service == 'getPedidoSeguimiento2') {
$cache_key = md5(uniqid());
} else {
$cache_key = md5($service . '-' . json_encode($parameters));
}
try {
if ($this->cache->hasItem($cache_key)) {
return $this->cache->getItem($cache_key)->get();
}
$startTime = microtime(true);
$response = $this->client->request('POST', self::$apiUrl . $service, [
'headers' => [
'accept' => '*/*'
],
'body' => $parameters,
]);
$content = $json ? json_decode($response->getContent()) : $response->getContent();
$durationMs = (microtime(true) - $startTime) * 1000;
$this->logger->info('API response time', [
'service' => $service,
'duration_ms' => round($durationMs, 2),
'parameters' => $parameters,
]);
$cacheItem = $this->cache->getItem($cache_key);
$cacheItem->set($content);
$cacheItem->expiresAfter($expire);
$this->cache->save($cacheItem);
// dump(date('d-m-Y H:i:s'));
// dump($content);
return $content;
} catch (\Exception $e) {
$this->errorLogger->error('API error', [
'service' => $service,
'parameters' => $parameters,
'exception' => $e,
]);
return new Response('Error al consumir la API: ' . $e->getMessage(), 500);
} catch (TransportExceptionInterface|InvalidArgumentException $e) {
$this->errorLogger->error('API error', [
'service' => $service,
'parameters' => $parameters,
'exception' => $e,
]);
return new Response('Error al consumir la API: ' . $e->getMessage(), 500);
}
}
public function getLoginToken(): string
{
return $this->getDataApi('getLoginToken', [
'TokenApi' => $this->EAS256CBC->encrypt('dm#dm', self::$key)
]);
}
public function getWorkToken()
{
return $this->getDataApi('LoginByToken', [
'LoginToken' => $this->getLoginToken()
]);
}
public function getClienteValidar($userPasswordHash)
{
return $this->getDataApi('getClienteValidar', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
], true);
}
//SOLO DE PRUEBA
public function getTokenUserPass()
{
return $this->getDataApi('getTokenUserPass', [
'WorkToken' => $this->getWorkToken(),
'usuario' => $this->cliente, $this->cliente,
'password' => $this->pass,
]);
}
public function getclientList($userPasswordHash)
{
return $this->getDataApi('getClienteListado', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
], true);
}
public function getclientInv($id, $userPasswordHash)
{
return $this->getDataApi('getInventario', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
'cod_cliente' => $id,
], true, 600);
}
public function getImportsTasksList($id, $userPasswordHash)
{
return $this->getDataApi('getImportsTasksList', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
'cod_cliente' => $id,
], true, 600);
}
public function getImportsTasksFormats($id, $userPasswordHash)
{
return $this->getDataApi('getImportsTasksFormats', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
'id_Tarea' => $id,
], true, 600);
}
public function getPedidoSeguimiento($cod_cliente, $userPasswordHash)
{
return $this->getDataApi('getPedidoSeguimiento', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
'cod_cliente' => $cod_cliente,
], true, 0);
}
public function getPedidoSeguimientoDetalle($cod_cliente, $order_id, $userPasswordHash)
{
return $this->getDataApi('getPedidoSeguimientoDetalle', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
'cod_cliente' => $cod_cliente,
'Pedido' => $order_id,
], true, 600);
}
public function getImportsTasksFile($userPasswordHash, $cod_cliente, $id_seguimiento, $id_tarea, $archivo_64)
{
dump([
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
'cod_cliente' => $cod_cliente,
'id_seguimiento' => (string)$id_seguimiento,
'id_tarea' => $id_tarea,
'Extension' => 'txt',
'Archivo64' => $archivo_64,
]);
exit;
return $this->getDataApi('getImportsTasksFile', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
'cod_cliente' => $cod_cliente,
'id_seguimiento' => (string)$id_seguimiento,
'id_tarea' => $id_tarea,
'Extension' => 'txt',
'Archivo64' => $archivo_64,
], false, 0);
}
public function getImportsTasksTracking($id_seguimiento, $BatchId, $userPasswordHash)
{
return $this->getDataApi('getImportsTasksTracking', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
'id_seguimiento' => $id_seguimiento,
'BatchId' => $BatchId,
], false, 0);
}
public function getFacturaResumen($userPasswordHash)
{
return $this->getDataApi('getFacturaResumen', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
], true);
}
public function getFacturaPDF($userPasswordHash, $folio)
{
return $this->getDataApi('getFacturaPDF', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
'Folio' => $folio,
], false);
}
public function getPowerBIList($userPasswordHash)
{
return $this->getDataApi('getPowerBiList', [
'WorkToken' => $this->getWorkToken(),
'UserToken' => $userPasswordHash,
], true);
}
}