-
Notifications
You must be signed in to change notification settings - Fork 79
feat: Determine URL protocol based on hostname #900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7dc5ce3
f6e383f
02e5fd3
0141d94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| {% block content %} | ||
|
|
||
| import re | ||
| import warnings | ||
| from typing import Callable, Dict, Optional, Sequence, Tuple | ||
|
|
||
|
|
@@ -180,7 +181,8 @@ class {{ service.name }}RestTransport({{ service.name }}Transport): | |
| {# TODO(yon-mg): Write helper method for handling grpc transcoding url #} | ||
| # TODO(yon-mg): need to handle grpc transcoding and parse url correctly | ||
| # current impl assumes basic case of grpc transcoding | ||
| url = 'https://{host}{{ method.http_opt['url'] }}'.format( | ||
| url = '{protocol}://{host}{{ method.http_opt['url'] }}'.format( | ||
| protocol=_protocol_for(self._host), | ||
| host=self._host, | ||
| {% for field in method.path_params %} | ||
| {{ field }}=request.{{ method.input.get_field(field).name }}, | ||
|
|
@@ -229,6 +231,29 @@ class {{ service.name }}RestTransport({{ service.name }}Transport): | |
| {% endif %} | ||
| {% endfor %} | ||
|
|
||
| {# TODO: Allow client creation to explictly specify connection | ||
| parameters, such as secure/insecure connections, and have tests use | ||
| that. Then remove this function. | ||
| Tracking issue: | ||
| https://github.com/googleapis/gapic-generator-python/issues/901 | ||
| #} | ||
| def _protocol_for(host: str) -> str: | ||
| """Determine the URL protocol to use for connecting to the given host. | ||
|
|
||
| This function returns 'http' for hosts that match | ||
| `^localhost(:.*)?$` and `https` for all others. This is intended | ||
| to allow integration testing over an insecure connection to a | ||
| locally running server. | ||
|
|
||
| Args: | ||
| host (str): The hostname for REST connection. | ||
|
|
||
| Returns: | ||
| str: The protocol (`http` or `https`) to use for connecting to `host`. | ||
| """ | ||
| if not hasattr(_protocol_for, 'regex'): | ||
| _protocol_for.regex = re.compile("^localhost(:.*)?$") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this regex be executed for every call made? If yes, it will have some performance implications (not big probably, but here we sacrifice production code to support rare testing case, which is suboptimal).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The compilation of the regex will only happen once because I have it memoized via this attribute. The matching will happen for every call for now, though we could refactor things so the protocol is only computed once per client and stored in an attribute. However, given that the performance hit is small, I'm inclined to think it's preferable to keep things simple. |
||
| return 'http' if _protocol_for.regex.match(host) else 'https' | ||
|
|
||
| __all__ = ( | ||
| '{{ service.name }}RestTransport', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have anything like this in the other languages? Overall this looks suspicious I would prefer to not have something like this. Determining protocol from host name does not seem reliable and also we are modifying production code for testing purposes. Also what if the host is specified as an IP (land specifically 127.0.0.1)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. This was discussed in an internal doc; I'll ping you the link. We're specifically looking for
localhostbecause yes, this is a temporary special case to allow testing (see #901 for the long-term solution)