Line data Source code
1 : /*
2 : * Packge : Ethereum
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 10/011/2017
5 : * Copyright : S.Hamblett
6 : *
7 : * Provides a common interface for Ethereum to connect over HTTP
8 : * on the server.
9 : */
10 :
11 : part of ethereum_server_client;
12 :
13 : class EthereumServerHTTPAdapter implements EthereumINetworkAdapter {
14 : /// The HTTP client
15 : HttpClient _client = new HttpClient();
16 :
17 : static const String jsonMimeType = 'application/json';
18 :
19 : /// Processes the HTTP request returning the HTTP response as
20 : /// a map
21 : Future<Map> httpRequest(Uri uri, Map request) {
22 1 : final completer = new Completer();
23 3 : _client.postUrl(uri).then((HttpClientRequest req) {
24 1 : final payload = JSON.encode(request);
25 2 : req.headers.add(HttpHeaders.CONTENT_TYPE, jsonMimeType);
26 2 : req.contentLength = payload.length;
27 1 : req.write(payload);
28 2 : req.close().then((HttpClientResponse resp) {
29 1 : resp.listen((data) {
30 2 : final Map payload = JSON.decode(new String.fromCharCodes(data));
31 1 : completer.complete(payload);
32 : }, onError: (e) {
33 0 : print(e);
34 : }, onDone: () {
35 2 : _client.close();
36 : });
37 : });
38 : }, onError: (e) {
39 0 : print(e);
40 : });
41 1 : return completer.future;
42 : }
43 : }
|