Skip to content

Commit 5771cfa

Browse files
committed
Fixed fastapi docs to get request body for execute endpoint.
execute endpoint changed to get request body with no type and pass it ko function
1 parent eb6d107 commit 5771cfa

File tree

3 files changed

+35
-46
lines changed

3 files changed

+35
-46
lines changed

src/python_activator/api.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22
from pathlib import Path
3+
from typing import Any
34
import uvicorn
4-
from fastapi import FastAPI, Header, HTTPException, Request
5-
from fastapi.openapi.docs import get_swagger_ui_html
5+
from fastapi import FastAPI, Header, HTTPException, Request,Body
66
from python_activator.loader import load_packages
77
from python_activator.installer import install_packages
88

@@ -11,30 +11,15 @@
1111
Knowledge_Objects = {}
1212
object_directory = "" # used for location of knowledge objects
1313

14-
15-
@app.get("/docs", include_in_schema=False)
16-
async def custom_swagger_ui_html():
17-
return get_swagger_ui_html(
18-
openapi_url="/openapi.json",
19-
title="API Documentation",
20-
swagger_js_url="/static/swagger-ui-bundle.js",
21-
swagger_css_url="/static/swagger-ui.css",
22-
)
23-
24-
25-
@app.get("/openapi.json", include_in_schema=False)
26-
async def get_openapi():
27-
return app.openapi()
28-
29-
3014
@app.get(
3115
"/",
3216
include_in_schema=False,
3317
)
3418
async def root(request: Request):
3519
return {
36-
"endpoints": request.url.__str__() + "endpoints",
37-
"execute": request.url.__str__() + "endpoints/{id}",
20+
"endpoints" : request.url.__str__() + "endpoints",
21+
"fastapi documentation" : request.url.__str__() + "docs",
22+
"execute" : request.url.__str__() + "endpoints/{id}",
3823
}
3924

4025

@@ -55,10 +40,10 @@ async def endpoint_detail(endpoint_key: str):
5540
# endpoint to expose all packages
5641
@app.post("/endpoints/{endpoint_key:path}")
5742
async def execute_endpoint(
58-
endpoint_key: str, request: Request, content_type: str = Header(...)
43+
endpoint_key: str, body: Any = Body(...), content_type: str = Header(default="application/json")
5944
):
6045
try:
61-
result = await Knowledge_Objects[endpoint_key].execute(request)
46+
result = await Knowledge_Objects[endpoint_key].execute(body)
6247
return result
6348
except KeyError as e:
6449
raise HTTPException(

src/python_activator/loader.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ def __init__(self, name, status, function=None, id="", url="", entry=""):
1717
self.url = url
1818
self.entry = entry
1919

20-
async def execute(self, request):
21-
data = (
22-
await request.json()
23-
) # if content_type == 'application/json': data = await request.json() else: data="test"
20+
async def execute(self, body):
2421
try:
25-
return self.function(data)
22+
return self.function(body)
2623
except TypeError as e:
2724
raise HTTPException(
2825
status_code=422,

tests/test_manifest.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import pathlib
44
import zipfile
55
from urllib.parse import urlparse
6-
76
import requests
87
import urllib
9-
10-
from python_activator.loader import load_packages
8+
from pathlib import Path
9+
from python_activator.loader import load_packages,get_uri
1110

1211

1312
def test_process_local_manifest():
1413
os.environ["MANIFEST_PATH"] = "/home/faridsei/dev/test/manifest/manifest.json"
15-
load_packages("/home/faridsei/dev/test/pyshelf/")
14+
os.environ["COLLECTION_PATH"] = "/home/faridsei/dev/test/pyshelf/"
15+
load_packages()
1616
del os.environ["MANIFEST_PATH"]
1717

1818
# Assert
@@ -23,15 +23,17 @@ def test_process_internet_manifest():
2323
os.environ[
2424
"MANIFEST_PATH"
2525
] = "https://github.com/kgrid-objects/example-collection/releases/download/4.2.1/manifest.json"
26-
load_packages("/home/faridsei/dev/test/pyshelf/")
26+
os.environ["COLLECTION_PATH"] = "/home/faridsei/dev/test/pyshelf/"
27+
load_packages()
2728
del os.environ["MANIFEST_PATH"]
2829

2930
# Assert
3031
assert 1 == 1
3132

3233

3334
def test_process_no_manifest():
34-
load_packages("/home/faridsei/dev/test/pyshelf/")
35+
os.environ["COLLECTION_PATH"] = "/home/faridsei/dev/test/pyshelf/"
36+
load_packages()
3537

3638
# Assert
3739
assert 1 == 1
@@ -72,22 +74,27 @@ def test_using_uri_to_extract_zip():
7274
assert os.path.exists(destenation_path + "/python-multimime-v1.0")
7375

7476
def test_get_uri():
75-
test=get_uri("/home/faridsei/dev/test/manifest/python-multimime-v1.0.zip")
76-
test=get_uri("https://github.com/kgrid-objects/example-collection/releases/download/4.2.1/python-multimime-v1.0.zip")
77-
assert 1!=1
78-
77+
uri=get_uri("/home/faridsei/dev/test/manifest/python-multimime-v1.0.zip")
78+
assert uri #if any
79+
uri=get_uri("https://github.com/kgrid-objects/example-collection/releases/download/4.2.1/python-multimime-v1.0.zip")
80+
assert uri #if any
81+
82+
def is_valid_uri(uri):
83+
try:
84+
result = urlparse(uri)
85+
if result.scheme and result.netloc:
86+
return True # Valid URL
87+
elif not result.scheme and not result.netloc:
88+
# Check if it's a valid local file path
89+
file_path = Path(result.path)
90+
return file_path.is_absolute() and file_path.exists()
91+
else:
92+
return False # Invalid URI
93+
except:
94+
return False # Invalid URI
7995
# def test_formatting_dictionary_of_object_to_json():
8096
# assert 1!=1
8197

8298

83-
def get_uri(path: str):
84-
uri = path
85-
if os.path.isabs(path): # if a local path create the URI
86-
uri = pathlib.Path(path).as_uri() # adds file:// if local path
87-
return uri
8899

89100

90-
def get_base_uri(uri):
91-
parsed_uri = urlparse(uri)
92-
base_uri = f"{parsed_uri.scheme}://{parsed_uri.netloc}"
93-
return base_uri

0 commit comments

Comments
 (0)