Skip to content

Commit 2fc2b35

Browse files
committed
feat: add suport for mapping exceptions to rest callables
1 parent 7ccbf57 commit 2fc2b35

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

google/api_core/gapic_v1/method_async.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import functools
2121

22-
from google.api_core import grpc_helpers_async
22+
from google.api_core import grpc_helpers_async, rest_helpers_async
2323
from google.api_core.gapic_v1 import client_info
2424
from google.api_core.gapic_v1.method import _GapicCallable
2525
from google.api_core.gapic_v1.method import DEFAULT # noqa: F401
@@ -32,6 +32,7 @@ def wrap_method(
3232
default_timeout=None,
3333
default_compression=None,
3434
client_info=client_info.DEFAULT_CLIENT_INFO,
35+
kind="grpc"
3536
):
3637
"""Wrap an async RPC method with common behavior.
3738
@@ -40,7 +41,10 @@ def wrap_method(
4041
and ``compression`` arguments and applies the common error mapping,
4142
retry, timeout, metadata, and compression behavior to the low-level RPC method.
4243
"""
43-
func = grpc_helpers_async.wrap_errors(func)
44+
if kind == "rest":
45+
func = rest_helpers_async.wrap_errors(func)
46+
else:
47+
func = grpc_helpers_async.wrap_errors(func)
4448

4549
metadata = [client_info.to_grpc_metadata()] if client_info is not None else None
4650

google/api_core/rest_helpers_async.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2024 Google LLC
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+
"""AsyncIO helpers for :mod:`rest` supporting 3.7+.
16+
"""
17+
18+
19+
import functools
20+
21+
import google.auth
22+
from google.api_core import exceptions
23+
24+
25+
26+
class _RestCall():
27+
"""Wrapped Rest Call to map exceptions."""
28+
29+
def __init__(self):
30+
self._call = None
31+
32+
def with_call(self, call):
33+
"""Supplies the call object separately to keep __init__ clean."""
34+
self._call = call
35+
return self
36+
37+
async def __call__(self):
38+
try:
39+
return await self._call
40+
except google.auth.exceptions.TransportError as rpc_exc:
41+
raise exceptions.GoogleAPICallError(str(rpc_exc), errors=(rpc_exc,), response=rpc_exc)
42+
43+
44+
def wrap_errors(callable_):
45+
"""Map errors for REST async callables."""
46+
47+
@functools.wraps(callable_)
48+
async def error_remapped_callable(*args, **kwargs):
49+
call = callable_(*args, **kwargs)
50+
call = _RestCall().with_call(call)
51+
response = await call()
52+
return response
53+
54+
return error_remapped_callable

0 commit comments

Comments
 (0)