Skip to content
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

Let the transport handles bad urls #12106

Merged
merged 6 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
### Bug fixes

- `AzureKeyCredentialPolicy` will now accept (and ignore) passed in kwargs #11963
- Better error messages if passed endpoint is incorrect #12106

## 1.6.0 (2020-06-03)

Expand Down
19 changes: 18 additions & 1 deletion sdk/core/azure-core/azure/core/pipeline/transport/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ def _case_insensitive_dict(*args, **kwargs):


def _format_url_section(template, **kwargs):
"""String format the template with the kwargs, auto-skip sections of the template that are NOT in the kwargs.

By default in Python, "format" will raise a KeyError if a template element is not found. Here the section between
the slashes will be removed from the template instead.

This is used for API like Storage, where when Swagger has template section not defined as parameter.

:param str template: a string template to fill
:param dict[str,str] kwargs: Template values as string
:rtype: str
:returns: Template completed
"""
components = template.split("/")
while components:
try:
Expand Down Expand Up @@ -718,7 +730,12 @@ def format_url(self, url_template, **kwargs):
parsed = urlparse(url)
if not parsed.scheme or not parsed.netloc:
url = url.lstrip("/")
base = self._base_url.format(**kwargs).rstrip("/")
try:
base = self._base_url.format(**kwargs).rstrip("/")
except KeyError as key:
err_msg = "The value provided for the url part {} was incorrect, and resulted in an invalid url"
raise ValueError(err_msg.format(key.args[0]))

url = _urljoin(base, url)
else:
url = self._base_url.format(**kwargs)
Expand Down
6 changes: 6 additions & 0 deletions sdk/core/azure-core/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ def test_format_url_no_base_url(self):
formatted = client.format_url("https://google.com/subpath/{foo}", foo="bar")
assert formatted == "https://google.com/subpath/bar"

def test_format_incorrect_endpoint(self):
# https://github.com/Azure/azure-sdk-for-python/pull/12106
client = PipelineClientBase('{Endpoint}/text/analytics/v3.0')
with pytest.raises(ValueError) as exp:
client.format_url("foo/bar")
assert str(exp.value) == "The value provided for the url part Endpoint was incorrect, and resulted in an invalid url"

class TestClientRequest(unittest.TestCase):
def test_request_json(self):
Expand Down