2424namespace apigatewaycl \api_client ;
2525
2626use Psr \Http \Message \ResponseInterface ;
27+ use GuzzleHttp \Psr7 \Utils ;
2728
2829/**
2930 * Cliente de la API de API Gateway.
@@ -38,7 +39,7 @@ class ApiClient
3839 *
3940 * @var string
4041 */
41- private $ api_url = 'https://legacy .apigateway.cl ' ;
42+ private $ api_url = 'https://app .apigateway.cl ' ;
4243
4344 /**
4445 * El prefijo para las rutas de la API.
@@ -52,7 +53,7 @@ class ApiClient
5253 *
5354 * @var string
5455 */
55- private $ api_version = 'v1 ' ;
56+ private $ api_version = 'v2 ' ;
5657
5758 /**
5859 * El token de autenticación para la API.
@@ -75,6 +76,13 @@ class ApiClient
7576 */
7677 private $ last_response = null ;
7778
79+ /**
80+ * Forma de autentificar de las distintas api's.
81+ *
82+ * @var string|null
83+ */
84+ private $ authorization = null ;
85+
7886 /**
7987 * Constructor del cliente de la API.
8088 *
@@ -91,6 +99,14 @@ public function __construct(?string $token = null, ?string $url = null)
9199 $ this ->api_url = $ url ?: $ this ->env (
92100 'APIGATEWAY_API_URL '
93101 ) ?: $ this ->api_url ;
102+
103+ $ this ->authorization = 'Token ' ;
104+
105+ $ this ->api_version = $ this ->env ('VERSION ' );
106+ if ($ this ->api_version == 'v1 ' ){
107+ $ this ->api_url = 'https://legacy.apigateway.cl ' ;
108+ $ this ->authorization = 'Bearer ' ;
109+ }
94110 }
95111
96112 /**
@@ -117,6 +133,16 @@ public function setToken(string $token): static
117133 return $ this ;
118134 }
119135
136+ /**
137+ * Obtiene la última URL utilizada en la solicitud HTTP.
138+ *
139+ * @return string|null
140+ */
141+ public function getLastApiUrl (): string |null
142+ {
143+ return $ this ->api_url ;
144+ }
145+
120146 /**
121147 * Obtiene la última URL utilizada en la solicitud HTTP.
122148 *
@@ -373,7 +399,7 @@ public function consume(
373399
374400 // preparar cabeceras que se usarán
375401 $ options [\GuzzleHttp \RequestOptions::HEADERS ] = array_merge ([
376- 'Authorization ' => ' Bearer ' . $ this ->api_token ,
402+ 'Authorization ' => $ this -> authorization . ' ' . $ this ->api_token ,
377403 'Content-Type ' => 'application/json ' ,
378404 'Accept ' => 'application/json ' ,
379405 ], $ headers );
@@ -393,6 +419,12 @@ public function consume(
393419
394420 // Ejecutar consulta al SII.
395421 try {
422+ // echo $method;
423+ // echo $this->last_url;
424+ // echo $options;
425+ // fwrite(STDERR, $method);
426+ // fwrite(STDERR, $this->last_url);
427+ // fwrite(STDERR, json_encode($options));
396428 $ this ->last_response = $ client ->request (
397429 method: $ method ,
398430 uri: $ this ->last_url ,
@@ -426,6 +458,15 @@ public function consume(
426458 }
427459
428460 // Entregar respuesta (contenida en el mismo objeto del cliente).
461+
462+ $ response_body = (string ) $ this ->last_response ->getBody ();
463+ $ data = json_decode ($ response_body , true );
464+ if (json_last_error () === JSON_ERROR_NONE && isset ($ data ['data ' ])){
465+ $ data = $ data ['data ' ];
466+ $ response_data = Utils::streamFor (json_encode ($ data ));
467+ $ this ->last_response = $ this ->last_response ->withBody ($ response_data );
468+ }
469+
429470 return $ this ;
430471 }
431472
0 commit comments