go method
Execute this Request and return its Response
.
Might reject with http.ClientException
during a network failure.
Implementation
Future<Response<T>> go() async {
if (client._closed) {
throw StateError(
'Cannot execute request because UnsplashClient has already been '
'closed.',
);
}
final httpRequest = _prepareRequest();
http.StreamedResponse httpResponse;
String body;
dynamic json;
T? data;
// ignore: deprecated_member_use_from_same_package
if (client.settings.debug) {
print('Sending request:\n${_printRequest(httpRequest)}\n');
}
if (client.logger.isLoggable(Level.FINE)) {
client.logger.fine('Sending request:\n${_printRequest(httpRequest)}');
}
httpResponse = await client._http.send(httpRequest);
// ignore: deprecated_member_use_from_same_package
if (client.settings.debug) {
print('Received response:\n${_printResponse(httpResponse)}\n');
}
if (client.logger.isLoggable(Level.FINE)) {
client.logger.fine('Received response:\n${_printResponse(httpResponse)}');
}
body = await httpResponse.stream.bytesToString();
try {
json = jsonDecode(body);
} catch (e) {
// ignore: avoid_catches_without_on_clauses
}
if (httpResponse.statusCode < 400 &&
json != null &&
bodyDeserializer != null) {
data = bodyDeserializer!(json);
}
return Response(
request: this,
httpRequest: httpRequest,
httpResponse: httpResponse,
body: body,
json: json,
data: data,
);
}