Client¶
A client for NDB which manages credentials, project, namespace.
-
class
google.cloud.ndb.client.
Client
(project=None, namespace=None, credentials=None)[source]¶ Bases:
google.cloud.client.ClientWithProject
An NDB client.
The NDB client must be created in order to use NDB, and any use of NDB must be within the context of a call to
context()
.- Parameters
project (Optional[str]) – The project to pass to proxied API methods. If not passed, falls back to the default inferred from the environment.
namespace (Optional[str]) – Namespace to pass to proxied API methods.
credentials (Optional[
Credentials
]) – The OAuth2 Credentials to use for this client. If not passed, falls back to the default inferred from the environment.
-
SCOPE
= ('https://www.googleapis.com/auth/datastore',)¶ The scopes required for authenticating as a Cloud Datastore consumer.
-
context
(cache_policy=None, global_cache=None, global_cache_policy=None, global_cache_timeout_policy=None, legacy_data=True)[source]¶ Establish a context for a set of NDB calls.
This method provides a context manager which establishes the runtime state for using NDB.
For example:
from google.cloud import ndb client = ndb.Client() with client.context(): # Use NDB for some stuff pass
Use of a context is required–NDB can only be used inside a running context. The context is used to manage the connection to Google Cloud Datastore, an event loop for asynchronous API calls, runtime caching policy, and other essential runtime state.
Code within an asynchronous context should be single threaded. Internally, a
threading.local
instance is used to track the current event loop.In a web application, it is recommended that a single context be used per HTTP request. This can typically be accomplished in a middleware layer.
- Parameters
cache_policy (Optional[Callable[[key.Key], bool]]) – The cache policy to use in this context. See:
set_cache_policy()
.global_cache (Optional[global_cache.GlobalCache]) – The global cache for this context. See:
GlobalCache
.global_cache_policy (Optional[Callable[[key.Key], bool]]) – The global cache policy to use in this context. See:
set_global_cache_policy()
.global_cache_timeout_policy (Optional[Callable[[key.Key], int]]) – The global cache timeout to use in this context. See:
set_global_cache_timeout_policy()
.legacy_data (bool) – Set to
True
(the default) to write data in a way that can be read by the legacy version of NDB.