OLD | NEW |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino 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 import 'dart:fletch'; | 5 import 'dart:dartino'; |
6 import 'dart:convert' show JSON, UTF8; | 6 import 'dart:convert' show JSON, UTF8; |
7 | 7 |
8 import 'package:http/http.dart'; | 8 import 'package:http/http.dart'; |
9 import 'package:socket/socket.dart'; | 9 import 'package:socket/socket.dart'; |
10 | 10 |
11 abstract class Connection { | 11 abstract class Connection { |
12 final String host; | 12 final String host; |
13 final int port; | 13 final int port; |
14 Socket connect(); | 14 Socket connect(); |
15 void close(); | 15 void close(); |
(...skipping 17 matching lines...) Expand all Loading... |
33 connect(); | 33 connect(); |
34 } | 34 } |
35 Socket connect() => _socket.accept(); | 35 Socket connect() => _socket.accept(); |
36 void close() { _socket.close(); } | 36 void close() { _socket.close(); } |
37 } | 37 } |
38 | 38 |
39 getJson(Connection service, String resource) { | 39 getJson(Connection service, String resource) { |
40 HttpConnection connection = new HttpConnection(service.connect()); | 40 HttpConnection connection = new HttpConnection(service.connect()); |
41 HttpRequest request = new HttpRequest('${service.host}/$resource'); | 41 HttpRequest request = new HttpRequest('${service.host}/$resource'); |
42 request.headers["Host"] = service.host; | 42 request.headers["Host"] = service.host; |
43 request.headers["User-Agent"] = 'fletch'; | 43 request.headers["User-Agent"] = 'dartino'; |
44 HttpResponse response = connection.send(request); | 44 HttpResponse response = connection.send(request); |
45 if (response.statusCode != 200) { | 45 if (response.statusCode != 200) { |
46 throw 'Failed request: $resource on port ${service.port}'; | 46 throw 'Failed request: $resource on port ${service.port}'; |
47 } | 47 } |
48 return JSON.decode(UTF8.decode(response.body)); | 48 return JSON.decode(UTF8.decode(response.body)); |
49 } | 49 } |
50 | 50 |
51 class Server { | 51 class Server { |
52 static const githubApiUrl = 'https://api.github.com'; | 52 static const githubApiUrl = 'https://api.github.com'; |
53 String get host => _connection.host; | 53 String get host => _connection.host; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 } | 136 } |
137 | 137 |
138 List fetch(int page) { | 138 List fetch(int page) { |
139 return _fetch(page, false); | 139 return _fetch(page, false); |
140 } | 140 } |
141 | 141 |
142 void prefetchItemsInRange(int start, int end) { | 142 void prefetchItemsInRange(int start, int end) { |
143 int firstPage = start ~/ count; | 143 int firstPage = start ~/ count; |
144 int lastPage = (end ~/ count) + 1; | 144 int lastPage = (end ~/ count) + 1; |
145 // Scheduling prefetching of surrounding pages in descending order. | 145 // Scheduling prefetching of surrounding pages in descending order. |
146 // (Currently the fletch scheduler will process these in reverse order). | 146 // (Currently the dartino scheduler will process these in reverse order). |
147 if (firstPage > 0) prefetch(firstPage - 1); | 147 if (firstPage > 0) prefetch(firstPage - 1); |
148 prefetch(lastPage + 1); | 148 prefetch(lastPage + 1); |
149 for (int i = lastPage; i >= firstPage; --i) { | 149 for (int i = lastPage; i >= firstPage; --i) { |
150 prefetch(i); | 150 prefetch(i); |
151 } | 151 } |
152 } | 152 } |
153 | 153 |
154 dynamic itemAt(int index) { | 154 dynamic itemAt(int index) { |
155 int page = index ~/ count; | 155 int page = index ~/ count; |
156 int entry = index % count; | 156 int entry = index % count; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 try { | 190 try { |
191 entries = server.get('$api?page=${page + 1}'); | 191 entries = server.get('$api?page=${page + 1}'); |
192 } catch (_) { | 192 } catch (_) { |
193 // Throws once when we hit past the last page. | 193 // Throws once when we hit past the last page. |
194 } | 194 } |
195 _pages[page] = entries; | 195 _pages[page] = entries; |
196 channel.send(entries); | 196 channel.send(entries); |
197 return entries; | 197 return entries; |
198 } | 198 } |
199 } | 199 } |
OLD | NEW |