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

Commit 0491005

Browse files
committedAug 24, 2017
chore: prepare project
0 parents  commit 0491005

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed
 

‎README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
3+
# How to install
4+
5+
```shell
6+
composer require glpi-project/rest-api-client
7+
```
8+
9+
# How to use
10+
11+
```php
12+
use GlpiProject\API\Rest\Client;
13+
use GuzzleHttp\Client as HttpClient;
14+
15+
$client = new Client.php(new HttpClient(['base_uri' => "http://localhost/glpi/apirest.php/"]));

‎composer.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name" : "glpi-project/rest-api-client",
3+
"type" : "library",
4+
"description" : "Client for GLPI rest API",
5+
"keywords" : [
6+
"GLPI",
7+
"rest",
8+
"api",
9+
"client"
10+
],
11+
"homepage" : "https://github.com/glpi-project/rest-api-client",
12+
"license" : "GPL-3.0+",
13+
"authors" : [{
14+
"name" : "Thierry Bugier",
15+
"email" : "tbugier@teclib.com",
16+
"role" : "Developer"
17+
}
18+
],
19+
"require" : {
20+
"php" : ">=5.6.0",
21+
"guzzlehttp/guzzle": "^6.3"
22+
},
23+
"require-dev" : {
24+
"atoum/atoum" : "^3.1"
25+
},
26+
"autoload" : {
27+
"psr-4" : {
28+
"GlpiProject\\API\\Rest\\" : "src/GlpiProject/API/Rest/"
29+
}
30+
}
31+
}

‎src/GlpiProject/API/Rest/Client.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace GlpiProject\API\Rest;
3+
use GuzzleHttp\Client as HttpClient;
4+
use Exception;
5+
6+
class Client {
7+
8+
/** @var HttpClient instance of the HTTP client */
9+
private $httpClient = null;
10+
11+
/** @var string URL to the GLPI API rest */
12+
private $url;
13+
14+
/** @var string Session token obtained after initSession() */
15+
private $sessionToken = '';
16+
17+
public function __construct(HttpClient $httpClient, $url) {
18+
$this->httpClient = $httpClient;
19+
$this->url = $url;
20+
}
21+
22+
public function initSessionByCredentials($user, $password) {
23+
$response = $this->httpClient->get($this->url . 'initSession', ['auth' => [$user, $password]]);
24+
if ($response->getStatusCode() != 200
25+
|| !$this->sessionToken = json_decode( (string) $response->getBody(), true)['session_token']) {
26+
throw new Exception("Cannot connect to api");
27+
} else {
28+
return true;
29+
}
30+
}
31+
32+
public function initSessionByUserToken($userToken) {
33+
$headers = $this->buildHeaders(['auth' => [$user, $password]]);
34+
$response = $this->httpClient->get($this->url . 'initSession', $headers);
35+
if ($response->getStatusCode() != 200
36+
|| !$this->sessionToken = json_decode( (string) $response->getBody(), true)['session_token']) {
37+
throw new Exception("Cannot connect to api");
38+
}
39+
}
40+
41+
/**
42+
* build a set of headers for the HTTP client by merging mandatory headers for rest API and provided ones
43+
*
44+
* @param array $headers
45+
*
46+
* @return array merged set of headers
47+
*/
48+
private function buildHeaders(array $headers) {
49+
$headers['Content-Type'] = 'application/json';
50+
return $headers;
51+
}
52+
}

‎tests/bootstrap.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
require __DIR__ . '/../vendor/autoload.php';
3+
4+
define('GLPI_URL', 'http://localhost/~dethegeek/glpi-flyvemdm-92/apirest.php/');

‎tests/unit/Client.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace GlpiProject\API\Rest\tests\units;
3+
use atoum;
4+
use GuzzleHttp\Client as HttpClient;
5+
6+
class Client extends atoum {
7+
8+
public function testInitSession() {
9+
$client = $this->newTestedInstance(new HttpClient(), GLPI_URL);
10+
$success = $client->initSessionByCredentials('glpi', 'glpi');
11+
$this->boolean($success)->isTrue();
12+
}
13+
14+
}

0 commit comments

Comments
 (0)