Content-Length: 605222 | pFad | http://github.com/googleapis/google-cloud-python/commit/0109bbd91e1d54b06fdb11eb8c2d6046e562b0c8

12 Moving datastore GAX/gRPC helpers out of _http and into dedicated mod… · googleapis/google-cloud-python@0109bbd · GitHub
Skip to content

Commit 0109bbd

Browse files
committed
Moving datastore GAX/gRPC helpers out of _http and into dedicated module.
1 parent c79fc92 commit 0109bbd

File tree

4 files changed

+510
-466
lines changed

4 files changed

+510
-466
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Copyright 2017 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Helpers for making API requests via GAX / gRPC."""
16+
17+
18+
import contextlib
19+
20+
from grpc import StatusCode
21+
22+
from google.cloud._helpers import make_insecure_stub
23+
from google.cloud._helpers import make_secure_stub
24+
from google.cloud import exceptions
25+
26+
from google.cloud.grpc.datastore.v1 import datastore_pb2_grpc
27+
28+
29+
_GRPC_ERROR_MAPPING = {
30+
StatusCode.UNKNOWN: exceptions.InternalServerError,
31+
StatusCode.INVALID_ARGUMENT: exceptions.BadRequest,
32+
StatusCode.DEADLINE_EXCEEDED: exceptions.GatewayTimeout,
33+
StatusCode.NOT_FOUND: exceptions.NotFound,
34+
StatusCode.ALREADY_EXISTS: exceptions.Conflict,
35+
StatusCode.PERMISSION_DENIED: exceptions.Forbidden,
36+
StatusCode.UNAUTHENTICATED: exceptions.Unauthorized,
37+
StatusCode.RESOURCE_EXHAUSTED: exceptions.TooManyRequests,
38+
StatusCode.FAILED_PRECONDITION: exceptions.PreconditionFailed,
39+
StatusCode.ABORTED: exceptions.Conflict,
40+
StatusCode.OUT_OF_RANGE: exceptions.BadRequest,
41+
StatusCode.UNIMPLEMENTED: exceptions.MethodNotImplemented,
42+
StatusCode.INTERNAL: exceptions.InternalServerError,
43+
StatusCode.UNAVAILABLE: exceptions.ServiceUnavailable,
44+
StatusCode.DATA_LOSS: exceptions.InternalServerError,
45+
}
46+
47+
48+
@contextlib.contextmanager
49+
def _grpc_catch_rendezvous():
50+
"""Remap gRPC exceptions that happen in context.
51+
52+
.. _code.proto: https://github.com/googleapis/googleapis/blob/\
53+
master/google/rpc/code.proto
54+
55+
Remaps gRPC exceptions to the classes defined in
56+
:mod:`~google.cloud.exceptions` (according to the description
57+
in `code.proto`_).
58+
"""
59+
try:
60+
yield
61+
except exceptions.GrpcRendezvous as exc:
62+
error_code = exc.code()
63+
error_class = _GRPC_ERROR_MAPPING.get(error_code)
64+
if error_class is None:
65+
raise
66+
else:
67+
raise error_class(exc.details())
68+
69+
70+
class _DatastoreAPIOverGRPC(object):
71+
"""Helper mapping datastore API methods.
72+
73+
Makes requests to send / receive protobuf content over gRPC.
74+
75+
Methods make bare API requests without any helpers for constructing
76+
the requests or parsing the responses.
77+
78+
:type connection: :class:`Connection`
79+
:param connection: A connection object that contains helpful
80+
information for making requests.
81+
82+
:type secure: bool
83+
:param secure: Flag indicating if a secure stub connection is needed.
84+
"""
85+
86+
def __init__(self, connection, secure):
87+
if secure:
88+
self._stub = make_secure_stub(connection.credentials,
89+
connection.USER_AGENT,
90+
datastore_pb2_grpc.DatastoreStub,
91+
connection.host)
92+
else:
93+
self._stub = make_insecure_stub(datastore_pb2_grpc.DatastoreStub,
94+
connection.host)
95+
96+
def lookup(self, project, request_pb):
97+
"""Perform a ``lookup`` request.
98+
99+
:type project: str
100+
:param project: The project to connect to. This is
101+
usually your project name in the cloud console.
102+
103+
:type request_pb: :class:`.datastore_pb2.LookupRequest`
104+
:param request_pb: The request protobuf object.
105+
106+
:rtype: :class:`.datastore_pb2.LookupResponse`
107+
:returns: The returned protobuf response object.
108+
"""
109+
request_pb.project_id = project
110+
with _grpc_catch_rendezvous():
111+
return self._stub.Lookup(request_pb)
112+
113+
def run_query(self, project, request_pb):
114+
"""Perform a ``runQuery`` request.
115+
116+
:type project: str
117+
:param project: The project to connect to. This is
118+
usually your project name in the cloud console.
119+
120+
:type request_pb: :class:`.datastore_pb2.RunQueryRequest`
121+
:param request_pb: The request protobuf object.
122+
123+
:rtype: :class:`.datastore_pb2.RunQueryResponse`
124+
:returns: The returned protobuf response object.
125+
"""
126+
request_pb.project_id = project
127+
with _grpc_catch_rendezvous():
128+
return self._stub.RunQuery(request_pb)
129+
130+
def begin_transaction(self, project, request_pb):
131+
"""Perform a ``beginTransaction`` request.
132+
133+
:type project: str
134+
:param project: The project to connect to. This is
135+
usually your project name in the cloud console.
136+
137+
:type request_pb:
138+
:class:`.datastore_pb2.BeginTransactionRequest`
139+
:param request_pb: The request protobuf object.
140+
141+
:rtype: :class:`.datastore_pb2.BeginTransactionResponse`
142+
:returns: The returned protobuf response object.
143+
"""
144+
request_pb.project_id = project
145+
with _grpc_catch_rendezvous():
146+
return self._stub.BeginTransaction(request_pb)
147+
148+
def commit(self, project, request_pb):
149+
"""Perform a ``commit`` request.
150+
151+
:type project: str
152+
:param project: The project to connect to. This is
153+
usually your project name in the cloud console.
154+
155+
:type request_pb: :class:`.datastore_pb2.CommitRequest`
156+
:param request_pb: The request protobuf object.
157+
158+
:rtype: :class:`.datastore_pb2.CommitResponse`
159+
:returns: The returned protobuf response object.
160+
"""
161+
request_pb.project_id = project
162+
with _grpc_catch_rendezvous():
163+
return self._stub.Commit(request_pb)
164+
165+
def rollback(self, project, request_pb):
166+
"""Perform a ``rollback`` request.
167+
168+
:type project: str
169+
:param project: The project to connect to. This is
170+
usually your project name in the cloud console.
171+
172+
:type request_pb: :class:`.datastore_pb2.RollbackRequest`
173+
:param request_pb: The request protobuf object.
174+
175+
:rtype: :class:`.datastore_pb2.RollbackResponse`
176+
:returns: The returned protobuf response object.
177+
"""
178+
request_pb.project_id = project
179+
with _grpc_catch_rendezvous():
180+
return self._stub.Rollback(request_pb)
181+
182+
def allocate_ids(self, project, request_pb):
183+
"""Perform an ``allocateIds`` request.
184+
185+
:type project: str
186+
:param project: The project to connect to. This is
187+
usually your project name in the cloud console.
188+
189+
:type request_pb: :class:`.datastore_pb2.AllocateIdsRequest`
190+
:param request_pb: The request protobuf object.
191+
192+
:rtype: :class:`.datastore_pb2.AllocateIdsResponse`
193+
:returns: The returned protobuf response object.
194+
"""
195+
request_pb.project_id = project
196+
with _grpc_catch_rendezvous():
197+
return self._stub.AllocateIds(request_pb)

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/googleapis/google-cloud-python/commit/0109bbd91e1d54b06fdb11eb8c2d6046e562b0c8

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy