getAllItem method Null safety
Return ALL the items of the given type the current user can see, depending on your server configuration and the number of items proceeded it may have unexpected results.
GlpiItemType contains all the available item types according to the latest GLPI documentation.
By default, only the 50 firts item will be returned. This can be changed by setting the rangeStart
parameter and rangeLimit
parameters.
Reference: https://github.com/glpi-project/glpi/blob/master/apirest.md#get-all-items
Implementation
Future<List<dynamic>> getAllItem(GlpiItemType itemType,
{bool expandDropdowns = false,
bool getHateoas = true,
bool onlyId = false,
int rangeStart = 0,
int rangeLimit = 50,
String sort = 'id',
String order = 'ASC',
String searchText = 'NULL',
bool isDeleted = false,
List? addKeysNames}) async {
if (_sessionToken!.isEmpty) {
throw Exception('No session token, initSession first');
}
final Map<String, String> headers = {
'Session-Token': _sessionToken!,
'Content-Type': 'application/json',
...?appToken != null ? {'App-Token': appToken!} : null,
};
final range = '$rangeStart-$rangeLimit';
final uri = Uri.parse(
'$baseUrl/${itemType.toString().split('.').last}?expand_dropdowns=$expandDropdowns&get_hateoas=$getHateoas&only_id=$onlyId&range=$range&sort=$sort&order=$order&searchText=$searchText&is_deleted=$isDeleted');
if (addKeysNames != null) {
for (var key in addKeysNames) {
uri.queryParameters.addAll({'add_keys_names[]': key});
}
}
final response = await http.get(uri, headers: headers);
if (response.statusCode != 200 && response.statusCode != 206) {
throw GlpiException.fromResponse(
response.statusCode, json.decode(response.body));
}
List<dynamic> decodedJson = json.decode(response.body);
List<Map<String, dynamic>> formatted = decodedJson.map((element) {
return element as Map<String, dynamic>;
}).toList();
return Future.value(formatted);
}