| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// Helpers for dealing with HTTP. | 5 /// Helpers for dealing with HTTP. |
| 6 library pub.http; | 6 library pub.http; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:convert'; |
| 9 import 'dart:io'; | 10 import 'dart:io'; |
| 10 import 'dart:json' as json; | |
| 11 | 11 |
| 12 import 'package:http/http.dart' as http; | 12 import 'package:http/http.dart' as http; |
| 13 | 13 |
| 14 import 'io.dart'; | 14 import 'io.dart'; |
| 15 import 'log.dart' as log; | 15 import 'log.dart' as log; |
| 16 import 'oauth2.dart' as oauth2; | 16 import 'oauth2.dart' as oauth2; |
| 17 import 'sdk.dart' as sdk; | 17 import 'sdk.dart' as sdk; |
| 18 import 'utils.dart'; | 18 import 'utils.dart'; |
| 19 | 19 |
| 20 // TODO(nweiz): make this configurable | 20 // TODO(nweiz): make this configurable |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 invalidServerResponse(response); | 196 invalidServerResponse(response); |
| 197 } | 197 } |
| 198 throw errorMap['error']['message']; | 198 throw errorMap['error']['message']; |
| 199 } | 199 } |
| 200 | 200 |
| 201 /// Parses a response body, assuming it's JSON-formatted. Throws a user-friendly | 201 /// Parses a response body, assuming it's JSON-formatted. Throws a user-friendly |
| 202 /// error if the response body is invalid JSON, or if it's not a map. | 202 /// error if the response body is invalid JSON, or if it's not a map. |
| 203 Map parseJsonResponse(http.Response response) { | 203 Map parseJsonResponse(http.Response response) { |
| 204 var value; | 204 var value; |
| 205 try { | 205 try { |
| 206 value = json.parse(response.body); | 206 value = JSON.decode(response.body); |
| 207 } on FormatException catch (e) { | 207 } on FormatException catch (e) { |
| 208 invalidServerResponse(response); | 208 invalidServerResponse(response); |
| 209 } | 209 } |
| 210 if (value is! Map) invalidServerResponse(response); | 210 if (value is! Map) invalidServerResponse(response); |
| 211 return value; | 211 return value; |
| 212 } | 212 } |
| 213 | 213 |
| 214 /// Throws an error describing an invalid response from the server. | 214 /// Throws an error describing an invalid response from the server. |
| 215 void invalidServerResponse(http.Response response) => | 215 void invalidServerResponse(http.Response response) => |
| 216 fail('Invalid server response:\n${response.body}'); | 216 fail('Invalid server response:\n${response.body}'); |
| 217 | 217 |
| 218 /// Exception thrown when an HTTP operation fails. | 218 /// Exception thrown when an HTTP operation fails. |
| 219 class PubHttpException implements Exception { | 219 class PubHttpException implements Exception { |
| 220 final http.Response response; | 220 final http.Response response; |
| 221 | 221 |
| 222 const PubHttpException(this.response); | 222 const PubHttpException(this.response); |
| 223 | 223 |
| 224 String toString() => 'HTTP error ${response.statusCode}: ' | 224 String toString() => 'HTTP error ${response.statusCode}: ' |
| 225 '${response.reasonPhrase}'; | 225 '${response.reasonPhrase}'; |
| 226 } | 226 } |
| OLD | NEW |