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

Commit

Permalink
feat(client): added new methods to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
DIOHz0r authored and ajsb85 committed Oct 25, 2017
1 parent 9286c14 commit 8724c9b
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/GlpiProject/API/Rest/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Exception;
use GlpiProject\API\Rest\Exception\BadEndpointException;
use GlpiProject\API\Rest\Exception\InsufficientArgumentsException;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;

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

/**
* Prepare and send a request to the GLPI Api.
*
* @param $method
* @param $uri
* @param array $options
* @return mixed|null|\Psr\Http\Message\ResponseInterface
* @throws Exception
*/
public function request($method, $uri, array $options = []) {
$apiToken = $this->addTokens();
try {
if ($apiToken) {
$sessionHeaders = ['Session-Token' => $apiToken['Session-Token']];
if (key_exists('App-Token', $apiToken)) {
$sessionHeaders['App-Token'] = $apiToken['App-Token'];
}
$options = array_merge_recursive($options, ['headers' => $sessionHeaders]);
}
$response = $this->httpClient->request($method, $uri, $options);
return $response;
} catch (ClientException $e) {
$response = $e->getResponse();
/*$body = $response->getBody()->getContents();
$reasonPhrase = $response->getReasonPhrase() . (($body) ? ' ' . $body : '');*/
return $response;
} catch (RequestException $e) {
$hasResponse = $e->hasResponse();
$statusCode = ($hasResponse) ? $e->getResponse()->getStatusCode() : '500';
$contents = ($hasResponse) ? $e->getResponse()->getReasonPhrase() : 'Request Error';
throw new Exception($contents, $statusCode);
}
}

/**
* Return the current php $_SESSION.
* @return array
*/
public function getFullSession() {
$response = $this->request('get', 'getFullSession');
return ['statusCode' => $response->getStatusCode(), 'body' => json_decode($response->getBody()->getContents())];
}

/**
* Return the current $CFG_GLPI.
* @return array
*/
public function getGlpiConfig() {
$response = $this->request('get', 'getFullSession');
return ['statusCode' => $response->getStatusCode(), 'body' => json_decode($response->getBody()->getContents())];
}

/**
* Execute a HTTP request to the rest API
*
Expand Down

0 comments on commit 8724c9b

Please sign in to comment.