def wrapped_func(*args, **kwargs): # params of function self = args[0] method, url = route_str.split(" ") def defaults_dict(): f_args, varargs, keywords, defaults = inspect.getargspec(f) defaults = defaults or [] return dict(zip(f_args[-len(defaults):], defaults)) defs = defaults_dict() route_args = dict(defs.items() + kwargs.items()) def get_destination_url(): try: return url.format(**route_args) except KeyError as e: raise AttributeError("Define {0} as named argument for route.".format(e)) # KeyError in format have a message with key destination_url = self.base_url + get_destination_url() f(*args, **kwargs) # generally this is "pass" bypass_args = dict([(param, route_args[param]) for param in ["data", "cookies", "auth", "files", "content_type", "params"] if param in route_args]) #add json content type for: # - all public api, meaning have basic auth # - private that ends with .json # - unless files are sent if "files" not in bypass_args and (destination_url.endswith('.json') or "auth" in route_args): bypass_args['headers'] = {'Content-Type': 'application/json'} if "content_type" in bypass_args and bypass_args['content_type']=="yaml": del bypass_args["content_type"] bypass_args['headers'] = {'Content-Type': 'application/x-yaml'} start = time.time() response = requests.request(method, destination_url, verify=self.verify_ssl, **bypass_args) end = time.time() elapsed = int((end - start) * 1000.0) ilog(elapsed) if self.verify_codes: if response.status_code is not 200: msg = "Route {0} {1} returned code={2} and error: {3}".format(method, get_destination_url(), response.status_code, response.text) if response.status_code in api_http_code_errors.keys(): raise api_http_code_errors[response.status_code](msg) else: log.debug(response.text) log.debug(response.request.body) raise ApiError(msg) return response
def wrapped_func(*args, **kwargs): # params of function self = args[0] method, url = route_str.split(" ") def defaults_dict(): f_args, varargs, keywords, defaults = inspect.getargspec(f) defaults = defaults or [] return dict(zip(f_args[-len(defaults):], defaults)) defs = defaults_dict() route_args = dict(defs.items() + kwargs.items()) def get_destination_url(): try: return url.format(**route_args) except KeyError as e: raise AttributeError("Define {0} as named argument for route.".format(e)) # KeyError in format have a message with key destination_url = self.base_url + get_destination_url() f(*args, **kwargs) # generally this is "pass" bypass_args = dict([ (param, route_args[param]) for param in ["data", "json", "cookies", "auth", "files", "content_type", "params"] if param in route_args ]) #add json content type for: # - unless files are sent # - private that ends with .json # - all public api with POST/PUT method, meaning have basic auth # - json parameter is present if "files" not in bypass_args and (destination_url.endswith('.json') or "json" in route_args or ("auth" in bypass_args and method in ["POST", "PUT"])): bypass_args['headers'] = {'Content-Type': 'application/json'} if "content_type" in bypass_args and bypass_args['content_type'] == "yaml": del bypass_args["content_type"] bypass_args['headers'] = {'Content-Type': 'application/x-yaml'} start = time.time() try: response = self._session.request(method, destination_url, verify=self.verify_ssl, **bypass_args) except requests.ConnectionError: log.info('ConnectionError caught. Trying again: \n %s:%s ' % (method, destination_url)) import traceback def log_exception(exc_class, exc, tb): log.info('Got exception: %s' % exc) log.info('Class: %s' % exc_class) log.info('Trace: %s' % traceback.format_tb(tb)) log.error('Got exception while executing: %s' % exc) log_exception(*sys.exc_info()) time.sleep(2) response = self._session.request(method, destination_url, verify=self.verify_ssl, **bypass_args) end = time.time() elapsed = int((end - start) * 1000.0) ilog(elapsed) if self.verify_codes: if response.status_code is not 200: msg = "Route {0} {1} returned code={2} and error: {3}".format(method, get_destination_url(), response.status_code, response.text) if response.status_code in api_http_code_errors.keys(): raise api_http_code_errors[response.status_code](msg) else: log.debug(response.text) log.debug(response.request.body) raise ApiError(msg) return response