Skip to content
This repository was archived by the owner on Nov 19, 2023. It is now read-only.

Commit dd3dda1

Browse files
committed
chore: handling PR comments
using django's URLValidator using requests instead of httpx
1 parent 8b049f2 commit dd3dda1

9 files changed

+32
-198
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,3 @@ repos:
7474
- drf-spectacular
7575
- pylint
7676
- faker
77-
- httpx

openapi_tester/loaders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import TYPE_CHECKING, cast
1010
from urllib.parse import urlparse
1111

12-
import httpx
12+
import requests
1313
import yaml
1414
from django.urls import Resolver404, resolve
1515
from django.utils.functional import cached_property
@@ -277,7 +277,7 @@ def load_schema(self) -> dict[str, Any]:
277277
:return: Schema contents as a dict
278278
:raises: ImproperlyConfigured
279279
"""
280-
response = httpx.get(self.url)
280+
response = requests.get(self.url, timeout=20)
281281
return cast(
282282
"dict",
283283
json.loads(response.content)

openapi_tester/schema_tester.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from typing import TYPE_CHECKING, Any, Callable, cast
66

77
from django.conf import settings
8-
from django.core.exceptions import ImproperlyConfigured
8+
from django.core.exceptions import ImproperlyConfigured, ValidationError
9+
from django.core.validators import URLValidator
910

1011
from openapi_tester.constants import (
1112
INIT_ERROR,
@@ -24,7 +25,7 @@
2425
StaticSchemaLoader,
2526
UrlStaticSchemaLoader,
2627
)
27-
from openapi_tester.utils import is_path_an_url, lazy_combinations, normalize_schema_section
28+
from openapi_tester.utils import lazy_combinations, normalize_schema_section
2829
from openapi_tester.validators import (
2930
validate_enum,
3031
validate_format,
@@ -75,11 +76,11 @@ def __init__(
7576
self.validators = validators or []
7677

7778
if schema_file_path is not None:
78-
self.loader = (
79-
UrlStaticSchemaLoader(schema_file_path, field_key_map=field_key_map)
80-
if is_path_an_url(schema_file_path)
81-
else StaticSchemaLoader(schema_file_path, field_key_map=field_key_map)
82-
)
79+
try:
80+
URLValidator()(schema_file_path)
81+
self.loader = UrlStaticSchemaLoader(schema_file_path, field_key_map=field_key_map)
82+
except ValidationError:
83+
self.loader = StaticSchemaLoader(schema_file_path, field_key_map=field_key_map)
8384
elif "drf_spectacular" in settings.INSTALLED_APPS:
8485
self.loader = DrfSpectacularSchemaLoader(field_key_map=field_key_map)
8586
elif "drf_yasg" in settings.INSTALLED_APPS:

openapi_tester/utils.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44
from __future__ import annotations
55

6-
import re
76
from copy import deepcopy
87
from itertools import chain, combinations
98
from typing import TYPE_CHECKING
@@ -59,17 +58,3 @@ def lazy_combinations(options_list: Sequence[dict[str, Any]]) -> Iterator[dict]:
5958
for i in range(2, len(options_list) + 1):
6059
for combination in combinations(options_list, i):
6160
yield merge_objects(combination)
62-
63-
64-
def is_path_an_url(path: str) -> bool:
65-
regex = re.compile(
66-
r"^(?:http|ftp)s?://" # http:// or https://
67-
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" # domain
68-
r"(?:[A-Za-z._-]+)|" # any hostname
69-
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # or ip
70-
r"(?::\d+)?" # optional port
71-
r"(?:/?|[/?]\S+)$",
72-
re.IGNORECASE,
73-
)
74-
75-
return re.match(regex, path) is not None

poetry.lock

Lines changed: 12 additions & 141 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ prance = "*"
5757
pyYAML = "*"
5858
drf-spectacular = { version = "*", optional = true }
5959
drf-yasg = { version = "*", optional = true }
60-
httpx = "^0.23.0"
61-
pytest-httpx = "^0.21.2"
6260

6361
[tool.poetry.extras]
6462
drf-yasg = ["drf-yasg"]

0 commit comments

Comments
 (0)