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

Commit

Permalink
Merge pull request #283 from maticardenas/282-url-schema-loader
Browse files Browse the repository at this point in the history
feat: adding url schema loader functionality
  • Loading branch information
sondrelg authored Nov 5, 2022
2 parents ef24951 + dd3dda1 commit 1929ad3
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 166 deletions.
26 changes: 26 additions & 0 deletions openapi_tester/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import TYPE_CHECKING, cast
from urllib.parse import urlparse

import requests
import yaml
from django.urls import Resolver404, resolve
from django.utils.functional import cached_property
Expand Down Expand Up @@ -258,3 +259,28 @@ def load_schema(self) -> dict[str, Any]:
return cast(
"dict", json.loads(content) if ".json" in self.path else yaml.load(content, Loader=yaml.FullLoader)
)


class UrlStaticSchemaLoader(BaseSchemaLoader):
"""
Loads OpenAPI schema from an url static file.
"""

def __init__(self, url: str, field_key_map: dict[str, str] | None = None):
super().__init__(field_key_map=field_key_map)
self.url = url

def load_schema(self) -> dict[str, Any]:
"""
Loads a static OpenAPI schema from url, and parses it to a python dict.
:return: Schema contents as a dict
:raises: ImproperlyConfigured
"""
response = requests.get(self.url, timeout=20)
return cast(
"dict",
json.loads(response.content)
if ".json" in self.url
else yaml.load(response.content, Loader=yaml.FullLoader),
)
18 changes: 14 additions & 4 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 @@ -18,7 +19,12 @@
VALIDATE_WRITE_ONLY_RESPONSE_KEY_ERROR,
)
from openapi_tester.exceptions import DocumentationError, OpenAPISchemaError, UndocumentedSchemaSectionError
from openapi_tester.loaders import DrfSpectacularSchemaLoader, DrfYasgSchemaLoader, StaticSchemaLoader
from openapi_tester.loaders import (
DrfSpectacularSchemaLoader,
DrfYasgSchemaLoader,
StaticSchemaLoader,
UrlStaticSchemaLoader,
)
from openapi_tester.utils import lazy_combinations, normalize_schema_section
from openapi_tester.validators import (
validate_enum,
Expand Down Expand Up @@ -46,7 +52,7 @@
class SchemaTester:
"""Schema Tester: this is the base class of the library."""

loader: StaticSchemaLoader | DrfSpectacularSchemaLoader | DrfYasgSchemaLoader
loader: StaticSchemaLoader | DrfSpectacularSchemaLoader | DrfYasgSchemaLoader | UrlStaticSchemaLoader
validators: list[Callable[[dict, Any], str | None]]

def __init__(
Expand All @@ -70,7 +76,11 @@ def __init__(
self.validators = validators or []

if schema_file_path is not None:
self.loader = 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
Loading

0 comments on commit 1929ad3

Please sign in to comment.