glpi_dart library Null safety
A library to access the GLPI API
This library is a wrapper around the GLPI API, it provides a simple way to access the API. It is based on the Dart http library so it can be used ont dartvm and dartweb. It was implemented following the GLPI API documentation
Usage
Before using the library, you need to make sure your GLPI instance is correctly setup to enable the api usage.
First you need to add the library to your pubspec.yaml file. Either run
dart pub add glpi_dart
or manually add
glpi_dart: ^0.0.1
to your pubspec.yaml file.
Then you can use the library by importing the client.
import 'package:glpi_dart/glpi_dart.dart';
There's two way to create a client to access the API, either by using the GlpiClient.withLogin class or by using the GlpiClient.withToken constructor.
import 'package:glpi_dart/glpi_dart.dart';
void main() async {
// Create a client with login and password
GlpiClient client = GlpiClient.withLogin('http://localhost/glpi/apirest.php/', 'username', 'password');
//Get the session token
await client.initSession();
//Then we can start making request for example, gell all the computers
try {
final computers = await client.getAll(GlpiItemType.Computer);
} catch (e) {
print(e);
}
//Or get a specific computer
try {
final computer = await client.getItem(GlpiItemType.Computer, '1');
} catch (e) {
print(e);
}
//Then we can delete the session
await client.killSession();
Check the example
directory for more examples.
Errors
By design the library will throw an GlpiException if an error occurs during the request. The exception will contain the http response code and a Map with the error code and the error localized message. The error code is the HTTP status code returned by the server. The error message is the message returned by the server.
Methods will also throw an exception if the session token is not set using GlpiClient.initSession.
Classes
- GlpiClient
- A client used to communicate with the GLPI API. To create a client you may use either GlpiClient.withLogin or GlpiClient.withToken. The former will create a client with a username and password, the latter with a user token and an app token.
- GlpiSearchCriteria
- Represent a search criteria to be used in a search request with GlpiClient.searchItems
Enums
- GlpiItemType
- The enum reference all the GLPI itemtype inherited from the GLPI class CommonDBTM. This mean we will keep the GLPI / PHP naming convention for the itemtype.
- GlpiSearchLink
- Represent both values that can be used inside a search criteria
- GlpiSearchType
- Contains all the search types usable inside a search criteria