google.appengine.api.apiproxy_stub_map module

Container of APIProxy stubs for more convenient unittesting.

Classes/variables/functions defined here:
APIProxyStubMap: container of APIProxy stubs. apiproxy: global instance of an APIProxyStubMap. MakeSyncCall: APIProxy entry point. UserRPC: User-visible class wrapping asynchronous RPCs.
class google.appengine.api.apiproxy_stub_map.APIProxyStubMap(default_stub=None)[source]

Bases: object

Container of APIProxy stubs for more convenient unittesting.

Stubs may be either trivial implementations of APIProxy services (e.g. DatastoreFileStub, UserServiceStub) or “real” implementations.

For unittests, we may want to mix and match real and trivial implementations of services in order to better focus testing on individual service implementations. To achieve this, we allow the client to attach stubs to service names, as well as define a default stub to be used if no specific matching stub is identified.

CancelApiCalls()[source]
GetPostCallHooks()[source]

Gets a collection for all precall hooks.

GetPreCallHooks()[source]

Gets a collection for all precall hooks.

GetStub(service)[source]

Retrieve the stub registered for the specified service.

Args:
service: string
Returns:
stub

Returns the stub registered for ‘service’, and returns the default stub if no such stub is found.

MakeSyncCall(service, call, request, response)[source]

The APIProxy entry point.

Args:
service: string representing which service to call call: string representing which function to call request: protocol buffer for the request response: protocol buffer for the response
Returns:
Response protocol buffer or None. Some implementations may return a response protocol buffer instead of modifying ‘response’. Caller must use returned value in such cases. If ‘response’ is modified then returns None.
Raises:
apiproxy_errors.Error or a subclass.
RegisterStub(service, stub)[source]

Register the provided stub for the specified service.

Args:
service: string stub: stub
ReplaceStub(service, stub)[source]

Replace the existing stub for the specified service with a new one.

NOTE: This is a risky operation; external callers should use this with caution.

Args:
service: string stub: stub
google.appengine.api.apiproxy_stub_map.CreateRPC(service, stubmap=None)[source]

Creates a RPC instance for the given service.

The instance is suitable for talking to remote services. Each RPC instance can be used only once, and should not be reused.

Args:
service: string representing which service to call. stubmap: optional APIProxyStubMap instance, for dependency injection.
Returns:
the rpc object.
Raises:
AssertionError or RuntimeError if the stub for service doesn’t supply a CreateRPC method.
google.appengine.api.apiproxy_stub_map.GetDefaultAPIProxy()[source]
class google.appengine.api.apiproxy_stub_map.ListOfHooks[source]

Bases: object

An ordered collection of hooks for a particular API call.

A hook is a function that has exactly the same signature as a service stub. It will be called before or after an api hook is executed, depending on whether this list is for precall of postcall hooks. Hooks can be used for debugging purposes (check certain pre- or postconditions on api calls) or to apply patches to protocol buffers before/after a call gets submitted.

Append(key, function, service=None)[source]

Appends a hook at the end of the list.

Args:
key: a unique key (within the module) for this particular function.
If something from the same module with the same key is already registered, nothing will be added.

function: the hook to be added. service: optional argument that restricts the hook to a particular api

Returns:
True if the collection was modified.
Call(service, call, request, response, rpc=None, error=None)[source]

Invokes all hooks in this collection.

NOTE: For backwards compatibility, if error is not None, hooks with 4 or 5 arguments are not called. This situation (error=None) only occurs when the RPC request raised an exception; in the past no hooks would be called at all in that case.

Args:
service: string representing which service to call call: string representing which function to call request: protocol buffer for the request response: protocol buffer for the response rpc: optional RPC used to make this call error: optional Exception instance to be passed as 6th argument
Clear()[source]

Removes all hooks from the list (useful for unit tests).

Push(key, function, service=None)[source]

Inserts a hook at the beginning of the list.

Args:
key: a unique key (within the module) for this particular function.
If something from the same module with the same key is already registered, nothing will be added.

function: the hook to be added. service: optional argument that restricts the hook to a particular api

Returns:
True if the collection was modified.
google.appengine.api.apiproxy_stub_map.MakeSyncCall(service, call, request, response, stubmap=None)[source]

The APIProxy entry point for a synchronous API call.

Args:
service: string representing which service to call call: string representing which function to call request: protocol buffer for the request response: protocol buffer for the response stubmap: optional APIProxyStubMap instance, for dependency injection.
Returns:
Response protocol buffer or None. Some implementations may return a response protocol buffer instead of modifying ‘response’. Caller must use returned value in such cases. If ‘response’ is modified then returns None.
Raises:
apiproxy_errors.Error or a subclass.
class google.appengine.api.apiproxy_stub_map.UserRPC(service, deadline=None, callback=None, stubmap=None)[source]

Bases: object

Wrapper class for asynchronous RPC.

Simplest low-level usage pattern:

rpc = UserRPC(‘service’, [deadline], [callback]) rpc.make_call(‘method’, request, response) . . . rpc.wait() rpc.check_success()

However, a service module normally provides a wrapper so that the typical usage pattern becomes more like this:

from google.appengine.api import service rpc = service.create_rpc([deadline], [callback]) service.make_method_call(rpc, [service-specific-args]) . . . rpc.wait() result = rpc.get_result()

The service.make_method_call() function sets a service- and method- specific hook function that is called by rpc.get_result() with the rpc object as its first argument, and service-specific value as its second argument. The hook function should call rpc.check_success() and then extract the user-level result from the rpc.result protobuffer. Additional arguments may be passed from make_method_call() to the get_result hook via the second argument.

Also note wait_any() and wait_all(), which wait for multiple RPCs.

class MyLocal[source]

Bases: thread._local

Class to hold per-thread class level attributes.

may_interrupt_wait = False
UserRPC.check_success()[source]

Check for success of the RPC, possibly raising an exception.

This function should be called at least once per RPC. If wait() hasn’t been called yet, it is called first. If the RPC caused an exceptional condition, an exception will be raised here. The first time check_success() is called, the postcall hooks are called.

UserRPC.deadline

Return the deadline, if set explicitly (otherwise None).

UserRPC.get_result()[source]

Get the result of the RPC, or possibly raise an exception.

This implies a call to check_success(). If a get-result hook was passed to make_call(), that hook is responsible for calling check_success(), and the return value of the hook is returned. Otherwise, check_success() is called directly and None is returned.

UserRPC.get_result_hook

Return the get-result hook function.

UserRPC.make_call(method, request, response, get_result_hook=None, user_data=None)[source]

Initiate a call.

Args:

method: The method name. request: The request protocol buffer. response: The response protocol buffer. get_result_hook: Optional get-result hook function. If not None,

this must be a function with exactly one argument, the RPC object (self). Its return value is returned from get_result().
user_data: Optional additional arbitrary data for the get-result
hook function. This can be accessed as rpc.user_data. The type of this value is up to the service module.

This function may only be called once per RPC object. It sends the request to the remote server, but does not wait for a response. This allows concurrent execution of the remote call and further local processing (e.g., making additional remote calls).

Before the call is initiated, the precall hooks are called.

UserRPC.method

Return the method name.

UserRPC.request

Return the request protocol buffer object.

UserRPC.response

Return the response protocol buffer object.

UserRPC.service

Return the service name.

UserRPC.state

Return the RPC state.

Possible values are attributes of apiproxy_rpc.RPC: IDLE, RUNNING, FINISHING.

UserRPC.user_data

Return the user data for the hook function.

UserRPC.wait()[source]

Wait for the call to complete, and call callback if needed.

This and wait_any()/wait_all() are the only time callback functions may be called. (However, note that check_success() and get_result() call wait().) Waiting for one RPC will not cause callbacks for other RPCs to be called. Callback functions may call check_success() and get_result().

Callbacks are called without arguments; if a callback needs access to the RPC object a Python nested function (a.k.a. closure) or a bound may be used. To facilitate this, the callback may be assigned after the RPC object is created (but before make_call() is called).

Note: don’t confuse callbacks with get-result hooks or precall and postcall hooks.

classmethod UserRPC.wait_all(rpcs)[source]

Wait until all given RPCs are finished.

This is a thin wrapper around wait_any() that loops until all given RPCs have finished.

Args:
rpcs: Iterable collection of UserRPC instances.
Returns:
None.
classmethod UserRPC.wait_any(rpcs)[source]

Wait until an RPC is finished.

Args:
rpcs: Iterable collection of UserRPC instances.
Returns:
A UserRPC instance, indicating the first RPC among the given RPCs that finished; or None, indicating that either an RPC not among the given RPCs finished in the mean time, or the iterable is empty.

NOTES:

  1. Repeatedly calling wait_any() with the same arguments will not make progress; it will keep returning the same RPC (the one that finished first). The callback, however, will only be called the first time the RPC finishes (which may be here or in the wait() method).
  2. It may return before any of the given RPCs finishes, if another pending RPC exists that is not included in the rpcs argument. In this case the other RPC’s callback will not be called. The motivation for this feature is that wait_any() may be used as a low-level building block for a variety of high-level constructs, some of which prefer to block for the minimal amount of time without busy-waiting.