thirdparty/google_appengine/google/appengine/api/apiproxy_stub_map.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Sun, 06 Sep 2009 23:31:53 +0200
changeset 2864 2e0b0af889be
parent 2413 d0b7dac5325c
child 3031 7678f72140e6
permissions -rwxr-xr-x
Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/env python
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2007 Google Inc.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#     http://www.apache.org/licenses/LICENSE-2.0
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
#
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
"""Container of APIProxy stubs for more convenient unittesting.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
Classes/variables/functions defined here:
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
  APIProxyStubMap: container of APIProxy stubs.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  apiproxy: global instance of an APIProxyStubMap.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
  MakeSyncCall: APIProxy entry point.
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
    24
  UserRPC: User-visible class wrapping asynchronous RPCs.
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
"""
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    31
import inspect
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
import sys
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
    34
from google.appengine.api import apiproxy_rpc
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
    35
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
    36
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    37
def CreateRPC(service):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    38
  """Creates a RPC instance for the given service.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    39
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    40
  The instance is suitable for talking to remote services.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    41
  Each RPC instance can be used only once, and should not be reused.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    42
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    43
  Args:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    44
    service: string representing which service to call.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    45
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    46
  Returns:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    47
    the rpc object.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    48
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    49
  Raises:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    50
    AssertionError or RuntimeError if the stub for service doesn't supply a
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    51
    CreateRPC method.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    52
  """
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    53
  stub = apiproxy.GetStub(service)
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    54
  assert stub, 'No api proxy found for service "%s"' % service
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    55
  assert hasattr(stub, 'CreateRPC'), ('The service "%s" doesn\'t have ' +
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    56
                                      'a CreateRPC method.' % service)
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    57
  return stub.CreateRPC()
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    58
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    59
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
def MakeSyncCall(service, call, request, response):
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    61
  """The APIProxy entry point for a synchronous API call.
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
  Args:
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
    service: string representing which service to call
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
    call: string representing which function to call
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
    request: protocol buffer for the request
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
    response: protocol buffer for the response
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
  Raises:
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
    apiproxy_errors.Error or a subclass.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
  """
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    72
  apiproxy.MakeSyncCall(service, call, request, response)
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    73
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    74
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    75
class ListOfHooks(object):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    76
  """An ordered collection of hooks for a particular API call.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    77
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    78
  A hook is a function that has exactly the same signature as
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    79
  a service stub. It will be called before or after an api hook is
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    80
  executed, depending on whether this list is for precall of postcall hooks.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    81
  Hooks can be used for debugging purposes (check certain
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    82
  pre- or postconditions on api calls) or to apply patches to protocol
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    83
  buffers before/after a call gets submitted.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    84
  """
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    85
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    86
  def __init__(self):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    87
    """Constructor."""
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    88
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    89
    self.__content = []
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    90
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    91
    self.__unique_keys = set()
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    92
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    93
  def __len__(self):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    94
    """Returns the amount of elements in the collection."""
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    95
    return self.__content.__len__()
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    96
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    97
  def __Insert(self, index, key, function, service=None):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    98
    """Appends a hook at a certain position in the list.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
    99
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   100
    Args:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   101
      index: the index of where to insert the function
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   102
      key: a unique key (within the module) for this particular function.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   103
        If something from the same module with the same key is already
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   104
        registered, nothing will be added.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   105
      function: the hook to be added.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   106
      service: optional argument that restricts the hook to a particular api
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   107
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   108
    Returns:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   109
      True if the collection was modified.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   110
    """
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   111
    unique_key = (key, inspect.getmodule(function))
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   112
    if unique_key in self.__unique_keys:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   113
      return False
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   114
    num_args = len(inspect.getargspec(function)[0])
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   115
    if (inspect.ismethod(function)):
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   116
      num_args -= 1
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   117
    self.__content.insert(index, (key, function, service, num_args))
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   118
    self.__unique_keys.add(unique_key)
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   119
    return True
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   120
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   121
  def Append(self, key, function, service=None):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   122
    """Appends a hook at the end of the list.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   123
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   124
    Args:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   125
      key: a unique key (within the module) for this particular function.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   126
        If something from the same module with the same key is already
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   127
        registered, nothing will be added.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   128
      function: the hook to be added.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   129
      service: optional argument that restricts the hook to a particular api
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   130
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   131
    Returns:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   132
      True if the collection was modified.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   133
    """
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   134
    return self.__Insert(len(self), key, function, service)
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   135
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   136
  def Push(self, key, function, service=None):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   137
    """Inserts a hook at the beginning of the list.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   138
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   139
    Args:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   140
      key: a unique key (within the module) for this particular function.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   141
        If something from the same module with the same key is already
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   142
        registered, nothing will be added.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   143
      function: the hook to be added.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   144
      service: optional argument that restricts the hook to a particular api
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   145
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   146
    Returns:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   147
      True if the collection was modified.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   148
    """
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   149
    return self.__Insert(0, key, function, service)
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   150
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   151
  def Clear(self):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   152
    """Removes all hooks from the list (useful for unit tests)."""
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   153
    self.__content = []
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   154
    self.__unique_keys = set()
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   155
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   156
  def Call(self, service, call, request, response, rpc=None):
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   157
    """Invokes all hooks in this collection.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   158
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   159
    Args:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   160
      service: string representing which service to call
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   161
      call: string representing which function to call
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   162
      request: protocol buffer for the request
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   163
      response: protocol buffer for the response
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   164
      rpc: optional RPC used to make this call
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   165
    """
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   166
    for key, function, srv, num_args in self.__content:
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   167
      if srv is None or srv == service:
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   168
        if num_args == 5:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   169
          function(service, call, request, response, rpc)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   170
        else:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   171
          function(service, call, request, response)
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   172
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   173
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   174
class APIProxyStubMap(object):
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   175
  """Container of APIProxy stubs for more convenient unittesting.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   176
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   177
  Stubs may be either trivial implementations of APIProxy services (e.g.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   178
  DatastoreFileStub, UserServiceStub) or "real" implementations.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   179
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   180
  For unittests, we may want to mix and match real and trivial implementations
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   181
  of services in order to better focus testing on individual service
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   182
  implementations. To achieve this, we allow the client to attach stubs to
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   183
  service names, as well as define a default stub to be used if no specific
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   184
  matching stub is identified.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   185
  """
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   186
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   187
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   188
  def __init__(self, default_stub=None):
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   189
    """Constructor.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   190
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   191
    Args:
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   192
      default_stub: optional stub
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   193
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   194
    'default_stub' will be used whenever no specific matching stub is found.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   195
    """
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   196
    self.__stub_map = {}
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   197
    self.__default_stub = default_stub
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   198
    self.__precall_hooks = ListOfHooks()
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   199
    self.__postcall_hooks = ListOfHooks()
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   200
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   201
  def GetPreCallHooks(self):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   202
    """Gets a collection for all precall hooks."""
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   203
    return self.__precall_hooks
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   204
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   205
  def GetPostCallHooks(self):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   206
    """Gets a collection for all precall hooks."""
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   207
    return self.__postcall_hooks
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   208
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   209
  def RegisterStub(self, service, stub):
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   210
    """Register the provided stub for the specified service.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   211
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   212
    Args:
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   213
      service: string
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   214
      stub: stub
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   215
    """
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   216
    assert not self.__stub_map.has_key(service), repr(service)
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   217
    self.__stub_map[service] = stub
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   218
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   219
    if service == 'datastore':
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   220
      self.RegisterStub('datastore_v3', stub)
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   221
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   222
  def GetStub(self, service):
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   223
    """Retrieve the stub registered for the specified service.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   224
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   225
    Args:
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   226
      service: string
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   227
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   228
    Returns:
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   229
      stub
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   230
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   231
    Returns the stub registered for 'service', and returns the default stub
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   232
    if no such stub is found.
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   233
    """
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   234
    return self.__stub_map.get(service, self.__default_stub)
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   235
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   236
  def MakeSyncCall(self, service, call, request, response):
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   237
    """The APIProxy entry point.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   238
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   239
    Args:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   240
      service: string representing which service to call
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   241
      call: string representing which function to call
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   242
      request: protocol buffer for the request
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   243
      response: protocol buffer for the response
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   244
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   245
    Raises:
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   246
      apiproxy_errors.Error or a subclass.
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   247
    """
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   248
    stub = self.GetStub(service)
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   249
    assert stub, 'No api proxy found for service "%s"' % service
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   250
    if hasattr(stub, 'CreateRPC'):
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   251
      rpc = stub.CreateRPC()
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   252
      self.__precall_hooks.Call(service, call, request, response, rpc)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   253
      rpc.MakeCall(service, call, request, response)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   254
      rpc.Wait()
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   255
      rpc.CheckSuccess()
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   256
      self.__postcall_hooks.Call(service, call, request, response, rpc)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   257
    else:
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   258
      self.__precall_hooks.Call(service, call, request, response)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   259
      stub.MakeSyncCall(service, call, request, response)
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   260
      self.__postcall_hooks.Call(service, call, request, response)
828
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   261
f5fd65cc3bf3 Load /Users/solydzajs/Downloads/google_appengine into
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 109
diff changeset
   262
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   263
class UserRPC(object):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   264
  """Wrapper class for asynchronous RPC.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   265
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   266
  Simplest low-level usage pattern:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   267
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   268
    rpc = UserRPC('service', [deadline], [callback])
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   269
    rpc.make_call('method', request, response)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   270
    .
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   271
    .
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   272
    .
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   273
    rpc.wait()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   274
    rpc.check_success()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   275
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   276
  However, a service module normally provides a wrapper so that the
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   277
  typical usage pattern becomes more like this:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   278
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   279
    from google.appengine.api import service
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   280
    rpc = service.create_rpc([deadline], [callback])
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   281
    service.make_method_call(rpc, [service-specific-args])
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   282
    .
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   283
    .
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   284
    .
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   285
    rpc.wait()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   286
    result = rpc.get_result()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   287
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   288
  The service.make_method_call() function sets a service- and method-
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   289
  specific hook function that is called by rpc.get_result() with the
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   290
  rpc object as its first argument, and service-specific value as its
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   291
  second argument.  The hook function should call rpc.check_success()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   292
  and then extract the user-level result from the rpc.result
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   293
  protobuffer.  Additional arguments may be passed from
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   294
  make_method_call() to the get_result hook via the second argument.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   295
  """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   296
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   297
  __method = None
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   298
  __get_result_hook = None
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   299
  __user_data = None
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   300
  __postcall_hooks_called = False
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   301
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   302
  def __init__(self, service, deadline=None, callback=None):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   303
    """Constructor.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   304
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   305
    Args:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   306
      service: The service name.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   307
      deadline: Optional deadline.  Default depends on the implementation.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   308
      callback: Optional argument-less callback function.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   309
    """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   310
    self.__service = service
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   311
    self.__rpc = CreateRPC(service)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   312
    self.__rpc.deadline = deadline
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   313
    self.__rpc.callback = callback
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   314
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   315
  @property
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   316
  def service(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   317
    """Return the service name."""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   318
    return self.__service
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   319
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   320
  @property
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   321
  def method(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   322
    """Return the method name."""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   323
    return self.__method
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   324
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   325
  @property
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   326
  def deadline(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   327
    """Return the deadline, if set explicitly (otherwise None)."""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   328
    return self.__rpc.deadline
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   329
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   330
  def __get_callback(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   331
    """Return the callback attribute, a function without arguments.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   332
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   333
    This attribute can also be assigned to.  For example, the
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   334
    following code calls some_other_function(rpc) when the RPC is
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   335
    complete:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   336
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   337
      rpc = service.create_rpc()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   338
      rpc.callback = lambda: some_other_function(rpc)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   339
      service.make_method_call(rpc)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   340
      rpc.wait()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   341
    """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   342
    return self.__rpc.callback
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   343
  def __set_callback(self, callback):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   344
    """Set the callback function."""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   345
    self.__rpc.callback = callback
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   346
  callback = property(__get_callback, __set_callback)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   347
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   348
  @property
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   349
  def request(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   350
    """Return the request protocol buffer object."""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   351
    return self.__rpc.request
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   352
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   353
  @property
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   354
  def response(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   355
    """Return the response protocol buffer object."""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   356
    return self.__rpc.response
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   357
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   358
  @property
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   359
  def state(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   360
    """Return the RPC state.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   361
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   362
    Possible values are attributes of apiproxy_rpc.RPC: IDLE, RUNNING,
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   363
    FINISHING.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   364
    """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   365
    return self.__rpc.state
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   366
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   367
  @property
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   368
  def get_result_hook(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   369
    """Return the get-result hook function."""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   370
    return self.__get_result_hook
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   371
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   372
  @property
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   373
  def user_data(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   374
    """Return the user data for the hook function."""
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   375
    return self.__user_data
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   376
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   377
  def make_call(self, method, request, response,
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   378
                get_result_hook=None, user_data=None):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   379
    """Initiate a call.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   380
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   381
    Args:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   382
      method: The method name.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   383
      request: The request protocol buffer.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   384
      response: The response protocol buffer.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   385
      get_result_hook: Optional get-result hook function.  If not None,
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   386
        this must be a function with exactly one argument, the RPC
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   387
        object (self).  Its return value is returned from get_result().
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   388
      user_data: Optional additional arbitrary data for the get-result
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   389
        hook function.  This can be accessed as rpc.user_data.  The
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   390
        type of this value is up to the service module.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   391
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   392
    This function may only be called once per RPC object.  It sends
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   393
    the request to the remote server, but does not wait for a
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   394
    response.  This allows concurrent execution of the remote call and
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   395
    further local processing (e.g., making additional remote calls).
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   396
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   397
    Before the call is initiated, the precall hooks are called.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   398
    """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   399
    assert self.__rpc.state == apiproxy_rpc.RPC.IDLE, repr(self.state)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   400
    self.__method = method
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   401
    self.__get_result_hook = get_result_hook
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   402
    self.__user_data = user_data
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   403
    apiproxy.GetPreCallHooks().Call(
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   404
        self.__service, method, request, response, self.__rpc)
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   405
    self.__rpc.MakeCall(self.__service, method, request, response)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   406
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   407
  def wait(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   408
    """Wait for the call to complete, and call callbacks.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   409
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   410
    This is the only time callback functions may be called.  (However,
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   411
    note that check_success() and get_result() call wait().)   Waiting
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   412
    for one RPC may cause callbacks for other RPCs to be called.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   413
    Callback functions may call check_success() and get_result().
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   414
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   415
    Callbacks are called without arguments; if a callback needs access
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   416
    to the RPC object a Python nested function (a.k.a. closure) or a
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   417
    bound may be used.  To facilitate this, the callback may be
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   418
    assigned after the RPC object is created (but before make_call()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   419
    is called).
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   420
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   421
    Note: don't confuse callbacks with get-result hooks or precall
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   422
    and postcall hooks.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   423
    """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   424
    assert self.__rpc.state != apiproxy_rpc.RPC.IDLE, repr(self.state)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   425
    if self.__rpc.state == apiproxy_rpc.RPC.RUNNING:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   426
      self.__rpc.Wait()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   427
    assert self.__rpc.state == apiproxy_rpc.RPC.FINISHING, repr(self.state)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   428
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   429
  def check_success(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   430
    """Check for success of the RPC, possibly raising an exception.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   431
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   432
    This function should be called at least once per RPC.  If wait()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   433
    hasn't been called yet, it is called first.  If the RPC caused
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   434
    an exceptional condition, an exception will be raised here.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   435
    The first time check_success() is called, the postcall hooks
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   436
    are called.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   437
    """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   438
    self.wait()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   439
    self.__rpc.CheckSuccess()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   440
    if not self.__postcall_hooks_called:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   441
      self.__postcall_hooks_called = True
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   442
      apiproxy.GetPostCallHooks().Call(self.__service, self.__method,
2864
2e0b0af889be Update Google App Engine from 1.2.3 to 1.2.5 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2413
diff changeset
   443
                                       self.request, self.response, self.__rpc)
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   444
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   445
  def get_result(self):
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   446
    """Get the result of the RPC, or possibly raise an exception.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   447
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   448
    This implies a call to check_success().  If a get-result hook was
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   449
    passed to make_call(), that hook is responsible for calling
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   450
    check_success(), and the return value of the hook is returned.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   451
    Otherwise, check_success() is called directly and None is
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   452
    returned.
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   453
    """
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   454
    if self.__get_result_hook is None:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   455
      self.check_success()
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   456
      return None
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   457
    else:
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   458
      return self.__get_result_hook(self)
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   459
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   460
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   461
def GetDefaultAPIProxy():
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   462
  try:
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   463
    runtime = __import__('google.appengine.runtime', globals(), locals(),
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   464
                         ['apiproxy'])
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   465
    return APIProxyStubMap(runtime.apiproxy)
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   466
  except (AttributeError, ImportError):
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   467
    return APIProxyStubMap()
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   468
2413
d0b7dac5325c Update GAE to 1.2.3 in thirdparty folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 828
diff changeset
   469
109
620f9b141567 Load ../../google_appengine into trunk/thirdparty/google_appengine.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   470
apiproxy = GetDefaultAPIProxy()