def parse_tx_response(response, skip_decode): """ Handle a response from the Transifex API. """ charset = None data = response.data if not skip_decode: charset = determine_charset(response) if isinstance(data, bytes): data = data.decode(charset) if response.status < 200 or response.status >= 400: if response.status == 401: raise AuthenticationError(data) elif response.status == 403: raise HttpNotAuthorized(data) elif response.status == 404: raise HttpNotFound(data) elif response.status >= 500: msg = "Failed to connect. Server responded with HTTP code {}" raise TXConnectionError(msg.format(response.status), code=response.status) else: raise Exception("Error received from server: {}".format(data)) return data, charset
def make_request(method, host, url, username, password, fields=None, skip_decode=False, get_params={}): # Initialize http and https pool managers num_pools = 1 managers = {} if host.lower().startswith("http://"): scheme = "http" if "http_proxy" in os.environ: proxy_url = urllib3.util.url.parse_url(os.environ["http_proxy"]) managers["http"] = urllib3.ProxyManager( proxy_url=proxy_url.url, proxy_headers=urllib3.util.make_headers( user_agent=user_agent_identifier(), proxy_basic_auth=proxy_url.auth), num_pools=num_pools) else: managers["http"] = urllib3.PoolManager(num_pools=num_pools) elif host.lower().startswith("https://"): scheme = "https" if "https_proxy" in os.environ: proxy_url = urllib3.util.url.parse_url(os.environ["https_proxy"]) managers["https"] = urllib3.ProxyManager( proxy_url=proxy_url.url, proxy_headers=urllib3.util.make_headers( user_agent=user_agent_identifier(), proxy_basic_auth=proxy_url.auth), num_pools=num_pools, cert_reqs=CERT_REQUIRED, ca_certs=certs_file()) else: managers["https"] = urllib3.PoolManager(num_pools=num_pools, cert_reqs=CERT_REQUIRED, ca_certs=certs_file()) else: raise Exception("Unknown scheme") charset = None headers = urllib3.util.make_headers(basic_auth='{0}:{1}'.format( username, password), accept_encoding=True, user_agent=user_agent_identifier(), keep_alive=True) response = None try: manager = managers[scheme] # All arguments must be bytes, not unicode encoded_request = encode_args(manager.request) response = encoded_request(method, urljoin(host, url), headers=dict(headers), fields=fields) data = response.data if not skip_decode: charset = determine_charset(response) if isinstance(data, bytes): data = data.decode(charset) if response.status < 200 or response.status >= 400: if response.status == 401: raise AuthenticationError(data) elif response.status == 403: raise HttpNotAuthorized(data) elif response.status == 404: raise HttpNotFound(data) elif response.status >= 500: msg = "Failed to connect. Server responded with HTTP code {}" raise TXConnectionError(msg.format(response.status), code=response.status) else: raise Exception("Error received from server: {}".format(data)) return data, charset except SSLError: logger.error("Invalid SSL certificate") raise finally: if response is not None: response.close()