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

Commit

Permalink
chore: handling PR comments
Browse files Browse the repository at this point in the history
using django's URLValidator

using requests instead of httpx
  • Loading branch information
maticardenas committed Nov 5, 2022
1 parent 8b049f2 commit dd3dda1
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 198 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,3 @@ repos:
- drf-spectacular
- pylint
- faker
- httpx
4 changes: 2 additions & 2 deletions openapi_tester/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import TYPE_CHECKING, cast
from urllib.parse import urlparse

import httpx
import requests
import yaml
from django.urls import Resolver404, resolve
from django.utils.functional import cached_property
Expand Down Expand Up @@ -277,7 +277,7 @@ def load_schema(self) -> dict[str, Any]:
:return: Schema contents as a dict
:raises: ImproperlyConfigured
"""
response = httpx.get(self.url)
response = requests.get(self.url, timeout=20)
return cast(
"dict",
json.loads(response.content)
Expand Down
15 changes: 8 additions & 7 deletions openapi_tester/schema_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from typing import TYPE_CHECKING, Any, Callable, cast

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.validators import URLValidator

from openapi_tester.constants import (
INIT_ERROR,
Expand All @@ -24,7 +25,7 @@
StaticSchemaLoader,
UrlStaticSchemaLoader,
)
from openapi_tester.utils import is_path_an_url, lazy_combinations, normalize_schema_section
from openapi_tester.utils import lazy_combinations, normalize_schema_section
from openapi_tester.validators import (
validate_enum,
validate_format,
Expand Down Expand Up @@ -75,11 +76,11 @@ def __init__(
self.validators = validators or []

if schema_file_path is not None:
self.loader = (
UrlStaticSchemaLoader(schema_file_path, field_key_map=field_key_map)
if is_path_an_url(schema_file_path)
else StaticSchemaLoader(schema_file_path, field_key_map=field_key_map)
)
try:
URLValidator()(schema_file_path)
self.loader = UrlStaticSchemaLoader(schema_file_path, field_key_map=field_key_map)
except ValidationError:
self.loader = StaticSchemaLoader(schema_file_path, field_key_map=field_key_map)
elif "drf_spectacular" in settings.INSTALLED_APPS:
self.loader = DrfSpectacularSchemaLoader(field_key_map=field_key_map)
elif "drf_yasg" in settings.INSTALLED_APPS:
Expand Down
15 changes: 0 additions & 15 deletions openapi_tester/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
from __future__ import annotations

import re
from copy import deepcopy
from itertools import chain, combinations
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -59,17 +58,3 @@ def lazy_combinations(options_list: Sequence[dict[str, Any]]) -> Iterator[dict]:
for i in range(2, len(options_list) + 1):
for combination in combinations(options_list, i):
yield merge_objects(combination)


def is_path_an_url(path: str) -> bool:
regex = re.compile(
r"^(?:http|ftp)s?://" # http:// or https://
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" # domain
r"(?:[A-Za-z._-]+)|" # any hostname
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # or ip
r"(?::\d+)?" # optional port
r"(?:/?|[/?]\S+)$",
re.IGNORECASE,
)

return re.match(regex, path) is not None
153 changes: 12 additions & 141 deletions poetry.lock

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

2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ prance = "*"
pyYAML = "*"
drf-spectacular = { version = "*", optional = true }
drf-yasg = { version = "*", optional = true }
httpx = "^0.23.0"
pytest-httpx = "^0.21.2"

[tool.poetry.extras]
drf-yasg = ["drf-yasg"]
Expand Down
Loading

0 comments on commit dd3dda1

Please sign in to comment.