Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
added dynamic docs url (#148)
Browse files Browse the repository at this point in the history
Added dynamic docs_url, which is related to pipeline_family. If pipeline_family isn't defined, docs_url will equal /docs.
Added test for testing dynamic doc url.
Updated test API.

Bumped version from 0.6.0 to 0.7.0
  • Loading branch information
kravetsmic authored Mar 23, 2023
1 parent 246a3e0 commit 756c7a1
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.7.0

* Add dynamic docs_url

# 0.6.0

* Add file type validation via `UNSTRUCTURED_ALLOWED_MIMETYPES`
Expand Down
11 changes: 11 additions & 0 deletions test_unstructured_api_tools/api/test_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from starlette.testclient import TestClient
from prepline_test_project.api.app import app

DOCS_ROUTE = "/test-project/docs"

client = TestClient(app)


def test_docs():
response = client.get(DOCS_ROUTE)
assert response.status_code == 200
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
title="Unstructured Pipeline API",
description="""""",
version="1.0.0",
docs_url="/test-project/docs",
)

app.include_router(process_file_1_router)
Expand Down
2 changes: 1 addition & 1 deletion unstructured_api_tools/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.6.0" # pragma: no cover
__version__ = "0.7.0" # pragma: no cover
47 changes: 29 additions & 18 deletions unstructured_api_tools/pipelines/api_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@
import re


def get_config(filename: Optional[str] = None):
if filename is None:
default = os.path.join(os.getcwd(), "preprocessing-pipeline-family.yaml")
filename = os.environ.get("PIPELINE_FAMILY_CONFIG", default)

if not os.path.exists(filename):
raise FileNotFoundError(
f"A pipeline family config was not found at {filename}."
"The config class looks for the config in the following "
"order:\n"
" 1. The filename parameter\n"
" 2. The PIPELINE_FAMILY_CONFIG environment variable\n"
' 3. "${PWD}"/pipeline-family.yaml'
)

with open(filename, "r") as f:
config = yaml.safe_load(f)

return config


@dataclass
class PipelineConfig:
name: str
Expand All @@ -17,24 +38,7 @@ def __init__(self, filename: Optional[str] = None):
"""Parses pipeline family metadata from the pipeline-family.yaml file. If no
filename is passed, reverts to the PIPELINE_FAMILY_CONFIG environment variable.
Otherwise, looks for pipeline-family.yaml in the working directory."""
if filename is None:
default = os.path.join(os.getcwd(), "preprocessing-pipeline-family.yaml")
self.filename = os.environ.get("PIPELINE_FAMILY_CONFIG", default)
else:
self.filename = filename

if not os.path.exists(self.filename):
raise FileNotFoundError(
f"A pipeline family config was not found at {filename}."
"The config class looks for the config in the following "
"order:\n"
" 1. The filename parameter\n"
" 2. The PIPELINE_FAMILY_CONFIG environment variable\n"
' 3. "${PWD}"/pipeline-family.yaml'
)

with open(self.filename, "r") as f:
config = yaml.safe_load(f)
config = get_config(filename)

self.name = config["name"]
self.version = config["version"]
Expand Down Expand Up @@ -91,3 +95,10 @@ def get_pipeline_path(
pipeline_name = filepath[-1].replace("_", "-").replace(".py", "")

return f"/{pipeline_family}/v{semver}/{pipeline_name}"


def get_api_name_from_config(filename: Optional[str] = None):
try:
return get_config(filename).get("name", None)
except FileNotFoundError:
return None
12 changes: 10 additions & 2 deletions unstructured_api_tools/pipelines/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
from nbconvert import ScriptExporter
import nbformat

from unstructured_api_tools.pipelines.api_conventions import get_pipeline_path, PipelineConfig
from unstructured_api_tools.pipelines.api_conventions import (
get_pipeline_path,
PipelineConfig,
get_api_name_from_config,
)
import unstructured_api_tools.pipelines.lint as lint

IMPORT_PATTERN = (
Expand Down Expand Up @@ -204,7 +208,11 @@ def build_root_app_module(
version = ""

content = template.render(
module_names=module_names, title=title, description=description, version=version
module_names=module_names,
title=title,
description=description,
version=version,
version_name=get_api_name_from_config(config_filename),
)
content = lint.format_black(content)
lint.check_flake8(content, opts=flake8_opts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ app = FastAPI(
title="{{ title }}",
description="""{{ description }}""",
version="{{ version or '1.0.0' }}",
docs_url="{{ '/' ~ version_name ~ '/docs' if version_name else '/docs' }}"
)

{% for module in module_names -%}
Expand Down

0 comments on commit 756c7a1

Please sign in to comment.