@@ -5,22 +5,28 @@ class ApiResponse():
55 """
66 A class that formats correctly the expected
77 response from the web applicaiton.
8+
9+ @author : github.com/flavienbwk | berwic_f
810 """
911
1012 def __init__ (self ) -> None :
1113 self .error = True
1214 self .message = ""
1315 self .details = {}
16+ self .http_code = 400
1417
15- def setAll (self , error : bool , message : str , details : dict ) -> None :
18+ def setAll (self , error : bool , message : str , details : dict , http_code : int = 400 ) -> None :
1619 self .error = error
1720 self .message = message
1821 self .details = details
22+ self .http_code = http_code
1923
2024 def setSuccess (self ) -> None :
25+ self .http_code = 200
2126 self .error = False
2227
2328 def setError (self ) -> None :
29+ self .http_code = 400
2430 self .error = True
2531
2632 def setMessage (self , message : str ) -> None :
@@ -29,11 +35,16 @@ def setMessage(self, message: str) -> None:
2935 def setDetails (self , details : dict ) -> None :
3036 self .details = details
3137
32- def getResponse (self ) -> {"error" : bool , "message" : str , "details" : {}}:
38+ def setHTTPCode (self , http_code : int ) -> None :
39+ self .http_code = http_code
40+
41+ def getResponse (self ) -> {"http_code" : int , "error" : bool , "message" : str , "data" : {}}:
3342 return {
43+ "flask_api" : True ,
44+ "http_code" : self .http_code ,
3445 "error" : self .error ,
3546 "message" : self .message ,
36- "details " : self .details
47+ "data " : self .details
3748 }
3849
3950 @staticmethod
@@ -45,24 +56,37 @@ def formatFlaskResponse(response):
4556 Switching from the Flask "errors" key to
4657 response format with "error" & "message".
4758
48- Adding an empty "details " object if no details
59+ Adding an empty "data " object if no details
4960 are returned, to remain consistent.
5061 """
5162 try :
63+ # Checking if response is from our Flask API.
64+ # Maybe it is from Swagger UI
5265 response_data = json .loads (response .get_data ())
53- response .headers .add ('Content-Type' , 'application/json' )
54- if "errors" in response_data :
55- response_data ["message" ] = ApiResponse .stringifyFlaskErrors (response_data ["errors" ])
56- response_data ["error" ] = True
57- del (response_data ["errors" ])
58- if "details" not in response_data :
59- response_data ["details" ] = {}
60- response .set_data (json .dumps (response_data ))
66+ if "flask_api" not in response_data :
67+ return response , response ._status_code
68+ else :
69+ del response_data ["flask_api" ]
6170 except ValueError :
62- # Response is not JSON, probably HTML
63- # Swagger UI returns HTML for example
64- pass
65- return response
71+ return response , response ._status_code
72+
73+ http_code = 200
74+ response .headers .add ('Content-Type' , 'application/json' )
75+ if "errors" in response_data :
76+ response_data ["message" ] = ApiResponse .stringifyFlaskErrors (response_data ["errors" ])
77+ response_data ["error" ] = True
78+ del (response_data ["errors" ])
79+ if "data" not in response_data :
80+ response_data ["data" ] = {}
81+ if "error" not in response_data :
82+ response_data ["error" ] = True
83+ if "http_code" in response_data :
84+ http_code = response_data ["http_code" ] if response_data ["error" ] is True else 200
85+ del response_data ["http_code" ]
86+ else :
87+ http_code = 400 if response_data ["error" ] is True else 200
88+ response .set_data (json .dumps (response_data ))
89+ return response , http_code
6690
6791 @staticmethod
6892 def stringifyFlaskErrors (obj : object ) -> str :
@@ -75,8 +99,9 @@ def stringifyFlaskErrors(obj: object) -> str:
7599 return final_message
76100
77101 def __repr__ (self ) -> str :
78- return "<ApiResponse(error='{}', message='{}', details={})>" .format (
102+ return "<ApiResponse(error='{}', message='{}', details={}, http_code={} )>" .format (
79103 self .error ,
80104 self .message ,
81- len (self .details )
105+ len (self .details ),
106+ self .http_code
82107 )
0 commit comments