|
| 1 | +{# |
| 2 | + # Copyright (C) 2024 Google LLC |
| 3 | + # |
| 4 | + # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + # you may not use this file except in compliance with the License. |
| 6 | + # You may obtain a copy of the License at |
| 7 | + # |
| 8 | + # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + # |
| 10 | + # Unless required by applicable law or agreed to in writing, software |
| 11 | + # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + # See the License for the specific language governing permissions and |
| 14 | + # limitations under the License. |
| 15 | +#} |
| 16 | + |
| 17 | +{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %} |
| 18 | + |
1 | 19 | {% if "rest" in opts.transport %} |
2 | 20 |
|
3 | 21 | {% for name, sig in api.mixin_api_signatures.items() %} |
4 | 22 | @property |
5 | 23 | def {{ name|snake_case }}(self): |
6 | 24 | return self._{{ name }}(self._session, self._host, self._interceptor) # type: ignore |
7 | 25 |
|
8 | | - class _{{ name }}({{service.name}}RestStub): |
| 26 | + class _{{ name }}(_Base{{ service.name }}RestTransport._Base{{name}}, {{service.name}}RestStub): |
| 27 | + {% set body_spec = api.mixin_http_options["{}".format(name)][0].body %} |
| 28 | + {{ shared_macros.response_method(body_spec)|indent(8) }} |
| 29 | + |
9 | 30 | def __call__(self, |
10 | 31 | request: {{ sig.request_type }}, *, |
11 | 32 | retry: OptionalRetry=gapic_v1.method.DEFAULT, |
|
32 | 53 | {{ sig.response_type }}: Response from {{ name }} method. |
33 | 54 | {% endif %} |
34 | 55 | """ |
35 | | - |
36 | | - http_options: List[Dict[str, str]] = [ |
37 | | - {%- for rule in api.mixin_http_options["{}".format(name)] %}{ |
38 | | - 'method': '{{ rule.method }}', |
39 | | - 'uri': '{{ rule.uri }}', |
40 | | - {% if rule.body %} |
41 | | - 'body': '{{ rule.body }}', |
42 | | - {% endif %}{# rule.body #} |
43 | | - }, |
44 | | - {% endfor %} |
45 | | - ] |
46 | | - |
47 | | - request, metadata = self._interceptor.pre_{{ name|snake_case }}(request, metadata) |
48 | | - request_kwargs = json_format.MessageToDict(request) |
49 | | - transcoded_request = path_template.transcode( |
50 | | - http_options, **request_kwargs) |
51 | | - |
52 | | - {% set body_spec = api.mixin_http_options["{}".format(name)][0].body %} |
53 | | - {%- if body_spec %} |
54 | | - body = json.dumps(transcoded_request['body']) |
55 | | - {%- endif %} |
56 | | - |
57 | | - uri = transcoded_request['uri'] |
58 | | - method = transcoded_request['method'] |
59 | | - |
60 | | - # Jsonify the query params |
61 | | - query_params = json.loads(json.dumps(transcoded_request['query_params'])) |
62 | | - |
63 | | - # Send the request |
64 | | - headers = dict(metadata) |
65 | | - headers['Content-Type'] = 'application/json' |
66 | | - |
67 | | - response = getattr(self._session, method)( |
68 | | - "{host}{uri}".format(host=self._host, uri=uri), |
69 | | - timeout=timeout, |
70 | | - headers=headers, |
71 | | - params=rest_helpers.flatten_query_params(query_params), |
72 | | - {% if body_spec %} |
73 | | - data=body, |
74 | | - {% endif %} |
75 | | - ) |
76 | | - |
77 | | - # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception |
78 | | - # subclass. |
79 | | - if response.status_code >= 400: |
80 | | - raise core_exceptions.from_http_response(response) |
| 56 | + {{ shared_macros.rest_call_method_common(body_spec, name, service.name)|indent(8) }} |
81 | 57 |
|
82 | 58 | {% if sig.response_type == "None" %} |
83 | 59 | return self._interceptor.post_{{ name|snake_case }}(None) |
|
0 commit comments