Skip to content

Commit

Permalink
Use existing code to perform download, added docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mjp4 committed Feb 15, 2024
1 parent 27e753c commit c19743e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions connexion/apps/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ def add_api(
Register an API represented by a single OpenAPI specification on this application.
Multiple APIs can be registered on a single application.
:param specification: OpenAPI specification. Can be provided either as dict, or as path
to file.
:param specification: OpenAPI specification. Can be provided either as dict, a path
to file, or a URL.
:param base_path: Base path to host the API. This overrides the basePath / servers in the
specification.
:param name: Name to register the API with. If no name is passed, the base_path is used
Expand Down
11 changes: 7 additions & 4 deletions connexion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,13 @@ def create_app(args: t.Optional[argparse.Namespace] = None) -> AbstractApp:

logging.basicConfig(level=logging_level)

spec_file_full_path = os.path.abspath(args.spec_file)
py_module_path = args.base_module_path or os.path.dirname(spec_file_full_path)
sys.path.insert(1, os.path.abspath(py_module_path))
logger.debug(f"Added {py_module_path} to system path.")
if args.spec_file.startswith("http") or args.spec_file.startswith("https"):
spec_file_full_path = args.spec_file
else:
spec_file_full_path = os.path.abspath(args.spec_file)
py_module_path = args.base_module_path or os.path.dirname(spec_file_full_path)
sys.path.insert(1, os.path.abspath(py_module_path))
logger.debug(f"Added {py_module_path} to system path.")

resolver_error = None
if args.stub:
Expand Down
4 changes: 2 additions & 2 deletions connexion/middleware/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ def add_api(
Register een API represented by a single OpenAPI specification on this middleware.
Multiple APIs can be registered on a single middleware.
:param specification: OpenAPI specification. Can be provided either as dict, or as path
to file.
:param specification: OpenAPI specification. Can be provided either as dict, a path
to file, or a URL.
:param base_path: Base path to host the API. This overrides the basePath / servers in the
specification.
:param name: Name to register the API with. If no name is passed, the base_path is used
Expand Down
15 changes: 5 additions & 10 deletions connexion/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@
import os
import pathlib
import pkgutil
import tempfile
import typing as t
from collections.abc import Mapping
from urllib.parse import urlsplit

import jinja2
import jsonschema
import requests
import yaml
from jsonschema import Draft4Validator
from jsonschema.validators import extend as extend_validator

from .exceptions import InvalidSpecification
from .json_schema import NullableTypeValidator, resolve_refs
from .json_schema import NullableTypeValidator, URLHandler, resolve_refs
from .operations import AbstractOperation, OpenAPIOperation, Swagger2Operation
from .utils import deep_get

Expand Down Expand Up @@ -161,15 +159,12 @@ def from_file(cls, spec, *, arguments=None, base_uri=""):
return cls.from_dict(spec, base_uri=base_uri)

@classmethod
def from_url(cls, spec, *, arguments=None, base_uri=""):
def from_url(cls, spec, *, base_uri=""):
"""
Takes in a path to a YAML file, and returns a Specification
"""
spec_content = requests.get(spec).content
with tempfile.NamedTemporaryFile() as tfile:
tfile.write(spec_content)
tfile.flush()
return cls.from_file(tfile.name, arguments=arguments, base_uri=base_uri)
spec = URLHandler()(spec)
return cls.from_dict(spec, base_uri=base_uri)

@staticmethod
def _get_spec_version(spec):
Expand Down Expand Up @@ -216,7 +211,7 @@ def load(cls, spec, *, arguments=None):
if isinstance(spec, str) and (
spec.startswith("http://") or spec.startswith("https://")
):
return cls.from_url(spec, arguments=arguments)
return cls.from_url(spec)
if not isinstance(spec, dict):
base_uri = f"{pathlib.Path(spec).parent}{os.sep}"
return cls.from_file(spec, arguments=arguments, base_uri=base_uri)
Expand Down
5 changes: 4 additions & 1 deletion docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ The basic usage of this command is:
Where:

- SPEC_FILE: Your OpenAPI specification file in YAML format.
- SPEC_FILE: Your OpenAPI specification file in YAML format. Can also be given
as a URL, which will be automatically downloaded.
- BASE_MODULE_PATH (optional): filesystem path where the API endpoints
handlers are going to be imported from. In short, where your Python
code is saved.
Expand All @@ -52,3 +53,5 @@ Your API specification file is not required to have any ``operationId``.
.. code-block:: bash
$ connexion run your_api.yaml --mock=all
$ connexion run https://raw.githubusercontent.com/spec-first/connexion/main/examples/helloworld_async/spec/openapi.yaml --mock=all

0 comments on commit c19743e

Please sign in to comment.