async def fetch(self, url, method='GET', headers=None, body=None): """Perform a HTTP request and return decoded JSON data""" headers = headers or {} if self.userAgent: if type(self.userAgent) is str: headers.update({'User-Agent': self.userAgent}) elif (type(self.userAgent) is dict) and ('User-Agent' in self.userAgent): headers.update(self.userAgent) if len(self.proxy): headers.update({'Origin': '*'}) headers.update({'Accept-Encoding': 'gzip, deflate'}) url = self.proxy + url if self.verbose: print(url, method, url, "\nRequest:", headers, body) encoded_body = body.encode() if body else None session_method = getattr(self.aiohttp_session, method.lower()) try: async with session_method(url, data=encoded_body, headers=headers, timeout=(self.timeout / 1000)) as response: text = await response.text() self.handle_rest_errors(None, response.status, text, url, method) except concurrent.futures._base.TimeoutError as e: raise RequestTimeout(' '.join([self.id, method, url, 'request timeout'])) except aiohttp.client_exceptions.ServerDisconnectedError as e: self.raise_error(ExchangeError, url, method, e, None) except aiohttp.client_exceptions.ClientConnectorError as e: self.raise_error(ExchangeError, url, method, e, None) if self.verbose: print(method, url, "\nResponse:", headers, text) return self.handle_rest_response(text, url, method, headers, body)
def fetch(self, url, method='GET', headers=None, body=None): now = self.milliseconds() elapsed = now - self.lastRestRequestTimestamp if self.enableRateLimit and (elapsed < self.rateLimit): delay = self.rateLimit - elapsed time.sleep(delay / 1000.0) self.lastRestRequestTimestamp = self.milliseconds() """Perform a HTTP request and return decoded JSON data""" headers = headers or {} if self.userAgent: if type(self.userAgent) is str: headers.update({'User-Agent': self.userAgent}) elif (type(self.userAgent) is dict) and ('User-Agent' in self.userAgent): headers.update(self.userAgent) if len(self.proxy): headers.update({'Origin': '*'}) headers.update({'Accept-Encoding': 'gzip, deflate'}) url = self.proxy + url if self.verbose: print(url, method, url, "\nRequest:", headers, body) if body: body = body.encode() request = _urllib.Request(url, body, headers) request.get_method = lambda: method response = None text = None try: # send request and load response handler = _urllib.HTTPHandler if url.startswith( 'http://') else _urllib.HTTPSHandler opener = _urllib.build_opener(handler) response = opener.open(request, timeout=int(self.timeout / 1000)) text = response.read() except socket.timeout as e: raise RequestTimeout(' '.join( [self.id, method, url, 'request timeout'])) except ssl.SSLError as e: self.raise_error(ExchangeNotAvailable, url, method, e) except _urllib.HTTPError as e: self.handle_rest_errors(e, e.code, text, url, method) self.raise_error(ExchangeError, url, method, e, text if text else None) except _urllib.URLError as e: self.raise_error(ExchangeNotAvailable, url, method, e) except httplib.BadStatusLine as e: self.raise_error(ExchangeNotAvailable, url, method, e) encoding = response.info().get('Content-Encoding') if encoding in ('gzip', 'x-gzip', 'deflate'): if encoding == 'deflate': text = zlib.decompress(text, -zlib.MAX_WBITS) else: data = gzip.GzipFile('', 'rb', 9, io.BytesIO(text)) text = data.read() decoded_text = text.decode('utf-8') if self.verbose: print(method, url, "\nResponse:", response.info().headers, decoded_text) return self.handle_rest_response(decoded_text, url, method, headers, body)
def fetch(self, url, method='GET', headers=None, body=None): """Perform a HTTP request and return decoded JSON data""" if self.enableRateLimit: self.throttle() self.lastRestRequestTimestamp = self.milliseconds() headers = headers or {} if self.userAgent: if type(self.userAgent) is str: headers.update({'User-Agent': self.userAgent}) elif (type(self.userAgent) is dict) and ('User-Agent' in self.userAgent): headers.update(self.userAgent) if self.proxy: headers.update({'Origin': '*'}) headers.update({'Accept-Encoding': 'gzip, deflate'}) url = self.proxy + url if self.verbose: print(url, method, url, "\nRequest:", headers, body) if body: body = body.encode() request = _urllib.Request(url, body, headers) request.get_method = lambda: method response = None text = None try: # send request and load response handler = _urllib.HTTPHandler if url.startswith( 'http://') else _urllib.HTTPSHandler opener = _urllib.build_opener(handler) response = opener.open(request, timeout=int(self.timeout / 1000)) text = response.read() self.last_http_response = text except socket.timeout as e: raise RequestTimeout(' '.join( [self.id, method, url, 'request timeout'])) except ssl.SSLError as e: self.raise_error(ExchangeNotAvailable, url, method, e) except _urllib.HTTPError as e: self.handle_errors(e.code, e.reason, url, method, None, text) self.handle_rest_errors(e, e.code, text, url, method) self.raise_error(ExchangeError, url, method, e, text if text else None) except _urllib.URLError as e: self.raise_error(ExchangeNotAvailable, url, method, e) except httplib.BadStatusLine as e: self.raise_error(ExchangeNotAvailable, url, method, e) text = self.gzip_deflate(response, text) decoded_text = text.decode('utf-8') self.last_http_response = decoded_text if self.verbose: print(method, url, "\nResponse:", str(response.info()), decoded_text) return self.handle_rest_response(decoded_text, url, method, headers, body)
def fetch(self, url, method='GET', headers=None, body=None): """Perform a HTTP request and return decoded JSON data""" headers = headers or {} if self.userAgent: if type(self.userAgent) is str: headers.update({'User-Agent': self.userAgent}) elif (type(self.userAgent) is dict) and ('User-Agent' in self.userAgent): headers.update(self.userAgent) if len(self.proxy): headers.update({'Origin': '*'}) headers.update({'Accept-Encoding': 'gzip, deflate'}) url = self.proxy + url if self.verbose: print(url, method, url, "\nRequest:", headers, body) if body: body = body.encode() request = _urllib.Request(url, body, headers) request.get_method = lambda: method response = None text = None try: # send request and load response handler = _urllib.HTTPHandler if url.startswith( 'http://') else _urllib.HTTPSHandler opener = _urllib.build_opener(handler) response = opener.open(request, timeout=int(self.timeout / 1000)) text = response.read() except socket.timeout as e: raise RequestTimeout(' '.join( [self.id, method, url, 'request timeout'])) except ssl.SSLError as e: self.raise_error(ExchangeNotAvailable, url, method, e) except _urllib.HTTPError as e: error = None details = text if text else None if e.code == 429: error = DDoSProtection elif e.code in [404, 409, 422, 500, 501, 502, 520, 521, 522, 525]: details = e.read().decode('utf-8', 'ignore') if e else None error = ExchangeNotAvailable elif e.code in [400, 403, 405, 503]: # special case to detect ddos protection reason = e.read().decode('utf-8', 'ignore') ddos_protection = re.search('(cloudflare|incapsula)', reason, flags=re.IGNORECASE) if ddos_protection: error = DDoSProtection else: error = ExchangeNotAvailable details = '(possible reasons: ' + ', '.join([ 'invalid API keys', 'bad or old nonce', 'exchange is down or offline', 'on maintenance', 'DDoS protection', 'rate-limiting', reason, ]) + ')' elif e.code in [408, 504]: error = RequestTimeout elif e.code in [401, 511]: error = AuthenticationError else: error = ExchangeError self.raise_error(error, url, method, e, details) except _urllib.URLError as e: self.raise_error(ExchangeNotAvailable, url, method, e) except httplib.BadStatusLine as e: self.raise_error(ExchangeNotAvailable, url, method, e) encoding = response.info().get('Content-Encoding') if encoding in ('gzip', 'x-gzip', 'deflate'): if encoding == 'deflate': text = zlib.decompress(text, -zlib.MAX_WBITS) else: data = gzip.GzipFile('', 'rb', 9, io.BytesIO(text)) text = data.read() body = text.decode('utf-8') if self.verbose: print(method, url, "\nResponse:", headers, body) return self.handle_response(url, method, headers, body)