getAllItem method Null safety

  1. @override
Future<List> getAllItem(
  1. GlpiItemType itemType,
  2. {bool expandDropdowns = false,
  3. bool getHateoas = true,
  4. bool onlyId = false,
  5. int rangeStart = 0,
  6. int rangeLimit = 50,
  7. String sort = 'id',
  8. String order = 'ASC',
  9. String searchText = 'NULL',
  10. bool isDeleted = false,
  11. List? addKeysNames}
)
override

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 firsts 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

@override
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(
      '$host/${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 _innerClient.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);
}