Skip to content

Commit

Permalink
Tweak nd-json list reader
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Aug 14, 2024
1 parent 2c91056 commit ba871fe
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions lib/src/model/common/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -419,18 +419,7 @@ extension ClientExtension on Client {
}) async {
final response = await get(url, headers: headers);
_checkResponseSuccess(url, response);
try {
final json = LineSplitter.split(utf8.decode(response.bodyBytes))
.where((e) => e.isNotEmpty && e != '\n')
.map((e) => jsonDecode(e) as Map<String, dynamic>);
return IList(json.map(mapper));
} catch (e) {
_logger.severe('Could not read nd-json objects as List<$T>.');
throw ClientException(
'Could not read nd-json objects as List<$T>: $e',
url,
);
}
return _readNdJsonList(response, mapper);
}

/// Sends an HTTP POST request with the given headers and body to the given URL and
Expand Down Expand Up @@ -484,16 +473,27 @@ extension ClientExtension on Client {
final response =
await post(url, headers: headers, body: body, encoding: encoding);
_checkResponseSuccess(url, response);
return _readNdJsonList(response, mapper);
}

IList<T> _readNdJsonList<T>(
Response response,
T Function(Map<String, dynamic>) mapper,
) {
try {
final json = LineSplitter.split(utf8.decode(response.bodyBytes))
.where((e) => e.isNotEmpty && e != '\n')
.map((e) => jsonDecode(e) as Map<String, dynamic>);
return IList(json.map(mapper));
return IList(
LineSplitter.split(utf8.decode(response.bodyBytes))
.where((e) => e.isNotEmpty && e != '\n')
.map((e) {
final json = jsonDecode(e) as Map<String, dynamic>;
return mapper(json);
}),
);
} catch (e) {
_logger.severe('Could not read nd-json objects as List<$T>.');
throw ClientException(
'Could not read nd-json objects as List<$T>.',
url,
'Could not read nd-json objects as List<$T>: $e',
response.request?.url,
);
}
}
Expand Down

0 comments on commit ba871fe

Please sign in to comment.