getItem method Null safety

  1. @override
Future<Map<String, dynamic>> getItem(
  1. GlpiItemType itemtype,
  2. int id,
  3. {bool expandDropdowns = false,
  4. bool getHateoas = true,
  5. bool getSha1 = false,
  6. bool withDevices = false,
  7. bool withDisks = false,
  8. bool withSoftwares = false,
  9. bool withConnections = false,
  10. bool withNetworkPorts = false,
  11. bool withInfoComs = false,
  12. bool withContracts = false,
  13. bool withDocuments = false,
  14. bool withTickets = false,
  15. bool withProblems = false,
  16. bool withChanges = false,
  17. bool withNotes = false,
  18. bool withLogs = false,
  19. List<String>? addKeysNames}
)
override

Return an item identified by the id and of type itemType Will throw an Exception if the request fails or if the selected id is incorrect. withDevices will be ignored if the itemtype isn't GlpiItemType.Computer,GlpiItemType.NetworkEquipment,GlpiItemType.Peripheral,GlpiItemType.Phone or GlpiItemType.Printer. withDisks, withSoftwares, withConnections will be ignored if the itemtype isn't GlpiItemType.Computer. Reference: https://github.com/glpi-project/glpi/blob/master/apirest.md#get-an-item

Implementation

@override
Future<Map<String, dynamic>> getItem(GlpiItemType itemtype, int id,
    {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 uriStr =
      '$host/${itemtype.toString().split('.').last}/$id?expand_dropdowns=$expandDropdowns&get_hateoas=$getHateoas&get_sha1=$getSha1&with_networkports=$withNetworkPorts&';

  if (itemtype == GlpiItemType.Computer) {
    uriStr =
        '$uriStr&with_devices=$withDevices&with_disks=$withDisks&with_softwares=$withSoftwares&with_connections=$withConnections';
  } else if (itemtype == GlpiItemType.NetworkEquipment ||
      itemtype == GlpiItemType.Peripheral ||
      itemtype == GlpiItemType.Phone ||
      itemtype == GlpiItemType.Printer) {
    uriStr = '$uriStr&with_devices=$withDevices';
  }

  final uri = Uri.parse(uriStr);

  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) {
    throw GlpiException.fromResponse(
        response.statusCode, json.decode(response.body));
  }

  return Future.value(json.decode(response.body));
}