@@ -37,7 +37,7 @@ class ApiClient
3737 *
3838 * @var string
3939 */
40- private $ api_url = 'https://bhexpress.cl ' ;
40+ private $ api_url = 'https://app. bhexpress.cl ' ;
4141
4242 /**
4343 * El prefijo para las rutas de la API.
@@ -89,9 +89,9 @@ class ApiClient
8989 * @param string|null $url URL base de la API.
9090 */
9191 public function __construct (
92- string $ token = null ,
93- string $ rut = null ,
94- string $ url = null
92+ ? string $ token = null ,
93+ ? string $ rut = null ,
94+ ? string $ url = null
9595 ) {
9696 $ this ->api_token = $ token ?: $ this ->env ('BHEXPRESS_API_TOKEN ' );
9797 if (!$ this ->api_token ) {
@@ -193,7 +193,7 @@ public function getBody(): string
193193 * @return array El cuerpo de la respuesta HTTP decodificado como un arreglo.
194194 * @throws ApiException Si no hay respuesta previa o el cuerpo no se puede decodificar.
195195 */
196- public function getBodyDecoded (): mixed
196+ public function getBodyDecoded (): ? array
197197 {
198198 $ decodedBody = json_decode (
199199 json: $ this ->getBody (),
@@ -386,7 +386,7 @@ public function consume(
386386 string $ resource ,
387387 array $ data = [],
388388 array $ headers = [],
389- string $ method = null ,
389+ ? string $ method = null ,
390390 array $ options = []
391391 ): static {
392392 $ this ->last_response = null ;
@@ -420,20 +420,29 @@ public function consume(
420420 $ options [\GuzzleHttp \RequestOptions::JSON ] = $ data ;
421421 }
422422
423- // realizar consulta HTTP
423+ // Ejecutar consulta al SII.
424424 try {
425425 $ this ->last_response = $ client ->request (
426426 method: $ method ,
427427 uri: $ this ->last_url ,
428428 options: $ options
429429 );
430- } catch (\GuzzleHttp \Exception \GuzzleException $ e ) {
430+ } catch (\GuzzleHttp \Exception \BadResponseException $ e ) {
431+ // Obtener la respuesta de la llamada.
431432 $ this ->last_response = $ e ->getResponse ();
433+
434+ // Si no es un error 401 con problema de sesión se lanza la excepción.
432435 $ this ->throwException ();
436+ } catch (\GuzzleHttp \Exception \GuzzleException $ e ) {
437+ throw new ApiException ('Error de conexión con el SII: ' . $ e ->getMessage (), 500 );
433438 }
439+
440+ // Si no se reintentó se lanza excepción por no ser código 200.
434441 if ($ this ->getLastResponse ()->getStatusCode () != 200 ) {
435442 $ this ->throwException ();
436443 }
444+
445+
437446 return $ this ;
438447 }
439448
@@ -462,7 +471,7 @@ private function getError(): object
462471 }
463472
464473 // Se maneja el caso donde no se encuentra un mensaje de error específico
465- if (!$ message || $ message === '' ) {
474+ if (!$ message ) {
466475 $ message = sprintf (
467476 '[BHExpress API] Código HTTP %d: %s ' ,
468477 $ code ,
0 commit comments