|
6 | 6 | use Exception;
|
7 | 7 | use GlpiProject\API\Rest\Exception\BadEndpointException;
|
8 | 8 | use GlpiProject\API\Rest\Exception\InsufficientArgumentsException;
|
| 9 | +use GuzzleHttp\Exception\ClientException; |
| 10 | +use GuzzleHttp\Exception\RequestException; |
9 | 11 |
|
10 | 12 | /**
|
11 | 13 | * @method Alert(string $method, array $input, array $params = [])
|
@@ -302,6 +304,58 @@ public function __call($name, $arguments) {
|
302 | 304 | return $this->doHttpRequest($method, $name, $params);
|
303 | 305 | }
|
304 | 306 |
|
| 307 | + /** |
| 308 | + * Prepare and send a request to the GLPI Api. |
| 309 | + * |
| 310 | + * @param $method |
| 311 | + * @param $uri |
| 312 | + * @param array $options |
| 313 | + * @return mixed|null|\Psr\Http\Message\ResponseInterface |
| 314 | + * @throws Exception |
| 315 | + */ |
| 316 | + public function request($method, $uri, array $options = []) { |
| 317 | + $apiToken = $this->addTokens(); |
| 318 | + try { |
| 319 | + if ($apiToken) { |
| 320 | + $sessionHeaders = ['Session-Token' => $apiToken['Session-Token']]; |
| 321 | + if (key_exists('App-Token', $apiToken)) { |
| 322 | + $sessionHeaders['App-Token'] = $apiToken['App-Token']; |
| 323 | + } |
| 324 | + $options = array_merge_recursive($options, ['headers' => $sessionHeaders]); |
| 325 | + } |
| 326 | + $response = $this->httpClient->request($method, $uri, $options); |
| 327 | + return $response; |
| 328 | + } catch (ClientException $e) { |
| 329 | + $response = $e->getResponse(); |
| 330 | + /*$body = $response->getBody()->getContents(); |
| 331 | + $reasonPhrase = $response->getReasonPhrase() . (($body) ? ' ' . $body : '');*/ |
| 332 | + return $response; |
| 333 | + } catch (RequestException $e) { |
| 334 | + $hasResponse = $e->hasResponse(); |
| 335 | + $statusCode = ($hasResponse) ? $e->getResponse()->getStatusCode() : '500'; |
| 336 | + $contents = ($hasResponse) ? $e->getResponse()->getReasonPhrase() : 'Request Error'; |
| 337 | + throw new Exception($contents, $statusCode); |
| 338 | + } |
| 339 | + } |
| 340 | + |
| 341 | + /** |
| 342 | + * Return the current php $_SESSION. |
| 343 | + * @return array |
| 344 | + */ |
| 345 | + public function getFullSession() { |
| 346 | + $response = $this->request('get', 'getFullSession'); |
| 347 | + return ['statusCode' => $response->getStatusCode(), 'body' => json_decode($response->getBody()->getContents())]; |
| 348 | + } |
| 349 | + |
| 350 | + /** |
| 351 | + * Return the current $CFG_GLPI. |
| 352 | + * @return array |
| 353 | + */ |
| 354 | + public function getGlpiConfig() { |
| 355 | + $response = $this->request('get', 'getFullSession'); |
| 356 | + return ['statusCode' => $response->getStatusCode(), 'body' => json_decode($response->getBody()->getContents())]; |
| 357 | + } |
| 358 | + |
305 | 359 | /**
|
306 | 360 | * Execute a HTTP request to the rest API
|
307 | 361 | *
|
|
0 commit comments