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_remote_api; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import 'rpc_service.dart'; |
| 11 import 'rpc_service_base.dart'; |
| 12 import '../internal/remote_api.pb.dart' as remote_api; |
| 13 |
| 14 |
| 15 class RPCServiceRemoteApi extends RPCServiceBase implements RPCService { |
| 16 static const Map<String, String> ADDITIONAL_HEADERS = const <String,String> { |
| 17 'X-Google-RPC-Service-Endpoint': 'app-engine-apis', |
| 18 'X-Google-RPC-Service-Method': '/VMRemoteAPI.CallRemoteAPI', |
| 19 }; |
| 20 |
| 21 final String hostname; |
| 22 final int port; |
| 23 final String path; |
| 24 HttpClient _client; |
| 25 |
| 26 RPCServiceRemoteApi(this.hostname, this.port, {this.path: '/rpc_http'}) { |
| 27 _client = new HttpClient(); |
| 28 } |
| 29 |
| 30 Future<List<int>> call(String apiPackage, |
| 31 String method, |
| 32 List<int> requestProtocolBuffer, |
| 33 {String ticket: 'invalid-ticket'}) { |
| 34 var apiRequest = new remote_api.Request(); |
| 35 apiRequest.serviceName = apiPackage; |
| 36 apiRequest.method = method; |
| 37 apiRequest.request = requestProtocolBuffer; |
| 38 apiRequest.requestId = ticket; |
| 39 return _call(apiRequest).then((remote_api.Response apiResponse) { |
| 40 if (apiResponse.hasApplicationError()) { |
| 41 throw new RpcApplicationError( |
| 42 apiResponse.applicationError.code, |
| 43 apiResponse.applicationError.detail); |
| 44 } |
| 45 // This can e.g. happen if the request ticket is invalid. |
| 46 if (apiResponse.hasRpcError()) { |
| 47 throw new Exception('An internal error occured while making a RPC call ' |
| 48 '(${apiResponse.rpcError.toString().replaceAll('\n',' ')}).'); |
| 49 } |
| 50 return apiResponse.response; |
| 51 }); |
| 52 } |
| 53 |
| 54 Future<remote_api.Response> _call(remote_api.Request rpcRequest) { |
| 55 return makeRequest(hostname, |
| 56 port, |
| 57 path, |
| 58 rpcRequest.writeToBuffer(), |
| 59 ADDITIONAL_HEADERS).then((List<int> responseData) { |
| 60 return new remote_api.Response.fromBuffer(responseData); |
| 61 }); |
| 62 } |
| 63 } |
OLD | NEW |