def __init__(self, auth_token): if not auth_token: raise NoHashKeyException('Token not provided for hashing server.') self.headers = { 'content-type': 'application/json', 'Accept': 'application/json', 'X-AuthToken': auth_token }
def rotate_hash_key(self): # Set hash key for this request if not self._hash_key_provider: msg = "No hash key configured!" self.log_error(msg) raise NoHashKeyException() old_hash_key = self._hash_key self._hash_key = self._hash_key_provider.next() if self._hash_key != old_hash_key: self.log_debug("Using hash key {}".format(self._hash_key)) self._api.activate_hash_server(self._hash_key)
def _call_request(self, request, action=None, jitter=True): # Wait until a previous user action gets completed if action: now = time.time() # wait for the time required, or at least a half-second if self._last_action > now + .5: time.sleep(self._last_action - now) else: time.sleep(0.5) if jitter: lat, lng = jitter_location(self.latitude, self.longitude) self._api.set_position(lat, lng, self.altitude) success = False response = {} while not success: try: # Set hash key for this request if not self._hash_key_provider: msg = "No hash key configured!" self.log_error(msg) raise NoHashKeyException() old_hash_key = self._hash_key self._hash_key = self._hash_key_provider.next() if self._hash_key != old_hash_key: self.log_debug("Using hash key {}".format(self._hash_key)) self._api.activate_hash_server(self._hash_key) response = request.call(use_dict=False) self._last_request = time.time() success = True except HashingQuotaExceededException as e: if self.cfg['retry_on_hash_quota_exceeded'] or self.cfg[ 'retry_on_hashing_error']: self.log_warning("{}: Retrying in 5s.".format(repr(e))) time.sleep(5) else: raise except (HashingOfflineException, HashingTimeoutException) as e: if self.cfg['retry_on_hashing_error']: self.log_warning("{}: Retrying in 5s.".format(repr(e))) time.sleep(5) else: raise if not 'envelope' in response: self.log_warning('No response envelope. Something is wrong!') raise PgoapiError # status_code 3 means BAD_REQUEST, so probably banned status_code = response['envelope'].status_code if status_code == 3: self.log_warning("Got BAD_REQUEST response.") self._bad_request_ban = True raise BannedAccountException # Clean up del response['envelope'] if not 'responses' in response: self.log_error("Got no responses at all!") return {} # Set the timer when the user action will be completed if action: self._last_action = self._last_request + action # Return only the responses responses = response['responses'] self._parse_responses(responses) if self.needs_pgpool_update(): self.update_pgpool() return responses