getMultipleItems method Null safety
- List<
Map< items,GlpiItemType, int> > - {bool expandDropdowns = false,
- bool getHateoas = true,
- bool getSha1 = false,
- bool withDevices = false,
- bool withDisks = false,
- bool withSoftwares = false,
- bool withConnections = false,
- bool withNetworkPorts = false,
- bool withInfoComs = false,
- bool withContracts = false,
- bool withDocuments = false,
- bool withTickets = false,
- bool withProblems = false,
- bool withChanges = false,
- bool withNotes = false,
- bool withLogs = false,
- List<
String> ? addKeysNames}
Return multiples items from many types.
You must pass a List
of Map with the following structure a GlpiItemType as the key and the id of the item as the value.
Reference: https://github.com/glpi-project/glpi/blob/master/apirest.md#get-multiple-items.
Implementation
Future<List<Map<String, dynamic>>> getMultipleItems(
List<Map<GlpiItemType, int>> items,
{bool expandDropdowns = false,
bool getHateoas = true,
bool getSha1 = false,
bool withDevices = false,
bool withDisks = false,
bool withSoftwares = false,
bool withConnections = false,
bool withNetworkPorts = false,
bool withInfoComs = false,
bool withContracts = false,
bool withDocuments = false,
bool withTickets = false,
bool withProblems = false,
bool withChanges = false,
bool withNotes = false,
bool withLogs = false,
List<String>? 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,
};
String itemsUrl = '';
for (var i = 0; i < items.length; i++) {
final type = items[i].keys.first.name.split('.').last;
final id = items[i].values.first;
itemsUrl += 'items[$i][itemtype]=$type&items[$i][items_id]=$id&';
}
String uriStr =
'$baseUrl/getMultipleItems?${itemsUrl}expand_dropdowns=$expandDropdowns&get_hateoas=$getHateoas&get_sha1=$getSha1&with_networkports=$withNetworkPorts&';
uriStr =
'$uriStr&with_devices=$withDevices&with_disks=$withDisks&with_softwares=$withSoftwares&with_connections=$withConnections';
print(uriStr);
final uri = Uri.parse(uriStr);
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);
}