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 library pub.source.hosted; | 5 library pub.source.hosted; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io' as io; | 8 import 'dart:io' as io; |
9 import 'dart:json' as json; | 9 import "dart:convert"; |
10 | 10 |
11 import 'package:http/http.dart' as http; | 11 import 'package:http/http.dart' as http; |
12 import 'package:path/path.dart' as path; | 12 import 'package:path/path.dart' as path; |
13 | 13 |
14 import '../http.dart'; | 14 import '../http.dart'; |
15 import '../io.dart'; | 15 import '../io.dart'; |
16 import '../log.dart' as log; | 16 import '../log.dart' as log; |
17 import '../package.dart'; | 17 import '../package.dart'; |
18 import '../pubspec.dart'; | 18 import '../pubspec.dart'; |
19 import '../source.dart'; | 19 import '../source.dart'; |
(...skipping 19 matching lines...) Expand all Loading... |
39 } | 39 } |
40 | 40 |
41 /// Downloads a list of all versions of a package that are available from the | 41 /// Downloads a list of all versions of a package that are available from the |
42 /// site. | 42 /// site. |
43 Future<List<Version>> getVersions(String name, description) { | 43 Future<List<Version>> getVersions(String name, description) { |
44 var url = _makeUrl(description, | 44 var url = _makeUrl(description, |
45 (server, package) => "$server/api/packages/$package"); | 45 (server, package) => "$server/api/packages/$package"); |
46 | 46 |
47 log.io("Get versions from $url."); | 47 log.io("Get versions from $url."); |
48 return httpClient.read(url, headers: PUB_API_HEADERS).then((body) { | 48 return httpClient.read(url, headers: PUB_API_HEADERS).then((body) { |
49 var doc = json.parse(body); | 49 var doc = JSON.decode(body); |
50 return doc['versions'] | 50 return doc['versions'] |
51 .map((version) => new Version.parse(version['version'])) | 51 .map((version) => new Version.parse(version['version'])) |
52 .toList(); | 52 .toList(); |
53 }).catchError((ex) { | 53 }).catchError((ex) { |
54 var parsed = _parseDescription(description); | 54 var parsed = _parseDescription(description); |
55 _throwFriendlyError(ex, parsed.first, parsed.last); | 55 _throwFriendlyError(ex, parsed.first, parsed.last); |
56 }); | 56 }); |
57 } | 57 } |
58 | 58 |
59 /// Downloads and parses the pubspec for a specific version of a package that | 59 /// Downloads and parses the pubspec for a specific version of a package that |
60 /// is available from the site. | 60 /// is available from the site. |
61 Future<Pubspec> describeUncached(PackageId id) { | 61 Future<Pubspec> describeUncached(PackageId id) { |
62 // Request it from the server. | 62 // Request it from the server. |
63 var url = _makeVersionUrl(id, (server, package, version) => | 63 var url = _makeVersionUrl(id, (server, package, version) => |
64 "$server/api/packages/$package/versions/$version"); | 64 "$server/api/packages/$package/versions/$version"); |
65 | 65 |
66 log.io("Describe package at $url."); | 66 log.io("Describe package at $url."); |
67 return httpClient.read(url, headers: PUB_API_HEADERS).then((version) { | 67 return httpClient.read(url, headers: PUB_API_HEADERS).then((version) { |
68 version = json.parse(version); | 68 version = JSON.decode(version); |
69 | 69 |
70 // TODO(rnystrom): After this is pulled down, we could place it in | 70 // TODO(rnystrom): After this is pulled down, we could place it in |
71 // a secondary cache of just pubspecs. This would let us have a | 71 // a secondary cache of just pubspecs. This would let us have a |
72 // persistent cache for pubspecs for packages that haven't actually | 72 // persistent cache for pubspecs for packages that haven't actually |
73 // been installed. | 73 // been installed. |
74 return new Pubspec.fromMap(version['pubspec'], systemCache.sources); | 74 return new Pubspec.fromMap(version['pubspec'], systemCache.sources); |
75 }).catchError((ex) { | 75 }).catchError((ex) { |
76 var parsed = _parseDescription(id.description); | 76 var parsed = _parseDescription(id.description); |
77 _throwFriendlyError(ex, id, parsed.last); | 77 _throwFriendlyError(ex, id, parsed.last); |
78 }); | 78 }); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 var name = description["name"]; | 261 var name = description["name"]; |
262 if (name is! String) { | 262 if (name is! String) { |
263 throw new FormatException("The 'name' key must have a string value."); | 263 throw new FormatException("The 'name' key must have a string value."); |
264 } | 264 } |
265 | 265 |
266 var url = description["url"]; | 266 var url = description["url"]; |
267 if (url == null) url = HostedSource.defaultUrl; | 267 if (url == null) url = HostedSource.defaultUrl; |
268 | 268 |
269 return new Pair<String, String>(name, url); | 269 return new Pair<String, String>(name, url); |
270 } | 270 } |
OLD | NEW |