From 5351b4bf195190572d2b43f794f8f7396dd572f4 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Pineau Date: Thu, 24 Aug 2017 00:18:26 +0200 Subject: [PATCH] feat: First endpoints --- README.md | 22 +- composer.json | 57 +-- composer.lock | 336 ++++++++++++++++++ src/GlpiProject/API/Rest/Client.php | 308 +++++++++++++++- .../Rest/Exception/BadEndpointException.php | 4 + .../InsufficientArgumentsException.php | 4 + tests/bootstrap.php | 7 +- tests/unit/Client.php | 21 ++ 8 files changed, 716 insertions(+), 43 deletions(-) create mode 100644 composer.lock create mode 100644 src/GlpiProject/API/Rest/Exception/BadEndpointException.php create mode 100644 src/GlpiProject/API/Rest/Exception/InsufficientArgumentsException.php diff --git a/README.md b/README.md index c28ba66..f201ba2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# +# Warning + +This library is WIP, created to develop Flyve MDM. # How to install @@ -12,4 +14,20 @@ composer require glpi-project/rest-api-client use GlpiProject\API\Rest\Client; use GuzzleHttp\Client as HttpClient; -$client = new Client.php(new HttpClient(['base_uri' => "http://localhost/glpi/apirest.php/"])); \ No newline at end of file +// Instanciate the API client +$client = new Client(new HttpClient(), 'http://localhost/glpi/apirest.php/'); + +// Authenticate +if (!$client->initSessionByCredentials('glpi', 'glpi')) { + die('failed to authenticate'); +} + +// The client handles the session token for you (app token not yet supported) + +// do something +try { + $response = $client->computer('post', ['name' => 'computer 0001']); +} catch (\Exception $e) { + // Handle here HTTP >= 400 +} +``` diff --git a/composer.json b/composer.json index c9c7932..059098f 100644 --- a/composer.json +++ b/composer.json @@ -1,31 +1,32 @@ { - "name" : "glpi-project/rest-api-client", - "type" : "library", - "description" : "Client for GLPI rest API", - "keywords" : [ - "GLPI", - "rest", + "name" : "glpi-project/rest-api-client", + "type" : "library", + "description" : "Client for GLPI rest API", + "keywords" : [ + "GLPI", + "rest", "api", "client" - ], - "homepage" : "https://github.com/glpi-project/rest-api-client", - "license" : "GPL-3.0+", - "authors" : [{ - "name" : "Thierry Bugier", - "email" : "tbugier@teclib.com", - "role" : "Developer" - } - ], - "require" : { - "php" : ">=5.6.0", - "guzzlehttp/guzzle": "^6.3" - }, - "require-dev" : { - "atoum/atoum" : "^3.1" - }, - "autoload" : { - "psr-4" : { - "GlpiProject\\API\\Rest\\" : "src/GlpiProject/API/Rest/" - } - } -} + ], + "homepage" : "https://github.com/glpi-project/rest-api-client", + "license" : "GPL-3.0+", + "authors" : [{ + "name" : "Thierry Bugier", + "email" : "tbugier@teclib.com", + "role" : "Developer" + } + ], + "require" : { + "php" : ">=5.6.0", + "guzzlehttp/guzzle" : "^6.3" + }, + "require-dev" : { + "atoum/atoum" : "^3.1" + }, + "autoload" : { + "psr-4" : { + "GlpiProject\\API\\Rest\\" : "src/GlpiProject/API/Rest/", + "GlpiProject\\API\\Rest\\Exception\\" : "src/GlpiProject/API/Rest/Exception/" + } + } +} \ No newline at end of file diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..4540011 --- /dev/null +++ b/composer.lock @@ -0,0 +1,336 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "75b53e36549d61c4a46db9d66f70fbf6", + "packages": [ + { + "name": "guzzlehttp/guzzle", + "version": "6.3.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "f4db5a78a5ea468d4831de7f0bf9d9415e348699" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/f4db5a78a5ea468d4831de7f0bf9d9415e348699", + "reference": "f4db5a78a5ea468d4831de7f0bf9d9415e348699", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.4", + "php": ">=5.5" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "^4.0 || ^5.0", + "psr/log": "^1.0" + }, + "suggest": { + "psr/log": "Required for using the Log middleware" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.2-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2017-06-22T18:50:49+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-12-20T10:07:11+00:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.4.2", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Schultze", + "homepage": "https://github.com/Tobion" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "request", + "response", + "stream", + "uri", + "url" + ], + "time": "2017-03-20T17:10:46+00:00" + }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" + } + ], + "packages-dev": [ + { + "name": "atoum/atoum", + "version": "3.1.1", + "source": { + "type": "git", + "url": "https://github.com/atoum/atoum.git", + "reference": "6af6b609a108fd2e63f2f42efbb4f0b799e6c710" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/atoum/atoum/zipball/6af6b609a108fd2e63f2f42efbb4f0b799e6c710", + "reference": "6af6b609a108fd2e63f2f42efbb4f0b799e6c710", + "shasum": "" + }, + "require": { + "ext-hash": "*", + "ext-json": "*", + "ext-session": "*", + "ext-tokenizer": "*", + "ext-xml": "*", + "php": "^5.6.0 || ^7.0.0" + }, + "replace": { + "mageekguy/atoum": "*" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "~1.12" + }, + "suggest": { + "atoum/stubs": "Provides IDE support (like autocompletion) for atoum", + "ext-mbstring": "Provides support for UTF-8 strings", + "ext-xdebug": "Provides code coverage report (>= 2.3)" + }, + "bin": [ + "bin/atoum" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "classmap": [ + "classes/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Frédéric Hardy", + "email": "frederic.hardy@atoum.org", + "homepage": "http://blog.mageekbox.net" + }, + { + "name": "François Dussert", + "email": "francois.dussert@atoum.org" + }, + { + "name": "Gérald Croes", + "email": "gerald.croes@atoum.org" + }, + { + "name": "Julien Bianchi", + "email": "julien.bianchi@atoum.org" + }, + { + "name": "Ludovic Fleury", + "email": "ludovic.fleury@atoum.org" + } + ], + "description": "Simple modern and intuitive unit testing framework for PHP 5.3+", + "homepage": "http://www.atoum.org", + "keywords": [ + "TDD", + "atoum", + "test", + "unit testing" + ], + "time": "2017-07-19T19:59:03+00:00" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.6.0" + }, + "platform-dev": [] +} diff --git a/src/GlpiProject/API/Rest/Client.php b/src/GlpiProject/API/Rest/Client.php index 5d726fe..0f686c9 100644 --- a/src/GlpiProject/API/Rest/Client.php +++ b/src/GlpiProject/API/Rest/Client.php @@ -2,7 +2,107 @@ namespace GlpiProject\API\Rest; use GuzzleHttp\Client as HttpClient; use Exception; +use GlpiProject\API\Rest\Exception\BadEndpointException; +use GlpiProject\API\Rest\Exception\InsufficientArgumentsException; +/** + * @method Alert(string $method, array $input, array $params = []) + * @method APIClient(string $method, array $input, array $params = []) + * @method AuthLDAP(string $method, array $input, array $params = []) + * @method AuthLdapReplicate(string $method, array $input, array $params = []) + * @method AuthMail(string $method, array $input, array $params = []) + * @method AutoUpdateSystem(string $method, array $input, array $params = []) + * @method Blacklist(string $method, array $input, array $params = []) + * @method BlacklistedMailContent(string $method, array $input, array $params = []) + * @method Budget(string $method, array $input, array $params = []) + * @method BudgetType(string $method, array $input, array $params = []) + * @method BusinessCriticity(string $method, array $input, array $params = []) + * @method Calendar_Holiday(string $method, array $input, array $params = []) + * @method Calendar(string $method, array $input, array $params = []) + * @method CalendarSegment(string $method, array $input, array $params = []) + * @method Cartridge(string $method, array $input, array $params = []) + * @method CartridgeItem_PrinterModel(string $method, array $input, array $params = []) + * @method CartridgeItem(string $method, array $input, array $params = []) + * @method CartridgeItemType(string $method, array $input, array $params = []) + * @method Certificate_Item(string $method, array $input, array $params = []) + * @method Certificate(string $method, array $input, array $params = []) + * @method CertificateType(string $method, array $input, array $params = []) + * @method Change_Group(string $method, array $input, array $params = []) + * @method Change_Item(string $method, array $input, array $params = []) + * @method Change_Problem(string $method, array $input, array $params = []) + * @method Change_Project(string $method, array $input, array $params = []) + * @method Change_Supplier(string $method, array $input, array $params = []) + * @method Change_Ticket(string $method, array $input, array $params = []) + * @method Change_User(string $method, array $input, array $params = []) + * @method Change(string $method, array $input, array $params = []) + * @method ChangeCost(string $method, array $input, array $params = []) + * @method ChangeTask(string $method, array $input, array $params = []) + * @method ChangeValidation(string $method, array $input, array $params = []) + * @method Computer_Item(string $method, array $input, array $params = []) + * @method Computer_SoftwareLicense(string $method, array $input, array $params = []) + * @method Computer_SoftwareVersion(string $method, array $input, array $params = []) + * @method Computer(string $method, array $input, array $params = []) + * @method ComputerAntivirus(string $method, array $input, array $params = []) + * @method ComputerDisk(string $method, array $input, array $params = []) + * @method ComputerModel(string $method, array $input, array $params = []) + * @method ComputerType(string $method, array $input, array $params = []) + * @method ComputerVirtualMachine(string $method, array $input, array $params = []) + * @method Config(string $method, array $input, array $params = []) + * @method Consumable(string $method, array $input, array $params = []) + * @method ConsumableItem(string $method, array $input, array $params = []) + * @method ConsumableItemType(string $method, array $input, array $params = []) + * @method Contact_Supplier(string $method, array $input, array $params = []) + * @method Contact(string $method, array $input, array $params = []) + * @method ContactType(string $method, array $input, array $params = []) + * @method Contract_Item(string $method, array $input, array $params = []) + * @method Contract_Supplier(string $method, array $input, array $params = []) + * @method Contract(string $method, array $input, array $params = []) + * @method ContractCost(string $method, array $input, array $params = []) + * @method ContractType(string $method, array $input, array $params = []) + * @method Control(string $method, array $input, array $params = []) + * @method CronTask(string $method, array $input, array $params = []) + * @method CronTaskLog(string $method, array $input, array $params = []) + * @method DeviceBattery(string $method, array $input, array $params = []) + * @method DeviceBatteryModel(string $method, array $input, array $params = []) + * @method DeviceBatteryType(string $method, array $input, array $params = []) + * @method DeviceCase(string $method, array $input, array $params = []) + * @method DeviceCaseModel(string $method, array $input, array $params = []) + * @method DeviceCaseType(string $method, array $input, array $params = []) + * @method DeviceControl(string $method, array $input, array $params = []) + * @method DeviceControlModel(string $method, array $input, array $params = []) + * @method DeviceDrive(string $method, array $input, array $params = []) + * @method DeviceDriveModel(string $method, array $input, array $params = []) + * @method DeviceFirmware(string $method, array $input, array $params = []) + * @method DeviceFirmwareModel(string $method, array $input, array $params = []) + * @method DeviceFirmwareType(string $method, array $input, array $params = []) + * @method DeviceGeneric(string $method, array $input, array $params = []) + * @method DeviceGenericModel(string $method, array $input, array $params = []) + * @method DeviceGenericType(string $method, array $input, array $params = []) + * @method DeviceGraphicCard(string $method, array $input, array $params = []) + * @method DeviceGraphicCardModel(string $method, array $input, array $params = []) + * @method DeviceHardDrive(string $method, array $input, array $params = []) + * @method DeviceHardDriveModel(string $method, array $input, array $params = []) + * @method DeviceMemory(string $method, array $input, array $params = []) + * @method DeviceMemoryModel(string $method, array $input, array $params = []) + * @method DeviceMemoryType(string $method, array $input, array $params = []) + * @method DeviceMotherboard(string $method, array $input, array $params = []) + * @method DeviceMotherBoardModel(string $method, array $input, array $params = []) + * @method DeviceNetworkCard(string $method, array $input, array $params = []) + * @method DeviceNetworkCardModel(string $method, array $input, array $params = []) + * @method DevicePci(string $method, array $input, array $params = []) + * @method DevicePciModel(string $method, array $input, array $params = []) + * @method DevicePowerSupply(string $method, array $input, array $params = []) + * @method DevicePowerSupplyModel(string $method, array $input, array $params = []) + * @method DeviceProcessor(string $method, array $input, array $params = []) + * @method DeviceProcessorModel(string $method, array $input, array $params = []) + * @method DeviceSensor(string $method, array $input, array $params = []) + * @method DeviceSensorModel(string $method, array $input, array $params = []) + * @method DeviceSensorType(string $method, array $input, array $params = []) + * @method DeviceSimcard(string $method, array $input, array $params = []) + * @method DeviceSimcardType(string $method, array $input, array $params = []) + * @method DeviceSoundCard(string $method, array $input, array $params = []) + * @method DeviceSoundCardModel(string $method, array $input, array $params = []) + */ class Client { /** @var HttpClient instance of the HTTP client */ @@ -11,42 +111,226 @@ class Client { /** @var string URL to the GLPI API rest */ private $url; + /** @var $appToken string an application token to use for requests */ + private $appToken = null; + /** @var string Session token obtained after initSession() */ - private $sessionToken = ''; + private $sessionToken = null; + + private $simpleEndpoints = [ + 'Alert', + 'APIClient', + 'AuthLDAP', + 'AuthLdapReplicate', + 'AuthMail', + 'AutoUpdateSystem', + 'Blacklist', + 'BlacklistedMailContent', + 'Budget', + 'BudgetType', + 'BusinessCriticity', + 'Calendar_Holiday', + 'Calendar', + 'CalendarSegment', + 'Cartridge', + 'CartridgeItem_PrinterModel', + 'CartridgeItem', + 'CartridgeItemType', + 'Certificate_Item', + 'Certificate', + 'CertificateType', + 'Change_Group', + 'Change_Item', + 'Change_Problem', + 'Change_Project', + 'Change_Supplier', + 'Change_Ticket', + 'Change_User', + 'Change', + 'ChangeCost', + 'ChangeTask', + 'ChangeValidation', + 'Computer_Item', + 'Computer_SoftwareLicense', + 'Computer_SoftwareVersion', + 'Computer', + 'ComputerAntivirus', + 'ComputerDisk', + 'ComputerModel', + 'ComputerType', + 'ComputerVirtualMachine', + 'Config', + 'Consumable', + 'ConsumableItem', + 'ConsumableItemType', + 'Contact_Supplier', + 'Contact', + 'ContactType', + 'Contract_Item', + 'Contract_Supplier', + 'Contract', + 'ContractCost', + 'ContractType', + 'Control', + 'CronTask', + 'CronTaskLog', + 'DeviceBattery', + 'DeviceBatteryModel', + 'DeviceBatteryType', + 'DeviceCase', + 'DeviceCaseModel', + 'DeviceCaseType', + 'DeviceControl', + 'DeviceControlModel', + 'DeviceDrive', + 'DeviceDriveModel', + 'DeviceFirmware', + 'DeviceFirmwareModel', + 'DeviceFirmwareType', + 'DeviceGeneric', + 'DeviceGenericModel', + 'DeviceGenericType', + 'DeviceGraphicCard', + 'DeviceGraphicCardModel', + 'DeviceHardDrive', + 'DeviceHardDriveModel', + 'DeviceMemory', + 'DeviceMemoryModel', + 'DeviceMemoryType', + 'DeviceMotherboard', + 'DeviceMotherBoardModel', + 'DeviceNetworkCard', + 'DeviceNetworkCardModel', + 'DevicePci', + 'DevicePciModel', + 'DevicePowerSupply', + 'DevicePowerSupplyModel', + 'DeviceProcessor', + 'DeviceProcessorModel', + 'DeviceSensor', + 'DeviceSensorModel', + 'DeviceSensorType', + 'DeviceSimcard', + 'DeviceSimcardType', + 'DeviceSoundCard', + 'DeviceSoundCardModel', + ]; public function __construct(HttpClient $httpClient, $url) { $this->httpClient = $httpClient; - $this->url = $url; + $this->url = trim($url, '/') . '/'; } + /** + * Set an application token to be used for each request + * + * @param string $appToken + */ + public function setAppToken($appToken = null) { + $this->appToken = $appToken; + } + + /** + * Initialize a session with user credentials + * @param string $user + * @param string $password + * @throws Exception + * @return boolean + */ public function initSessionByCredentials($user, $password) { - $response = $this->httpClient->get($this->url . 'initSession', ['auth' => [$user, $password]]); + $response = $this->doHttpRequest('get', 'initSession', ['auth' => [$user, $password]]); if ($response->getStatusCode() != 200 || !$this->sessionToken = json_decode( (string) $response->getBody(), true)['session_token']) { throw new Exception("Cannot connect to api"); - } else { - return true; } + return true; } + /** + * initialize a session with a user token + * + * @param string $userToken + * + * @throws Exception + * + * @return boolean True if success + */ public function initSessionByUserToken($userToken) { - $headers = $this->buildHeaders(['auth' => [$user, $password]]); - $response = $this->httpClient->get($this->url . 'initSession', $headers); + $response = $this->doHttpRequest('get', 'initSession', ['Authorization' => "user_token $userToken"]); if ($response->getStatusCode() != 200 || !$this->sessionToken = json_decode( (string) $response->getBody(), true)['session_token']) { throw new Exception("Cannot connect to api"); } + return true; + } + + public function getSimpleEndpoints() { + return $this->simpleEndpoints; + } + + public function __call($name, $arguments) { + $name = ucfirst($name); + if (!in_array($name, $this->simpleEndpoints)) { + throw new BadEndpointException(); + } + + if (func_num_args() < 2) { + throw new InsufficientArgumentsException(); + } + + $method = $arguments[0]; + $input = $arguments[1]; + $params = isset($arguments[2]) ? $arguments[2] : []; + $params['body'] = json_encode(['input' => $input]); + $params['headers'] = $this->addTokens(); + return $this->doHttpRequest($method, $name, $params); } /** - * build a set of headers for the HTTP client by merging mandatory headers for rest API and provided ones + * Execute a HTTP request to the rest API + * + * @param string $verb + * @param string $relative_uri + * @param array $params * - * @param array $headers + * @throws Exception * - * @return array merged set of headers + * @return \Psr\Http\Message\ResponseInterface the response sent by the server */ - private function buildHeaders(array $headers) { - $headers['Content-Type'] = 'application/json'; + protected function doHttpRequest($verb = "get", $relative_uri = "", $params = []) { + if (!empty($relative_uri)) { + $params['headers']['Content-Type'] = "application/json"; + } + if (isset($params['multipart'])) { + // Guzzle lib will automatically push the correct Content-type + unset($params['headers']['Content-Type']); + } + + $verb = strtolower($verb); + if (in_array($verb, ['get', 'post', 'delete', 'put', 'options', 'patch'])) { + try { + return $this->httpClient->{$verb}($this->url . $relative_uri, + $params); + } catch (Exception $e) { + throw $e; + } + } + } + + /** + * generate headers containing session and app tokens for Http client object + * + * @return string[] + */ + private function addTokens() { + $headers = []; + if ($this->appToken !== null) { + $headers['App-Token'] = $this->appToken; + } + if ($this->sessionToken !== null) { + $headers['Session-Token'] = $this->sessionToken; + } + return $headers; } } diff --git a/src/GlpiProject/API/Rest/Exception/BadEndpointException.php b/src/GlpiProject/API/Rest/Exception/BadEndpointException.php new file mode 100644 index 0000000..677d0ac --- /dev/null +++ b/src/GlpiProject/API/Rest/Exception/BadEndpointException.php @@ -0,0 +1,4 @@ +newTestedInstance(new HttpClient(), GLPI_URL); + + // Test invalid credentials + $this->exception(function() use ($client) { + $client->initSessionByCredentials('glpi', 'bad password'); + })->isInstanceOf(\GuzzleHttp\Exception\ClientException::class); + + // Test valid credentials $success = $client->initSessionByCredentials('glpi', 'glpi'); $this->boolean($success)->isTrue(); } + /** + * @engine inline + */ + public function testComputer() { + $client = $this->newTestedInstance(new HttpClient(), GLPI_URL); + $this->boolean($client->initSessionByCredentials('glpi', 'glpi'))->isTrue(); + + $response = $client->computer('post', ['name' => 'computer 0001']); + $this->object($response)->isInstanceOf(\Psr\Http\Message\ResponseInterface::class); + } + } \ No newline at end of file