Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 8724c9b

Browse files
DIOHz0rajsb85
authored andcommitted
feat(client): added new methods to the client
1 parent 9286c14 commit 8724c9b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/GlpiProject/API/Rest/Client.php

+54
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Exception;
77
use GlpiProject\API\Rest\Exception\BadEndpointException;
88
use GlpiProject\API\Rest\Exception\InsufficientArgumentsException;
9+
use GuzzleHttp\Exception\ClientException;
10+
use GuzzleHttp\Exception\RequestException;
911

1012
/**
1113
* @method Alert(string $method, array $input, array $params = [])
@@ -302,6 +304,58 @@ public function __call($name, $arguments) {
302304
return $this->doHttpRequest($method, $name, $params);
303305
}
304306

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+
305359
/**
306360
* Execute a HTTP request to the rest API
307361
*

0 commit comments

Comments
 (0)