class http(object): log = log.getChild('http') @staticmethod def target(*args, **kwargs): return HTTPTarget(*args, **kwargs) @staticmethod def request(method, address, session=None, params=None, headers=None, cookies=None, data=None, json=None, allow_redirects=True, timeout=30): """ :param method: str :param address: str :return: response :rtype: HTTPResponse """ http.log.info("Request: %s %s", method, address) msg = "Request: params=%r, headers=%r, cookies=%r, data=%r, json=%r, allow_redirects=%r, timeout=%r" http.log.debug(msg, params, headers, cookies, data, json, allow_redirects, timeout) if headers is None: headers = {} if "User-Agent" not in headers: headers["User-Agent"] = "Apiritif" if session is None: session = requests.Session() request = requests.Request(method, address, params=params, headers=headers, cookies=cookies, json=json, data=data) prepared = session.prepare_request(request) settings = session.merge_environment_settings(prepared.url, {}, False, False, None) try: response = session.send(prepared, allow_redirects=allow_redirects, timeout=timeout, **settings) except requests.exceptions.Timeout: raise TimeoutError("Connection to %s timed out" % address) except requests.exceptions.ConnectionError: raise ConnectionError("Connection to %s failed" % address) except BaseException: raise http.log.info("Response: %s %s", response.status_code, response.reason) http.log.debug("Response headers: %r", response.headers) http.log.debug("Response cookies: %r", {x: response.cookies.get(x) for x in response.cookies}) http.log.debug('Response content: \n%s', response.content) wrapped_response = HTTPResponse(response) recorder.record_http_request(method, address, prepared, wrapped_response, session) return wrapped_response @staticmethod def get(address, **kwargs): return http.request("GET", address, **kwargs) @staticmethod def post(address, **kwargs): return http.request("POST", address, **kwargs) @staticmethod def put(address, **kwargs): return http.request("PUT", address, **kwargs) @staticmethod def delete(address, **kwargs): return http.request("DELETE", address, **kwargs) @staticmethod def patch(address, **kwargs): return http.request("PATCH", address, **kwargs) @staticmethod def head(address, **kwargs): return http.request("HEAD", address, **kwargs) @staticmethod def options(address, **kwargs): return http.request("OPTIONS", address, **kwargs) @staticmethod def connect(address, **kwargs): return http.request("CONNECT", address, **kwargs)
def __init__(self): self.log = log.getChild('recorder') self.log.debug("Creating recorder")