@@ -74,7 +74,25 @@ def __init__(self, ip, port, credentials, timeout, log_min_duration=1):
7474 self .timeout = timeout
7575 self ._log_min_duration = log_min_duration
7676
77- def _call (self , method , url , data = None , timeout = None , clean = False ):
77+ def _call (self , method , url , data = None , json = None , timeout = None , clean = False ):
78+ # type: (callable, str, dict, dict, int, bool) -> any
79+ """
80+ Calls the provided function and adds headings
81+ :param method: Method to call
82+ :type method: callable
83+ :param url: Url to call
84+ :type url: str
85+ :param data: Data to provide. This parameter will not set the JSON header so data may be interpreted differently!
86+ :type data: dict
87+ :param json: Data to provide as JSON parameters. This parameter will set the JSOn header to data will be interpreted as a JSON string
88+ :type json: dict
89+ :param timeout: Timeout to wait for a reply of the server
90+ :type timeout: int
91+ :param clean: Should the data be cleaned (metdata entries stripped from the result)
92+ :type clean: bool
93+ :return: The response
94+ :rtype: any
95+ """
7896 if timeout is None :
7997 timeout = self .timeout
8098
@@ -86,8 +104,13 @@ def _call(self, method, url, data=None, timeout=None, clean=False):
86104 'headers' : self ._base_headers ,
87105 'verify' : False ,
88106 'timeout' : timeout }
89- if data is not None :
90- kwargs ['data' ] = data
107+ # Requests library can both take in 'data' or 'json' keyword.
108+ # When data is given: no extra heading is added and the data is serialized as json
109+ # When json is given: the 'Content type: Application/json' header is added.
110+ # The loop is to provide both options
111+ for key , val in [('json' , json ), ('data' , data )]:
112+ if val is not None :
113+ kwargs [key ] = val
91114 response = method (** kwargs )
92115 if response .status_code == 404 :
93116 msg = 'URL not found: {0}' .format (kwargs ['url' ])
@@ -121,7 +144,7 @@ def _refresh(self):
121144 return self ._base_url , self ._base_headers
122145
123146 def extract_data (self , response_data , old_key = None ):
124- # type: (dict) -> any
147+ # type: (dict, Optional[str] ) -> any
125148 """
126149 Extract the data from the API
127150 For backwards compatibility purposes (older asd-managers might not wrap their data)
@@ -158,3 +181,83 @@ def clean(cls, data):
158181 elif isinstance (data_copy [key ], dict ):
159182 data_copy [key ] = cls .clean (data_copy [key ])
160183 return data_copy
184+
185+ def get (self , url , data = None , json = None , timeout = None , clean = False ):
186+ """
187+ Executes a GET call
188+ :param url: Url to call
189+ :type url: str
190+ :param data: Data to provide. This parameter will not set the JSON header so data may be interpreted differently!
191+ :type data: dict
192+ :param json: Data to provide as JSON parameters. This parameter will set the JSOn header to data will be interpreted as a JSON string
193+ :type json: dict
194+ :param timeout: Timeout to wait for a reply of the server
195+ :type timeout: int
196+ :param clean: Should the data be cleaned (metadata entries stripped from the result)
197+ :type clean: bool
198+ """
199+ return self ._call (method = requests .get , url = url , data = data , json = json , clean = clean , timeout = timeout )
200+
201+ def post (self , url , data = None , json = None , timeout = None , clean = False ):
202+ """
203+ Executes a POST call
204+ :param url: Url to call
205+ :type url: str
206+ :param data: Data to provide. This parameter will not set the JSON header so data may be interpreted differently!
207+ :type data: dict
208+ :param json: Data to provide as JSON parameters. This parameter will set the JSOn header to data will be interpreted as a JSON string
209+ :type json: dict
210+ :param timeout: Timeout to wait for a reply of the server
211+ :type timeout: int
212+ :param clean: Should the data be cleaned (metadata entries stripped from the result)
213+ :type clean: bool
214+ """
215+ return self ._call (method = requests .post , url = url , data = data , json = json , clean = clean , timeout = timeout )
216+
217+ def put (self , url , data = None , json = None , timeout = None , clean = False ):
218+ """
219+ Executes a PUT call
220+ :param url: Url to call
221+ :type url: str
222+ :param data: Data to provide. This parameter will not set the JSON header so data may be interpreted differently!
223+ :type data: dict
224+ :param json: Data to provide as JSON parameters. This parameter will set the JSOn header to data will be interpreted as a JSON string
225+ :type json: dict
226+ :param timeout: Timeout to wait for a reply of the server
227+ :type timeout: int
228+ :param clean: Should the data be cleaned (metadata entries stripped from the result)
229+ :type clean: bool
230+ """
231+ return self ._call (method = requests .put , url = url , data = data , json = json , clean = clean , timeout = timeout )
232+
233+ def patch (self , url , data = None , json = None , timeout = None , clean = False ):
234+ """
235+ Executes a PATCH call
236+ :param url: Url to call
237+ :type url: str
238+ :param data: Data to provide. This parameter will not set the JSON header so data may be interpreted differently!
239+ :type data: dict
240+ :param json: Data to provide as JSON parameters. This parameter will set the JSOn header to data will be interpreted as a JSON string
241+ :type json: dict
242+ :param timeout: Timeout to wait for a reply of the server
243+ :type timeout: int
244+ :param clean: Should the data be cleaned (metadata entries stripped from the result)
245+ :type clean: bool
246+ """
247+ return self ._call (method = requests .patch , url = url , data = data , json = json , clean = clean , timeout = timeout )
248+
249+ def delete (self , url , data = None , json = None , timeout = None , clean = False ):
250+ """
251+ Executes a DELETE call
252+ :param url: Url to call
253+ :type url: str
254+ :param data: Data to provide. This parameter will not set the JSON header so data may be interpreted differently!
255+ :type data: dict
256+ :param json: Data to provide as JSON parameters. This parameter will set the JSOn header to data will be interpreted as a JSON string
257+ :type json: dict
258+ :param timeout: Timeout to wait for a reply of the server
259+ :type timeout: int
260+ :param clean: Should the data be cleaned (metadata entries stripped from the result)
261+ :type clean: bool
262+ """
263+ return self ._call (method = requests .delete , url = url , data = data , json = json , clean = clean , timeout = timeout )
0 commit comments