Skip to content

Commit

Permalink
fix: fix rest transport unit test and required query prams handling (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vam-google authored Jul 12, 2021
1 parent 36e9c11 commit b793017
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class {{ service.name }}RestTransport({{ service.name }}Transport):
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
quota_project_id: Optional[str] = None,
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
always_use_jwt_access: Optional[bool] = False,
) -> None:
"""Instantiate the transport.

Expand Down Expand Up @@ -88,6 +89,7 @@ class {{ service.name }}RestTransport({{ service.name }}Transport):
host=host,
credentials=credentials,
client_info=client_info,
always_use_jwt_access=always_use_jwt_access,
)
self._session = AuthorizedSession(self._credentials, default_host=self.DEFAULT_HOST)
{% if service.has_lro %}
Expand Down Expand Up @@ -196,23 +198,17 @@ class {{ service.name }}RestTransport({{ service.name }}Transport):
if {{ method.input.ident }}.{{ field }} in request:
query_params['{{ field|camel_case }}'] = request.{{ field }}
{% else %}
if request.{{ field }}:
query_params['{{ field|camel_case }}'] = request.{{ field }}
query_params['{{ field|camel_case }}'] = request.{{ field }}
{% endif %}
{% endfor %}

# TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here
# discards default values
# TODO(yon-mg): add test for proper url encoded strings
query_params = ['{k}={v}'.format(k=k, v=v) for k, v in query_params.items()]
url += '?{}'.format('&'.join(query_params)).replace(' ', '+')

# Send the request
headers = dict(metadata)
headers['Content-Type'] = 'application/json'
response = self._session.{{ method.http_opt['verb'] }}(
url,
headers=headers,
params=query_params,
{% if 'body' in method.http_opt %}
data=body,
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ def test_{{ method.name|snake_case }}_rest_flattened():
assert len(req.mock_calls) == 1
_, http_call, http_params = req.mock_calls[0]
body = http_params.get('data')
params = http_params.get('params')
{% for key, field in method.flattened_fields.items() %}
{% if not field.oneof or field.proto3_optional %}
{% if field.ident|string() == 'timestamp_pb2.Timestamp' %}
Expand All @@ -1230,7 +1231,7 @@ def test_{{ method.name|snake_case }}_rest_flattened():
assert {% if field.field_pb is msg_field_pb %}{{ field.ident }}.to_json({{ field.name }}, including_default_value_fields=False, use_integers_for_enums=False)
{%- elif field.field_pb is str_field_pb %}{{ field.mock_value }}
{%- else %}str({{ field.mock_value }})
{%- endif %} in http_call[1] + str(body)
{%- endif %} in http_call[1] + str(body) + str(params)
{% endif %}
{% endif %}{% endfor %}

Expand Down

0 comments on commit b793017

Please sign in to comment.