OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 part of dartiverse_search; | |
6 | |
7 | |
8 class StackOverflowSearchEngine { | |
9 String get name => 'StackOverflow'; | |
10 | |
11 Stream<SearchResult> search(String input) { | |
12 var query = { | |
13 'site': 'stackoverflow', | |
14 'tagged': 'dart', | |
15 'intitle': input, | |
16 'sort': 'relevance', | |
17 'order': 'desc', | |
18 'pagesize': '3' | |
19 }; | |
20 var searchUri = new Uri.https( | |
21 'api.stackexchange.com', | |
22 '/2.1/search', | |
23 query); | |
24 var controller = new StreamController(); | |
25 http_client.get(searchUri) | |
26 .then((response) { | |
27 if (response.statusCode != HttpStatus.OK) throw "Bad status code."; | |
28 var json = JSON.decode(response.body); | |
29 json.putIfAbsent('items', () => []); | |
30 json['items'].take(3).forEach((item) { | |
31 controller.add(new SearchResult( | |
32 item['title'], item['link'])); | |
33 }); | |
34 }) | |
35 .catchError(controller.addError) | |
36 .whenComplete(controller.close); | |
37 return controller.stream; | |
38 } | |
39 } | |
OLD | NEW |