getThing method

Future<Thing> getThing(
  1. String thingId, {
  2. FieldQuery? fields,
})

Requests a directory thing entry (matching the thingId) from the S3I-Directory.

Specify fields if only a subset of thing information is required. See FieldQuery for more information.

Throws a NetworkAuthenticationException if AuthenticationManager.getAccessToken fails. Throws a SocketException if no internet connection is available. Throws a NetworkResponseException if the received status code is not 200. Throws a ResponseParsingException if something went wrong during the parsing to the directory objects.

Implementation

Future<Thing> getThing(String thingId, {FieldQuery? fields}) async {
  //if some fields are specified we need to add thingId
  //to create a valid thing
  if (fields != null) {
    if (fields.fields.isNotEmpty &&
        !fields.fields.contains(DittoKeys.thingId)) {
      fields.fields.add(DittoKeys.thingId);
    }
  }
  final Response response = await getDirectory(
      assembleQuery('/things/$thingId', fieldQuery: fields));
  if (response.statusCode != 200) throw NetworkResponseException(response);
  try {
    return Thing.fromJson(
        jsonDecode(utf8.decode(response.bodyBytes)) as Map<String, dynamic>);
  } on InvalidJsonSchemaException catch (e) {
    throw ResponseParsingException(e);
  } on TypeError catch (e) {
    throw ResponseParsingException(
        InvalidJsonSchemaException(e.stackTrace.toString(), response.body));
  }
}