Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit aedb628

Browse files
Release v1.3.0 (#11)
Release v1.3.0
1 parent 41cee83 commit aedb628

File tree

1 file changed

+74
-16
lines changed

1 file changed

+74
-16
lines changed

app.py

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
class ErrorCodes:
1414
UNRECOGNIZED_USER = "UNRECOGNIZED_USER"
15+
FORBIDDEN_USER = "FORBIDDEN_USER"
1516
PROJECT_NOT_FOUND = "PROJECT_NOT_FOUND"
1617

1718

@@ -24,6 +25,23 @@ def get_user_id_from_request(request):
2425
return user_id
2526

2627

28+
def handle_response(response):
29+
if response.status_code == status.HTTP_200_OK:
30+
return responses.JSONResponse(
31+
status_code=status.HTTP_200_OK, content=response.json()
32+
)
33+
elif response.status_code == status.HTTP_404_NOT_FOUND:
34+
return responses.JSONResponse(
35+
status_code=status.HTTP_404_NOT_FOUND,
36+
content={"error_code": ErrorCodes.PROJECT_NOT_FOUND},
37+
)
38+
elif response.status_code == status.HTTP_403_FORBIDDEN:
39+
return responses.JSONResponse(
40+
status_code=status.HTTP_403_FORBIDDEN,
41+
content={"error_code": ErrorCodes.FORBIDDEN_USER},
42+
)
43+
44+
2745
@app.get("/project/{project_id}/export")
2846
def get_export(request: Request, project_id: str, num_samples: Optional[int] = None):
2947
try:
@@ -35,7 +53,7 @@ def get_export(request: Request, project_id: str, num_samples: Optional[int] = N
3553
)
3654
url = f"{BASE_URI}/project/{project_id}/export"
3755
resp = requests.get(url, params={"user_id": user_id, "num_samples": num_samples})
38-
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
56+
return handle_response(resp)
3957

4058

4159
@app.get("/project/{project_id}/lookup_list/{lookup_list_id}")
@@ -49,11 +67,11 @@ def get_lookup_list(request: Request, project_id: str, lookup_list_id: str):
4967
)
5068
url = f"{BASE_URI}/project/{project_id}/knowledge_base/{lookup_list_id}"
5169
resp = requests.get(url, params={"user_id": user_id})
52-
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
70+
return handle_response(resp)
5371

5472

55-
@app.post("/project/{project_id}/import")
56-
async def post_import(request: Request, project_id: str):
73+
@app.post("/project/{project_id}/import_file")
74+
async def post_import_file(request: Request, project_id: str):
5775
try:
5876
user_id = get_user_id_from_request(request)
5977
except KeyError:
@@ -62,7 +80,7 @@ async def post_import(request: Request, project_id: str):
6280
content={"error_code": ErrorCodes.UNRECOGNIZED_USER},
6381
)
6482
request_body = await request.json()
65-
url = f"{BASE_URI}/project/{project_id}/import"
83+
url = f"{BASE_URI}/project/{project_id}/import_file"
6684
resp = requests.post(
6785
url,
6886
json={
@@ -72,9 +90,57 @@ async def post_import(request: Request, project_id: str):
7290
"file_import_options": request_body.get("file_import_options"),
7391
},
7492
)
93+
return handle_response(resp)
94+
95+
96+
@app.post("/project/{project_id}/import_json")
97+
async def post_import_json(request: Request, project_id: str):
98+
try:
99+
user_id = get_user_id_from_request(request)
100+
except KeyError:
101+
return responses.JSONResponse(
102+
status_code=status.HTTP_401_UNAUTHORIZED,
103+
content={"error_code": ErrorCodes.UNRECOGNIZED_USER},
104+
)
105+
request_body = await request.json()
106+
url = f"{BASE_URI}/project/{project_id}/import_json"
107+
resp = requests.post(
108+
url,
109+
json={
110+
"user_id": user_id,
111+
"records": request_body["records"],
112+
"request_uuid": request_body["request_uuid"],
113+
"is_last": request_body["is_last"],
114+
},
115+
)
75116
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
76117

77118

119+
@app.post("/project/{project_id}/associations")
120+
async def post_associations(request: Request, project_id: str):
121+
try:
122+
user_id = get_user_id_from_request(request)
123+
except KeyError:
124+
return responses.JSONResponse(
125+
status_code=status.HTTP_401_UNAUTHORIZED,
126+
content={"error_code": ErrorCodes.UNRECOGNIZED_USER},
127+
)
128+
request_body = await request.json()
129+
url = f"{BASE_URI}/project/{project_id}/associations"
130+
resp = requests.post(
131+
url,
132+
json={
133+
"user_id": user_id,
134+
"associations": request_body["associations"],
135+
"indices": request_body["indices"],
136+
"name": request_body["name"],
137+
"label_task_name": request_body["label_task_name"],
138+
"source_type": request_body["source_type"],
139+
},
140+
)
141+
return handle_response(resp)
142+
143+
78144
@app.get("/project/{project_id}")
79145
def get_details(request: Request, project_id: str):
80146
try:
@@ -86,15 +152,7 @@ def get_details(request: Request, project_id: str):
86152
)
87153
url = f"{BASE_URI}/project/{project_id}"
88154
resp = requests.get(url, params={"user_id": user_id})
89-
if resp.status_code == 200:
90-
return responses.JSONResponse(
91-
status_code=status.HTTP_200_OK, content=resp.json()
92-
)
93-
else:
94-
return responses.JSONResponse(
95-
status_code=status.HTTP_404_NOT_FOUND,
96-
content={"error_code": ErrorCodes.PROJECT_NOT_FOUND},
97-
)
155+
return handle_response(resp)
98156

99157

100158
@app.get("/project/{project_id}/import/base_config")
@@ -108,7 +166,7 @@ def get_base_config(request: Request, project_id: str):
108166
)
109167
url = f"{CONFIG_URI}/base_config"
110168
resp = requests.get(url)
111-
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
169+
return handle_response(resp)
112170

113171

114172
@app.get("/project/{project_id}/import/task/{task_id}")
@@ -122,4 +180,4 @@ def get_details(request: Request, project_id: str, task_id: str):
122180
)
123181
url = f"{BASE_URI}/project/{project_id}/import/task/{task_id}"
124182
resp = requests.get(url, params={"user_id": user_id})
125-
return responses.JSONResponse(status_code=status.HTTP_200_OK, content=resp.json())
183+
return handle_response(resp)

0 commit comments

Comments
 (0)