python - Dealing with variable number of keyword arguments to send requests -
related python 2.7
how 1 go building request through variable number of kwargs when using requests.
i using requests module directly interact rest api requires variable number of keyword arguments in order successful.
rather re-writing same get/post request code, maintain within single api class. handling variable number of arguments seems boil down series of if-else statements isn't particularly readable.
for example:
def request(self): try: if self.data: request = requests.post(url=self.url, headers=self.headers, data=self.data, timeout=self.timeout, verify=false) else: request = requests.get(url=self.url, headers=self.headers, timeout=self.timeout, verify=false) ... ... preferably request properties build on time , them passed through single or post request (granted, above code still require minor).
if make attributes default same values arguments requests.post (basically, none), can safely pass of them keyword arguments:
def request(self): try: request = requests.post(url=self.url, headers=self.headers, data=self.data, timeout=self.timeout, verify=false) ...
Comments
Post a Comment