@@ -16,13 +16,17 @@ def __init__(
1616 api_key :str ,
1717 path : str ,
1818 params : Union [Dict [Any , Any ], List [Dict [Any , Any ]]],
19- verb : RequestVerb
19+ verb : RequestVerb ,
20+ headers : Dict [str , str ] = {"Content-Type" : "application/json" },
21+ data : Union [bytes , None ] = None
2022 ):
2123 self .path = path
2224 self .params = params
2325 self .verb = verb
2426 self .api_url = api_url
2527 self .api_key = api_key
28+ self .data = data
29+ self .headers = headers
2630
2731 def perform (self ) -> Union [T , None ]:
2832 """Is the main function that makes the HTTP request
@@ -37,13 +41,6 @@ def perform(self) -> Union[T, None]:
3741 """
3842 resp = self .make_request (url = f"{ self .api_url } { self .path } " )
3943
40-
41- url = f"{ self .api_url } { self .path } "
42-
43- print (url )
44-
45-
46-
4744 # delete calls do not return a body
4845 if resp .text == "" and resp .status_code == 200 :
4946 return None
@@ -66,7 +63,30 @@ def perform(self) -> Union[T, None]:
6663 message = error .get ("message" ),
6764 error_type = error .get ("name" ),
6865 )
66+
6967 return cast (T , resp .json ())
68+
69+
70+ def perform_file (self ) -> Union [T , None ]:
71+
72+ resp = self .make_request (url = f"{ self .api_url } { self .path } " )
73+
74+ # delete calls do not return a body
75+ if resp .text == "" and resp .status_code == 200 :
76+ return None
77+
78+
79+ # handle error in case there is a statusCode attr present
80+ # and status != 200 and response is a json.
81+ if resp .status_code != 200 and resp .json ().get ("statusCode" ):
82+ error = resp .json ()
83+ raise_for_code_and_type (
84+ code = error .get ("statusCode" ),
85+ message = error .get ("message" ),
86+ error_type = error .get ("name" ),
87+ )
88+
89+ return resp
7090
7191 def perform_with_content (self ) -> T :
7292 """
@@ -82,6 +102,23 @@ def perform_with_content(self) -> T:
82102 if resp is None :
83103 raise NoContentError ()
84104 return resp
105+
106+
107+ def perform_with_content_file (self ) -> T :
108+ """
109+ Perform an HTTP request and return the response content.
110+
111+ Returns:
112+ T: The content of the response
113+
114+ Raises:
115+ NoContentError: If the response content is `None`.
116+ """
117+ resp = self .perform_file ()
118+ if resp is None :
119+ raise NoContentError ()
120+ return resp
121+
85122
86123 def __get_headers (self ) -> Dict [Any , Any ]:
87124 """get_headers returns the HTTP headers that will be
@@ -90,11 +127,17 @@ def __get_headers(self) -> Dict[Any, Any]:
90127 Returns:
91128 Dict: configured HTTP Headers
92129 """
93- return {
130+
131+ h = {
94132 "Content-Type" : "application/json" ,
95133 "Accept" : "application/json" ,
96- "x-api-key" : f"{ self .api_key } "
134+ "x-api-key" : f"{ self .api_key } " ,
97135 }
136+ _headers = h .copy ()
137+ _headers .update (self .headers )
138+
139+ return _headers
140+
98141
99142 def make_request (self , url : str ) -> requests .Response :
100143 """make_request is a helper function that makes the actual
@@ -112,7 +155,9 @@ def make_request(self, url: str) -> requests.Response:
112155 headers = self .__get_headers ()
113156 params = self .params
114157 verb = self .verb
158+ data = self .data
159+
115160 try :
116- return requests .request (verb , url , json = params ,headers = headers ,)
161+ return requests .request (verb , url , json = params ,headers = headers , data = data )
117162 except requests .HTTPError as e :
118163 raise e
0 commit comments