Skip to content

Commit

Permalink
Let the transport handles bad urls (#12106)
Browse files Browse the repository at this point in the history
* Let the transport handle bad urls

* Typo

* ChangeLog

* Tests

* Make it better

* Pylint
  • Loading branch information
lmazuel authored Jun 18, 2020
1 parent c01658b commit f5d1a6d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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

0 comments on commit f5d1a6d

Please sign in to comment.