OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 library rpc_service_base; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import '../../../api/errors.dart'; |
| 11 |
| 12 abstract class RPCServiceBase { |
| 13 static ContentType RPC_CONTENT_TYPE = |
| 14 new ContentType('application', 'octet-stream'); |
| 15 |
| 16 HttpClient _client; |
| 17 |
| 18 RPCServiceBase() : _client = new HttpClient(); |
| 19 |
| 20 Future<List<int>> makeRequest(String host, |
| 21 int port, |
| 22 String path, |
| 23 List<int> data, |
| 24 Map<String, String> additionalHeaders) { |
| 25 return _client.post(host, port, path).then((HttpClientRequest request) { |
| 26 var headers = request.headers; |
| 27 headers.contentType = RPC_CONTENT_TYPE; |
| 28 headers.contentLength = data.length; |
| 29 |
| 30 for (var key in additionalHeaders.keys) { |
| 31 headers.set(key, additionalHeaders[key]); |
| 32 } |
| 33 |
| 34 request.add(data); |
| 35 return request.close().then((HttpClientResponse response) { |
| 36 if (response.statusCode != HttpStatus.OK) { |
| 37 return response.drain().then((_) { |
| 38 throw new ProtocolError("Http statusCode was " |
| 39 "${response.statusCode} instead of ${HttpStatus.OK}"); |
| 40 }); |
| 41 } |
| 42 return response.fold(new BytesBuilder(), (buffer, data) { |
| 43 buffer.add(data); |
| 44 return buffer; |
| 45 }).then((BytesBuilder buffer) => buffer.takeBytes()); |
| 46 }); |
| 47 }).catchError((error) { |
| 48 throw new NetworkError('$error $port $host'); |
| 49 }); |
| 50 } |
| 51 } |
OLD | NEW |