Skip to content

feat/split the api wrapper code to take in concrete functions #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.7

* **Improve code separation to help with unit tests**

## 0.0.6

* **Support streaming response types for /invoke if callable is async generator**
Expand Down
2 changes: 1 addition & 1 deletion unstructured_platform_plugins/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.6" # pragma: no cover
__version__ = "0.0.7" # pragma: no cover
57 changes: 33 additions & 24 deletions unstructured_platform_plugins/etl_uvicorn/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,11 @@ def check_precheck_func(precheck_func: Callable):
raise ValueError(f"no output should exist for precheck function, found: {outputs}")


def generate_fast_api(
app: str,
method_name: Optional[str] = None,
id_str: Optional[str] = None,
id_method: Optional[str] = None,
precheck_str: Optional[str] = None,
precheck_method: Optional[str] = None,
) -> FastAPI:
instance = import_from_string(app)
func = get_func(instance, method_name)
if id_str:
id_ref = import_from_string(id_str)
plugin_id = get_plugin_id(instance=id_ref, method_name=id_method)
else:
plugin_id = hashlib.sha256(
json.dumps(get_schema_dict(func), sort_keys=True).encode()
).hexdigest()[:32]

precheck_func = None
if precheck_str:
precheck_instance = import_from_string(precheck_str)
precheck_func = get_func(precheck_instance, precheck_method)
elif precheck_method:
precheck_func = get_func(instance, precheck_method)
def wrap_in_fastapi(
func: Callable,
plugin_id: str,
precheck_func: Optional[Callable] = None,
):
if precheck_func is not None:
check_precheck_func(precheck_func=precheck_func)

Expand Down Expand Up @@ -210,3 +191,31 @@ async def get_id() -> str:
)

return fastapi_app


def generate_fast_api(
app: str,
method_name: Optional[str] = None,
id_str: Optional[str] = None,
id_method: Optional[str] = None,
precheck_str: Optional[str] = None,
precheck_method: Optional[str] = None,
) -> FastAPI:
instance = import_from_string(app)
func = get_func(instance, method_name)
if id_str:
id_ref = import_from_string(id_str)
plugin_id = get_plugin_id(instance=id_ref, method_name=id_method)
else:
plugin_id = hashlib.sha256(
json.dumps(get_schema_dict(func), sort_keys=True).encode()
).hexdigest()[:32]

precheck_func = None
if precheck_str:
precheck_instance = import_from_string(precheck_str)
precheck_func = get_func(precheck_instance, precheck_method)
elif precheck_method:
precheck_func = get_func(instance, precheck_method)

return wrap_in_fastapi(func=func, plugin_id=plugin_id, precheck_func=precheck_func)
Loading