pub.http library
Helpers for dealing with HTTP.
Properties
final HTTP_TIMEOUT #
The amount of time in milliseconds to allow HTTP requests before assuming they've failed.
final HTTP_TIMEOUT = 30 * 1000
Functions
void invalidServerResponse(Response response) #
Throws an error describing an invalid response from the server.
void invalidServerResponse(http.Response response) { throw 'Invalid server response:\n${response.body}'; }
Map parseJsonResponse(Response response) #
Parses a response body, assuming it's JSON-formatted. Throws a user-friendly error if the response body is invalid JSON, or if it's not a map.
Map parseJsonResponse(http.Response response) { var value; try { value = json.parse(response.body); } catch (e) { // TODO(nweiz): narrow this catch clause once issue 6775 is fixed. invalidServerResponse(response); } if (value is! Map) invalidServerResponse(response); return value; }
void handleJsonError(Response response) #
Handles an unsuccessful JSON-formatted response from pub.dartlang.org.
These responses are expected to be of the form {"error": {"message": "some
message"}}
. If the format is correct, the message will be raised as an
error; otherwise an invalidServerResponse error will be raised.
void handleJsonError(http.Response response) { var errorMap = parseJsonResponse(response); if (errorMap['error'] is! Map || !errorMap['error'].containsKey('message') || errorMap['error']['message'] is! String) { invalidServerResponse(response); } throw errorMap['error']['message']; }
void handleJsonSuccess(Response response) #
The HTTP client to use for all HTTP requests. Handles a successful JSON-formatted response from pub.dartlang.org.
These responses are expected to be of the form {"success": {"message":
"some message"}}
. If the format is correct, the message will be printed;
otherwise an error will be raised.
void handleJsonSuccess(http.Response response) { var parsed = parseJsonResponse(response); if (parsed['success'] is! Map || !parsed['success'].containsKey('message') || parsed['success']['message'] is! String) { invalidServerResponse(response); } log.message(parsed['success']['message']); }