Skip to content

Commit

Permalink
remove validators dependency from Python SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
dphuang2 committed Oct 30, 2023
1 parent 6e163cc commit 7bf33b7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ public void processOpts() {

if (!generateSourceCodeOnly) {
supportingFiles.add(new SupportingFile("poetry.lock." + templateExtension, "", "poetry.lock"));
supportingFiles.add(new SupportingFile("settings.json." + templateExtension, "", ".vscode/settings.json"));
supportingFiles.add(new SupportingFile("pyproject." + templateExtension, "", "pyproject.toml"));
// supportingFiles.add(new SupportingFile("tox." + templateExtension, "", "tox.ini"));
// supportingFiles.add(new SupportingFile("test-requirements." + templateExtension, "", "test-requirements.txt"));
Expand Down Expand Up @@ -441,6 +442,7 @@ public void processOpts() {
}

supportingFiles.add(new SupportingFile("test_simple." + templateExtension, testFolder, "test_simple.py"));
supportingFiles.add(new SupportingFile("test_check_url." + templateExtension, testFolder, "test_simple.py"));
supportingFiles.add(new SupportingFile("test_deprecation_warning." + templateExtension, testFolder, "test_deprecation_warning.py"));
supportingFiles.add(new SupportingFile("exceptions." + templateExtension, packagePath(), "exceptions.py"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import urllib.parse
{{/if}}


import validators
import re
from urllib.parse import urlparse
from http import client as http_client
from {{packageName}}.exceptions_base import ApiValueError
Expand Down Expand Up @@ -740,16 +740,21 @@ conf = {{{packageName}}}.Configuration(
self._base_path = check_url(value)
self.server_index = None

def check_url(url: str):
DOMAIN_REGEX = re.compile(
r'^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9](?::[0-9]{1,5})?$|^(?:[0-9]{1,3}\.){3}[0-9]{1,3}(?::[0-9]{1,5})?$'
)
def check_url(url: str) -> str:
parsed = urlparse(url)
if parsed.query != '':
raise InvalidHostConfigurationError(url, "query string is not allowed")
if parsed.fragment != '':
raise InvalidHostConfigurationError(url, "fragment is not allowed")
if parsed.scheme not in ["http", "https"]:
raise InvalidHostConfigurationError(url, 'scheme must be "http" or "https"'.format(parsed.scheme))
if not validators.url(url):
raise InvalidHostConfigurationError(url, "invalid url")
if (parsed.netloc == ''):
raise InvalidHostConfigurationError(url, "host is not set")
if not DOMAIN_REGEX.match(parsed.netloc):
raise InvalidHostConfigurationError(url, "Invalid domain")
if (url.endswith("/")):
return url[:-1]
return url
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ certifi = ">=2023.7.22"
python-dateutil = "^2.8.2"
typing_extensions = "^4.3.0"
urllib3 = "^1.26.7"
validators = "^0.20.0"
frozendict = "^2.3.4"
aiohttp = "^3.8.4"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"python.testing.pytestArgs": [],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# coding: utf-8

{{>partial_header}}

import unittest
from {{packageName}}.configuration import check_url
from {{packageName}}.exceptions import InvalidHostConfigurationError


class TestIsValidUrl(unittest.TestCase):
def test_valid_urls(self):
valid_urls = [
"http://www.example.com",
"https://www.example.com",
"http://example.com",
"https://example.com/path/to/resource",
"http://example.com:8080",
"https://example.co.uk",
"https://subdomain.example.com",
"https://api.example.com/v1/resource",
"https://example.com/path/to/resource/123",
"https://www.example.com:8080",
"https://www.example.com:8080/path/to/resource",
"http://sub.example.com:8080",
"http://deep.sub.domain.example.com",
"http://127.0.0.1:4010",
"https://deep.sub.domain.example.com:8080/path",
"http://example.io",
"https://example.app",
]
for url in valid_urls:
with self.subTest(url=url):
self.assertTrue(check_url(url))

def test_invalid_urls(self):
invalid_urls = [
"not_a_url",
"http:/example.com",
"http://",
"http://.com",
"example.com",
"http://example.com#fragment",
"www.example.com",
"https://example.com/path/to/resource?query=value",
"https://example.com/path/to/resource?query=value&key2=value2",
"https://",
"ftp://files.example.com",
"//example.com",
"https://example,com",
"https:/example.com",
"https:// example.com",
"https://example.com path",
"http://..com",
"https://..example.com",
"http://example..com",
"https://example.com./path",
"https://example.com..",
"http://:8080",
"https://example.com:",
"http://example.com:abc",
"https://.example.com",
"http://example.",
"https:// example:8080.com",
"http:// example.com:8080/path",
"https://:8080/path",
]
for url in invalid_urls:
with self.subTest(url=url):
with self.assertRaises(InvalidHostConfigurationError):
check_url(url)
raise Exception("URL should be invalid: " + url)


if __name__ == "__main__":
unittest.main()

0 comments on commit 7bf33b7

Please sign in to comment.