getEntryFrom<T> method

Future<Entry<T>?> getEntryFrom<T>({
  1. required T fromJsonT(
    1. Object?
    ),
  2. required String entryID,
  3. String? envId,
})

Fetch and parse the given type of Data Entry. Parses the language to utf8. Throws Exception on error.

Implementation

Future<Entry<T>?> getEntryFrom<T>({
  required T Function(Object?) fromJsonT,
  required String entryID,
  String? envId,
}) async {
  final environmentId = envId ?? 'master';
  final url = '$_baseUrl/spaces/${_client.spaceId}'
      '/environments/$environmentId/entries/'
      '$entryID?access_token=${_client.accessToken}';

  final response = await http.get(Uri.parse(url));
  if (response.statusCode == 200) {
    final body = utf8.decode(response.bodyBytes);
    final jsonBody = jsonDecode(body) as Map<String, dynamic>?;
    if (jsonBody == null) return null;
    return Entry.fromJson(jsonBody, fromJsonT);
  } else {
    throw Exception('Failed to load article');
  }
}