src/Services/ApiConsumer.php line 152

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Utils\EAS256CBC;
  4. use Psr\Cache\InvalidArgumentException;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Contracts\Cache\CacheInterface;
  8. use Symfony\Contracts\Cache\ItemInterface;
  9. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  10. use Symfony\Contracts\HttpClient\HttpClientInterface;
  11. class ApiConsumer
  12. {
  13.     protected HttpClientInterface $client;
  14.     protected CacheInterface $cache;
  15.     protected EAS256CBC      $EAS256CBC;
  16.     private LoggerInterface  $logger;
  17.     private LoggerInterface  $errorLogger;
  18.     private $cliente 'tomatsa';
  19.     private $pass    'ETCHEBARNE';
  20.     protected static string $apiUrl 'http://10.10.10.8/whMainWebApi/';
  21.     public static string $key "hUf3eof71VCa8IIjFlNewZ73yBWjckdX";
  22.     public function __construct(CacheInterface $cacheEAS256CBC $EAS256CBCHttpClientInterface $clientLoggerInterface $loggerLoggerInterface $errorLogger)
  23.     {
  24.         $this->client    $client;
  25.         $this->cache     $cache;
  26.         $this->EAS256CBC $EAS256CBC;
  27.         $this->logger    $logger;
  28.         $this->errorLogger $errorLogger;
  29.     }
  30.     public function getDataApi($service$parameters$json false$expire 3600)
  31.     {
  32.         if ($service == 'getPedidoSeguimiento2') {
  33.             $cache_key md5(uniqid());
  34.         } else {
  35.             $cache_key md5($service '-' json_encode($parameters));
  36.         }
  37.         try {
  38.             if ($this->cache->hasItem($cache_key)) {
  39.                 return $this->cache->getItem($cache_key)->get();
  40.             }
  41.             $startTime microtime(true);
  42.             $response $this->client->request('POST'self::$apiUrl $service, [
  43.                 'headers' => [
  44.                     'accept' => '*/*'
  45.                 ],
  46.                 'body'    => $parameters,
  47.             ]);
  48.             $content $json json_decode($response->getContent()) : $response->getContent();
  49.             $durationMs = (microtime(true) - $startTime) * 1000;
  50.             $this->logger->info('API response time', [
  51.                 'service' => $service,
  52.                 'duration_ms' => round($durationMs2),
  53.                 'parameters' => $parameters,
  54.             ]);
  55.             $cacheItem $this->cache->getItem($cache_key);
  56.             $cacheItem->set($content);
  57.             $cacheItem->expiresAfter($expire);
  58.             $this->cache->save($cacheItem);
  59. //            dump(date('d-m-Y H:i:s'));
  60. //            dump($content);
  61.             return $content;
  62.         } catch (\Exception $e) {
  63.             $this->errorLogger->error('API error', [
  64.                 'service' => $service,
  65.                 'parameters' => $parameters,
  66.                 'exception' => $e,
  67.             ]);
  68.             return new Response('Error al consumir la API: ' $e->getMessage(), 500);
  69.         } catch (TransportExceptionInterface|InvalidArgumentException $e) {
  70.             $this->errorLogger->error('API error', [
  71.                 'service' => $service,
  72.                 'parameters' => $parameters,
  73.                 'exception' => $e,
  74.             ]);
  75.             return new Response('Error al consumir la API: ' $e->getMessage(), 500);
  76.         }
  77.     }
  78.     public function getLoginToken(): string
  79.     {
  80.         return $this->getDataApi('getLoginToken', [
  81.             'TokenApi' => $this->EAS256CBC->encrypt('dm#dm'self::$key)
  82.         ]);
  83.     }
  84.     public function getWorkToken()
  85.     {
  86.         return $this->getDataApi('LoginByToken', [
  87.             'LoginToken' => $this->getLoginToken()
  88.         ]);
  89.     }
  90.     public function getClienteValidar($userPasswordHash)
  91.     {
  92.         return $this->getDataApi('getClienteValidar', [
  93.             'WorkToken' => $this->getWorkToken(),
  94.             'UserToken' => $userPasswordHash,
  95.         ],                       true);
  96.     }
  97.     //SOLO DE PRUEBA
  98.     public function getTokenUserPass()
  99.     {
  100.         return $this->getDataApi('getTokenUserPass', [
  101.             'WorkToken' => $this->getWorkToken(),
  102.             'usuario'   => $this->cliente$this->cliente,
  103.             'password'  => $this->pass,
  104.         ]);
  105.     }
  106.     public function getclientList($userPasswordHash)
  107.     {
  108.         return $this->getDataApi('getClienteListado', [
  109.             'WorkToken' => $this->getWorkToken(),
  110.             'UserToken' => $userPasswordHash,
  111.         ],                       true);
  112.     }
  113.     public function getclientInv($id$userPasswordHash)
  114.     {
  115.         return $this->getDataApi('getInventario', [
  116.             'WorkToken'   => $this->getWorkToken(),
  117.             'UserToken'   => $userPasswordHash,
  118.             'cod_cliente' => $id,
  119.         ],                       true600);
  120.     }
  121.     public function getImportsTasksList($id$userPasswordHash)
  122.     {
  123.         return $this->getDataApi('getImportsTasksList', [
  124.             'WorkToken'   => $this->getWorkToken(),
  125.             'UserToken'   => $userPasswordHash,
  126.             'cod_cliente' => $id,
  127.         ],                       true600);
  128.     }
  129.     public function getImportsTasksFormats($id$userPasswordHash)
  130.     {
  131.         return $this->getDataApi('getImportsTasksFormats', [
  132.             'WorkToken' => $this->getWorkToken(),
  133.             'UserToken' => $userPasswordHash,
  134.             'id_Tarea'  => $id,
  135.         ],                       true600);
  136.     }
  137.     public function getPedidoSeguimiento($cod_cliente$userPasswordHash)
  138.     {
  139.         return $this->getDataApi('getPedidoSeguimiento', [
  140.             'WorkToken'   => $this->getWorkToken(),
  141.             'UserToken'   => $userPasswordHash,
  142.             'cod_cliente' => $cod_cliente,
  143.         ],                       true0);
  144.     }
  145.     public function getPedidoSeguimientoDetalle($cod_cliente$order_id$userPasswordHash)
  146.     {
  147.         return $this->getDataApi('getPedidoSeguimientoDetalle', [
  148.             'WorkToken'   => $this->getWorkToken(),
  149.             'UserToken'   => $userPasswordHash,
  150.             'cod_cliente' => $cod_cliente,
  151.             'Pedido'      => $order_id,
  152.         ],                       true600);
  153.     }
  154.     public function getImportsTasksFile($userPasswordHash$cod_cliente$id_seguimiento$id_tarea$archivo_64)
  155.     {
  156.         dump([
  157.                  'WorkToken'      => $this->getWorkToken(),
  158.                  'UserToken'      => $userPasswordHash,
  159.                  'cod_cliente'    => $cod_cliente,
  160.                  'id_seguimiento' => (string)$id_seguimiento,
  161.                  'id_tarea'       => $id_tarea,
  162.                  'Extension'      => 'txt',
  163.                  'Archivo64'      => $archivo_64,
  164.              ]);
  165.         exit;
  166.         return $this->getDataApi('getImportsTasksFile', [
  167.             'WorkToken'      => $this->getWorkToken(),
  168.             'UserToken'      => $userPasswordHash,
  169.             'cod_cliente'    => $cod_cliente,
  170.             'id_seguimiento' => (string)$id_seguimiento,
  171.             'id_tarea'       => $id_tarea,
  172.             'Extension'      => 'txt',
  173.             'Archivo64'      => $archivo_64,
  174.         ],                       false0);
  175.     }
  176.     public function getImportsTasksTracking($id_seguimiento$BatchId$userPasswordHash)
  177.     {
  178.         return $this->getDataApi('getImportsTasksTracking', [
  179.             'WorkToken'      => $this->getWorkToken(),
  180.             'UserToken'      => $userPasswordHash,
  181.             'id_seguimiento' => $id_seguimiento,
  182.             'BatchId'        => $BatchId,
  183.         ],                       false0);
  184.     }
  185.     public function getFacturaResumen($userPasswordHash)
  186.     {
  187.         return $this->getDataApi('getFacturaResumen', [
  188.             'WorkToken' => $this->getWorkToken(),
  189.             'UserToken' => $userPasswordHash,
  190.         ],                       true);
  191.     }
  192.     public function getFacturaPDF($userPasswordHash$folio)
  193.     {
  194.         return $this->getDataApi('getFacturaPDF', [
  195.             'WorkToken' => $this->getWorkToken(),
  196.             'UserToken' => $userPasswordHash,
  197.             'Folio'     => $folio,
  198.         ],                       false);
  199.     }
  200.     public function getPowerBIList($userPasswordHash)
  201.     {
  202.         return $this->getDataApi('getPowerBiList', [
  203.             'WorkToken' => $this->getWorkToken(),
  204.             'UserToken' => $userPasswordHash,
  205.         ],                       true);
  206.     }
  207. }