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

[ENG-1314] Look into removing or upgrading validators dependency in Python SDK #316

Merged
merged 2 commits into from
Oct 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_check_url.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()
Loading