|
14 | 14 |
|
15 | 15 | """Connections to Google Cloud Datastore API servers.""" |
16 | 16 |
|
17 | | -import os |
18 | | - |
19 | 17 | from google.rpc import status_pb2 |
20 | 18 |
|
21 | 19 | from google.cloud import _http as connection_module |
22 | | -from google.cloud.environment_vars import DISABLE_GRPC |
23 | 20 | from google.cloud import exceptions |
24 | 21 | from google.cloud.proto.datastore.v1 import datastore_pb2 as _datastore_pb2 |
25 | 22 |
|
26 | 23 | from google.cloud.datastore import __version__ |
27 | | -try: |
28 | | - from google.cloud.datastore._gax import _DatastoreAPIOverGRPC |
29 | | - _HAVE_GRPC = True |
30 | | -except ImportError: # pragma: NO COVER |
31 | | - _DatastoreAPIOverGRPC = None |
32 | | - _HAVE_GRPC = False |
33 | 24 |
|
34 | 25 |
|
35 | 26 | DATASTORE_API_HOST = 'datastore.googleapis.com' |
|
42 | 33 | '/{project}:{method}') |
43 | 34 | """A template for the URL of a particular API call.""" |
44 | 35 |
|
45 | | -_DISABLE_GRPC = os.getenv(DISABLE_GRPC, False) |
46 | | -_USE_GRPC = _HAVE_GRPC and not _DISABLE_GRPC |
47 | 36 | _CLIENT_INFO = connection_module.CLIENT_INFO_TEMPLATE.format(__version__) |
48 | 37 |
|
49 | 38 |
|
@@ -148,121 +137,45 @@ def build_api_url(project, method, base_url): |
148 | 137 | project=project, method=method) |
149 | 138 |
|
150 | 139 |
|
151 | | -class _DatastoreAPIOverHttp(object): |
152 | | - """Helper mapping datastore API methods. |
153 | | -
|
154 | | - Makes requests to send / receive protobuf content over HTTP/1.1. |
| 140 | +class HTTPDatastoreAPI(object): |
| 141 | + """An API object that sends proto-over-HTTP requests. |
155 | 142 |
|
156 | | - Methods make bare API requests without any helpers for constructing |
157 | | - the requests or parsing the responses. |
| 143 | + Intended to provide the same methods as the GAPIC ``DatastoreClient``. |
158 | 144 |
|
159 | | - :type connection: :class:`Connection` |
160 | | - :param connection: A connection object that contains helpful |
161 | | - information for making requests. |
| 145 | + :type client: :class:`~google.cloud.datastore.client.Client` |
| 146 | + :param client: The client that provides configuration. |
162 | 147 | """ |
163 | 148 |
|
164 | | - def __init__(self, connection): |
165 | | - self.connection = connection |
| 149 | + def __init__(self, client): |
| 150 | + self.client = client |
166 | 151 |
|
167 | | - def lookup(self, project, request_pb): |
| 152 | + def lookup(self, project, read_options, key_pbs): |
168 | 153 | """Perform a ``lookup`` request. |
169 | 154 |
|
170 | 155 | :type project: str |
171 | 156 | :param project: The project to connect to. This is |
172 | 157 | usually your project name in the cloud console. |
173 | 158 |
|
174 | | - :type request_pb: :class:`.datastore_pb2.LookupRequest` |
175 | | - :param request_pb: The request protobuf object. |
176 | | -
|
177 | | - :rtype: :class:`.datastore_pb2.LookupResponse` |
178 | | - :returns: The returned protobuf response object. |
179 | | - """ |
180 | | - return _rpc(self.connection.http, project, 'lookup', |
181 | | - self.connection.api_base_url, |
182 | | - request_pb, _datastore_pb2.LookupResponse) |
183 | | - |
184 | | - |
185 | | -class Connection(connection_module.Connection): |
186 | | - """A connection to the Google Cloud Datastore via the Protobuf API.
9E81
|
187 | | -
|
188 | | - This class should understand only the basic types (and protobufs) |
189 | | - in method arguments, however it should be capable of returning advanced |
190 | | - types. |
191 | | -
|
192 | | - :type client: :class:`~google.cloud.datastore.client.Client` |
193 | | - :param client: The client that owns the current connection. |
194 | | - """ |
195 | | - |
196 | | - def __init__(self, client): |
197 | | - super(Connection, self).__init__(client) |
198 | | - self.api_base_url = client._base_url |
199 | | - if _USE_GRPC: |
200 | | - self._datastore_api = _DatastoreAPIOverGRPC(self) |
201 | | - else: |
202 | | - self._datastore_api = _DatastoreAPIOverHttp(self) |
203 | | - |
204 | | - def lookup(self, project, key_pbs, |
205 | | - eventual=False, transaction_id=None): |
206 | | - """Lookup keys from a project in the Cloud Datastore. |
207 | | -
|
208 | | - Maps the ``DatastoreService.Lookup`` protobuf RPC. |
209 | | -
|
210 | | - This uses mostly protobufs |
211 | | - (:class:`.entity_pb2.Key` as input and :class:`.entity_pb2.Entity` |
212 | | - as output). It is used under the hood in |
213 | | - :meth:`Client.get() <.datastore.client.Client.get>`: |
214 | | -
|
215 | | - .. code-block:: python |
216 | | -
|
217 | | - >>> from google.cloud import datastore |
218 | | - >>> client = datastore.Client(project='project') |
219 | | - >>> key = client.key('MyKind', 1234) |
220 | | - >>> client.get(key) |
221 | | - [<Entity object>] |
222 | | -
|
223 | | - Using a :class:`Connection` directly: |
224 | | -
|
225 | | - .. code-block:: python |
226 | | -
|
227 | | - >>> connection.lookup('project', [key.to_protobuf()]) |
228 | | - [<Entity protobuf>] |
229 | | -
|
230 | | - :type project: str |
231 | | - :param project: The project to look up the keys in. |
| 159 | + :type read_options: :class:`.datastore_pb2.ReadOptions` |
| 160 | + :param read_options: The options for this lookup. Contains a |
| 161 | + either the transaction for the read or |
| 162 | + ``STRONG`` or ``EVENTUAL`` read consistency. |
232 | 163 |
|
233 | 164 | :type key_pbs: list of |
234 | 165 | :class:`.entity_pb2.Key` |
235 | 166 | :param key_pbs: The keys to retrieve from the datastore. |
236 | 167 |
|
237 | | - :type eventual: bool |
238 | | - :param eventual: If False (the default), request ``STRONG`` read |
239 | | - consistency. If True, request ``EVENTUAL`` read |
240 | | - consistency. |
241 | | -
|
242 | | - :type transaction_id: str |
243 | | - :param transaction_id: If passed, make the request in the scope of |
244 | | - the given transaction. Incompatible with |
245 | | - ``eventual==True``. |
246 | | -
|
247 | 168 | :rtype: :class:`.datastore_pb2.LookupResponse` |
248 | | - :returns: The returned protobuf for the lookup request. |
| 169 | + :returns: The returned protobuf response object. |
249 | 170 | """ |
250 | | - lookup_request = _datastore_pb2.LookupRequest(keys=key_pbs) |
251 | | - _set_read_options(lookup_request, eventual, transaction_id) |
252 | | - return self._datastore_api.lookup(project, lookup_request) |
253 | | - |
254 | | - |
255 | | -class HTTPDatastoreAPI(object): |
256 | | - """An API object that sends proto-over-HTTP requests. |
257 | | -
|
258 | | - Intended to provide the same methods as the GAPIC ``DatastoreClient``. |
259 | | -
|
260 | | - :type client: :class:`~google.cloud.datastore.client.Client` |
261 | | - :param client: The client that provides configuration. |
262 | | - """ |
263 | | - |
264 | | - def __init__(self, client): |
265 | | - self.client = client |
| 171 | + request_pb = _datastore_pb2.LookupRequest( |
| 172 | + project_id=project, |
| 173 | + read_options=read_options, |
| 174 | + keys=key_pbs, |
| 175 | + ) |
| 176 | + return _rpc(self.client._http, project, 'lookup', |
| 177 | + self.client._base_url, |
| 178 | + request_pb, _datastore_pb2.LookupResponse) |
266 | 179 |
|
267 | 180 | def run_query(self, project, partition_id, read_options, |
268 | 181 | query=None, gql_query=None): |
@@ -390,21 +303,3 @@ def allocate_ids(self, project, key_pbs): |
390 | 303 | return _rpc(self.client._http, project, 'allocateIds', |
391 | 304 | self.client._base_url, |
392 | 305 | request_pb, _datastore_pb2.AllocateIdsResponse) |
393 | | - |
394 | | - |
395 | | -def _set_read_options(request, eventual, transaction_id): |
396 | | - """Validate rules for read options, and assign to the request. |
397 | | -
|
398 | | - Helper method for ``lookup()`` and ``run_query``. |
399 | | -
|
400 | | - :raises: :class:`ValueError` if ``eventual`` is ``True`` and the |
401 | | - ``transaction_id`` is not ``None``. |
402 | | - """ |
403 | | - if eventual and (transaction_id is not None): |
404 | | - raise ValueError('eventual must be False when in a transaction') |
405 | | - |
406 | | - opts = request.read_options |
407 | | - if eventual: |
408 | | - opts.read_consistency = _datastore_pb2.ReadOptions.EVENTUAL |
409 | | - elif transaction_id: |
410 | | - opts.transaction = transaction_id |
0 commit comments