|
1 #!/usr/bin/env python |
|
2 # |
|
3 # Copyright 2007 Google Inc. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 # |
|
17 |
|
18 """Base class for implementing API proxy stubs.""" |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 from google.appengine.runtime import apiproxy_errors |
|
26 |
|
27 |
|
28 MAX_REQUEST_SIZE = 1 << 20 |
|
29 |
|
30 |
|
31 class APIProxyStub(object): |
|
32 """Base class for implementing API proxy stub classes. |
|
33 |
|
34 To implement an API proxy stub: |
|
35 - Extend this class. |
|
36 - Override __init__ to pass in appropriate default service name. |
|
37 - Implement service methods as _Dynamic_<method>(request, response). |
|
38 """ |
|
39 |
|
40 def __init__(self, service_name, max_request_size=MAX_REQUEST_SIZE): |
|
41 """Constructor. |
|
42 |
|
43 Args: |
|
44 service_name: Service name expected for all calls. |
|
45 max_request_size: int, maximum allowable size of the incoming request. A |
|
46 apiproxy_errors.RequestTooLargeError will be raised if the inbound |
|
47 request exceeds this size. Default is 1 MB. |
|
48 """ |
|
49 self.__service_name = service_name |
|
50 self.__max_request_size = max_request_size |
|
51 |
|
52 def MakeSyncCall(self, service, call, request, response): |
|
53 """The main RPC entry point. |
|
54 |
|
55 Args: |
|
56 service: Must be name as provided to service_name of constructor. |
|
57 call: A string representing the rpc to make. Must be part of |
|
58 the underlying services methods and impemented by _Dynamic_<call>. |
|
59 request: A protocol buffer of the type corresponding to 'call'. |
|
60 response: A protocol buffer of the type corresponding to 'call'. |
|
61 """ |
|
62 assert service == self.__service_name, ('Expected "%s" service name, ' |
|
63 'was "%s"' % (self.__service_name, |
|
64 service)) |
|
65 if request.ByteSize() > self.__max_request_size: |
|
66 raise apiproxy_errors.RequestTooLargeError( |
|
67 'The request to API call %s.%s() was too large.' % (service, call)) |
|
68 messages = [] |
|
69 assert request.IsInitialized(messages), messages |
|
70 |
|
71 method = getattr(self, '_Dynamic_' + call) |
|
72 method(request, response) |