Skip to content

Commit 46921b1

Browse files
authored
feat/split the api wrapper code to take in concrete functions (#24)
1 parent e4f94a7 commit 46921b1

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.7
2+
3+
* **Improve code separation to help with unit tests**
4+
15
## 0.0.6
26

37
* **Support streaming response types for /invoke if callable is async generator**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.6" # pragma: no cover
1+
__version__ = "0.0.7" # pragma: no cover

unstructured_platform_plugins/etl_uvicorn/api_generator.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,11 @@ def check_precheck_func(precheck_func: Callable):
6767
raise ValueError(f"no output should exist for precheck function, found: {outputs}")
6868

6969

70-
def generate_fast_api(
71-
app: str,
72-
method_name: Optional[str] = None,
73-
id_str: Optional[str] = None,
74-
id_method: Optional[str] = None,
75-
precheck_str: Optional[str] = None,
76-
precheck_method: Optional[str] = None,
77-
) -> FastAPI:
78-
instance = import_from_string(app)
79-
func = get_func(instance, method_name)
80-
if id_str:
81-
id_ref = import_from_string(id_str)
82-
plugin_id = get_plugin_id(instance=id_ref, method_name=id_method)
83-
else:
84-
plugin_id = hashlib.sha256(
85-
json.dumps(get_schema_dict(func), sort_keys=True).encode()
86-
).hexdigest()[:32]
87-
88-
precheck_func = None
89-
if precheck_str:
90-
precheck_instance = import_from_string(precheck_str)
91-
precheck_func = get_func(precheck_instance, precheck_method)
92-
elif precheck_method:
93-
precheck_func = get_func(instance, precheck_method)
70+
def wrap_in_fastapi(
71+
func: Callable,
72+
plugin_id: str,
73+
precheck_func: Optional[Callable] = None,
74+
):
9475
if precheck_func is not None:
9576
check_precheck_func(precheck_func=precheck_func)
9677

@@ -210,3 +191,31 @@ async def get_id() -> str:
210191
)
211192

212193
return fastapi_app
194+
195+
196+
def generate_fast_api(
197+
app: str,
198+
method_name: Optional[str] = None,
199+
id_str: Optional[str] = None,
200+
id_method: Optional[str] = None,
201+
precheck_str: Optional[str] = None,
202+
precheck_method: Optional[str] = None,
203+
) -> FastAPI:
204+
instance = import_from_string(app)
205+
func = get_func(instance, method_name)
206+
if id_str:
207+
id_ref = import_from_string(id_str)
208+
plugin_id = get_plugin_id(instance=id_ref, method_name=id_method)
209+
else:
210+
plugin_id = hashlib.sha256(
211+
json.dumps(get_schema_dict(func), sort_keys=True).encode()
212+
).hexdigest()[:32]
213+
214+
precheck_func = None
215+
if precheck_str:
216+
precheck_instance = import_from_string(precheck_str)
217+
precheck_func = get_func(precheck_instance, precheck_method)
218+
elif precheck_method:
219+
precheck_func = get_func(instance, precheck_method)
220+
221+
return wrap_in_fastapi(func=func, plugin_id=plugin_id, precheck_func=precheck_func)

0 commit comments

Comments
 (0)