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 modules_impl; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:appengine/api/modules.dart'; |
| 10 |
| 11 import '../appengine_context.dart'; |
| 12 |
| 13 import '../../api/errors.dart' as errors; |
| 14 import '../protobuf_api/modules_service.dart'; |
| 15 import '../protobuf_api/rpc/rpc_service.dart'; |
| 16 import '../protobuf_api/internal/modules_service.pb.dart' as pb; |
| 17 |
| 18 class ModulesRpcImpl extends ModulesService { |
| 19 final ModulesServiceClientRPCStub _clientRPCStub; |
| 20 final AppengineContext _appengineContext; |
| 21 |
| 22 buildModulesException(RpcApplicationError error) { |
| 23 var errorCode = pb.ModulesServiceError_ErrorCode.valueOf(error.code); |
| 24 switch (errorCode) { |
| 25 case pb.ModulesServiceError_ErrorCode.INVALID_MODULE: |
| 26 return new errors.ApplicationError( |
| 27 "Modules error: Invalid module."); |
| 28 case pb.ModulesServiceError_ErrorCode.INVALID_VERSION: |
| 29 return new errors.ApplicationError( |
| 30 "Modules error: Invalid version."); |
| 31 case pb.ModulesServiceError_ErrorCode.INVALID_INSTANCES: |
| 32 return new errors.ApplicationError( |
| 33 "Modules error: Invalid instance."); |
| 34 case pb.ModulesServiceError_ErrorCode.TRANSIENT_ERROR: |
| 35 return new errors.ApplicationError( |
| 36 "Modules error: Transient error."); |
| 37 case pb.ModulesServiceError_ErrorCode.UNEXPECTED_STATE: |
| 38 return new errors.ApplicationError( |
| 39 "Modules error: Unexpected state."); |
| 40 default: |
| 41 return error; |
| 42 } |
| 43 } |
| 44 |
| 45 ModulesRpcImpl(RPCService rpcService, |
| 46 AppengineContext this._appengineContext, |
| 47 String ticket) |
| 48 : _clientRPCStub = new ModulesServiceClientRPCStub(rpcService, ticket) { |
| 49 } |
| 50 |
| 51 String get currentModule { |
| 52 return _appengineContext.module; |
| 53 } |
| 54 |
| 55 String get currentVersion { |
| 56 return _appengineContext.version; |
| 57 } |
| 58 |
| 59 String get currentInstance { |
| 60 return _appengineContext.instance; |
| 61 } |
| 62 |
| 63 Future<String> defaultVersion(String module) { |
| 64 var request = new pb.GetDefaultVersionRequest(); |
| 65 request..module = module; |
| 66 return _clientRPCStub.GetDefaultVersion(request) |
| 67 .then((response) => response.version) |
| 68 .catchError((RpcApplicationError error) { |
| 69 throw buildModulesException(error); |
| 70 }, test: (error) => error is RpcApplicationError); |
| 71 } |
| 72 |
| 73 Future<List<String>> modules() { |
| 74 var request = new pb.GetModulesRequest(); |
| 75 return _clientRPCStub.GetModules(request) |
| 76 .then((response) => response.module) |
| 77 .catchError((RpcApplicationError error) { |
| 78 throw buildModulesException(error); |
| 79 }, test: (error) => error is RpcApplicationError); |
| 80 } |
| 81 |
| 82 Future<List<String>> versions(String module) { |
| 83 var request = new pb.GetVersionsRequest(); |
| 84 request.module = module; |
| 85 return _clientRPCStub.GetVersions(request) |
| 86 .then((response) => response.version) |
| 87 .catchError((RpcApplicationError error) { |
| 88 throw buildModulesException(error); |
| 89 }, test: (error) => error is RpcApplicationError); |
| 90 } |
| 91 |
| 92 Future<String> hostname([String module, String version, String instance]) { |
| 93 // Maps double-wildcard domains hosted at appspot.com as Google is |
| 94 // issuing certificates for SSL certificates for double-wildcard |
| 95 // domains hosted at appspot.com (i.e. *.*.appspot.com). |
| 96 // |
| 97 // It performs mapping like this: |
| 98 // |
| 99 // project.appspot.com -> project.appspot.com |
| 100 // version.project.appspot.com -> version-dot-project.appspot.com |
| 101 // version.module.project.appspot.com -> |
| 102 // version-dot-module-dot-project.appspot.com |
| 103 String mapHostname(String hostname) { |
| 104 const appSpotDotCom = '.appspot.com'; |
| 105 if (hostname.endsWith(appSpotDotCom)) { |
| 106 var lastDotindex = hostname.length - appSpotDotCom.length; |
| 107 if (hostname.indexOf('.') < lastDotindex) { |
| 108 return hostname.substring(0, lastDotindex) |
| 109 .split('.') |
| 110 .join('-dot-') |
| 111 + appSpotDotCom; |
| 112 } |
| 113 } |
| 114 return hostname; |
| 115 } |
| 116 var request = new pb.GetHostnameRequest(); |
| 117 if (module != null) { |
| 118 request.module = module; |
| 119 } |
| 120 if (version != null) { |
| 121 request.version = version; |
| 122 } |
| 123 if (instance != null) { |
| 124 request.instance = instance; |
| 125 } |
| 126 return _clientRPCStub.GetHostname(request) |
| 127 .then((response) => mapHostname(response.hostname)) |
| 128 .catchError((RpcApplicationError error) { |
| 129 throw buildModulesException(error); |
| 130 }, test: (error) => error is RpcApplicationError); |
| 131 } |
| 132 } |
OLD | NEW |