12
12
13
13
class ErrorCodes :
14
14
UNRECOGNIZED_USER = "UNRECOGNIZED_USER"
15
+ FORBIDDEN_USER = "FORBIDDEN_USER"
15
16
PROJECT_NOT_FOUND = "PROJECT_NOT_FOUND"
16
17
17
18
@@ -24,6 +25,23 @@ def get_user_id_from_request(request):
24
25
return user_id
25
26
26
27
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
+
27
45
@app .get ("/project/{project_id}/export" )
28
46
def get_export (request : Request , project_id : str , num_samples : Optional [int ] = None ):
29
47
try :
@@ -35,7 +53,7 @@ def get_export(request: Request, project_id: str, num_samples: Optional[int] = N
35
53
)
36
54
url = f"{ BASE_URI } /project/{ project_id } /export"
37
55
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 )
39
57
40
58
41
59
@app .get ("/project/{project_id}/lookup_list/{lookup_list_id}" )
@@ -49,7 +67,7 @@ def get_lookup_list(request: Request, project_id: str, lookup_list_id: str):
49
67
)
50
68
url = f"{ BASE_URI } /project/{ project_id } /knowledge_base/{ lookup_list_id } "
51
69
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 )
53
71
54
72
55
73
@app .post ("/project/{project_id}/import_file" )
@@ -72,7 +90,7 @@ async def post_import_file(request: Request, project_id: str):
72
90
"file_import_options" : request_body .get ("file_import_options" ),
73
91
},
74
92
)
75
- return responses . JSONResponse ( status_code = status . HTTP_200_OK , content = resp . json () )
93
+ return handle_response ( resp )
76
94
77
95
78
96
@app .post ("/project/{project_id}/import_json" )
@@ -118,7 +136,7 @@ async def post_associations(request: Request, project_id: str):
118
136
"source_type" : request_body ["source_type" ],
119
137
},
120
138
)
121
- return responses . JSONResponse ( status_code = status . HTTP_200_OK , content = resp . json () )
139
+ return handle_response ( resp )
122
140
123
141
124
142
@app .get ("/project/{project_id}" )
@@ -132,15 +150,7 @@ def get_details(request: Request, project_id: str):
132
150
)
133
151
url = f"{ BASE_URI } /project/{ project_id } "
134
152
resp = requests .get (url , params = {"user_id" : user_id })
135
- if resp .status_code == 200 :
136
- return responses .JSONResponse (
137
- status_code = status .HTTP_200_OK , content = resp .json ()
138
- )
139
- else :
140
- return responses .JSONResponse (
141
- status_code = status .HTTP_404_NOT_FOUND ,
142
- content = {"error_code" : ErrorCodes .PROJECT_NOT_FOUND },
143
- )
153
+ return handle_response (resp )
144
154
145
155
146
156
@app .get ("/project/{project_id}/import/base_config" )
@@ -154,7 +164,7 @@ def get_base_config(request: Request, project_id: str):
154
164
)
155
165
url = f"{ CONFIG_URI } /base_config"
156
166
resp = requests .get (url )
157
- return responses . JSONResponse ( status_code = status . HTTP_200_OK , content = resp . json () )
167
+ return handle_response ( resp )
158
168
159
169
160
170
@app .get ("/project/{project_id}/import/task/{task_id}" )
@@ -168,4 +178,4 @@ def get_details(request: Request, project_id: str, task_id: str):
168
178
)
169
179
url = f"{ BASE_URI } /project/{ project_id } /import/task/{ task_id } "
170
180
resp = requests .get (url , params = {"user_id" : user_id })
171
- return responses . JSONResponse ( status_code = status . HTTP_200_OK , content = resp . json () )
181
+ return handle_response ( resp )
0 commit comments