Skip to content

Commit f5d1a6d

Browse files
authored
Let the transport handles bad urls (#12106)
* Let the transport handle bad urls * Typo * ChangeLog * Tests * Make it better * Pylint
1 parent c01658b commit f5d1a6d

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

sdk/core/azure-core/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
### Bug fixes
77

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

1011
## 1.6.0 (2020-06-03)
1112

sdk/core/azure-core/azure/core/pipeline/transport/_base.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ def _case_insensitive_dict(*args, **kwargs):
112112

113113

114114
def _format_url_section(template, **kwargs):
115+
"""String format the template with the kwargs, auto-skip sections of the template that are NOT in the kwargs.
116+
117+
By default in Python, "format" will raise a KeyError if a template element is not found. Here the section between
118+
the slashes will be removed from the template instead.
119+
120+
This is used for API like Storage, where when Swagger has template section not defined as parameter.
121+
122+
:param str template: a string template to fill
123+
:param dict[str,str] kwargs: Template values as string
124+
:rtype: str
125+
:returns: Template completed
126+
"""
115127
components = template.split("/")
116128
while components:
117129
try:
@@ -718,7 +730,12 @@ def format_url(self, url_template, **kwargs):
718730
parsed = urlparse(url)
719731
if not parsed.scheme or not parsed.netloc:
720732
url = url.lstrip("/")
721-
base = self._base_url.format(**kwargs).rstrip("/")
733+
try:
734+
base = self._base_url.format(**kwargs).rstrip("/")
735+
except KeyError as key:
736+
err_msg = "The value provided for the url part {} was incorrect, and resulted in an invalid url"
737+
raise ValueError(err_msg.format(key.args[0]))
738+
722739
url = _urljoin(base, url)
723740
else:
724741
url = self._base_url.format(**kwargs)

sdk/core/azure-core/tests/test_pipeline.py

+6
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ def test_format_url_no_base_url(self):
204204
formatted = client.format_url("https://google.com/subpath/{foo}", foo="bar")
205205
assert formatted == "https://google.com/subpath/bar"
206206

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

208214
class TestClientRequest(unittest.TestCase):
209215
def test_request_json(self):

0 commit comments

Comments
 (0)