Esempio n. 1
0
    def request(self, endpoint, subrequests, player_position):

        if not self._auth_provider or self._auth_provider.is_login() is False:
            raise NotLoggedInException()

        request_proto = self._build_main_request(subrequests, player_position)
        response = self._make_rpc(endpoint, request_proto)

        response_dict = self._parse_main_response(response, subrequests)

        self.check_authentication(response_dict)

        # some response validations
        if isinstance(response_dict, dict):
            status_code = response_dict.get('status_code', None)
            if status_code == 102:
                raise AuthTokenExpiredException()
            elif status_code == 52:
                raise ServerSideRequestThrottlingException("Request throttled by server... slow down man")
            elif status_code == 53:
                api_url = response_dict.get('api_url', None)
                if api_url is not None:
                    exception = ServerApiEndpointRedirectException()
                    exception.set_redirected_endpoint(api_url)
                    raise exception
                else:
                    raise UnexpectedResponseException()

        return response_dict
Esempio n. 2
0
    def call(self, max_retry=15):
        request_callers = self._pop_request_callers()
        if not self.can_call():
            return False  # currently this is never ran, exceptions are raised before

        request_timestamp = None
        api_req_method_list = self._req_method_list
        result = None
        try_cnt = 0
        throttling_retry = 0
        unexpected_response_retry = 0
        while True:
            request_timestamp = self.throttle_sleep()
            # self._call internally clear this field, so save it
            self._req_method_list = [
                req_method for req_method in api_req_method_list
            ]
            should_throttle_retry = False
            should_unexpected_response_retry = False
            try:
                result = self._call()
            except ServerSideRequestThrottlingException:
                should_throttle_retry = True
            except UnexpectedResponseException:
                should_unexpected_response_retry = True

            if should_throttle_retry:
                throttling_retry += 1
                if throttling_retry >= max_retry:
                    raise ServerSideRequestThrottlingException(
                        'Server throttled too many times')
                sleep(1)  # huge sleep ?
                continue  # skip response checking

            if should_unexpected_response_retry:
                unexpected_response_retry += 1
                if unexpected_response_retry >= 5:
                    self.logger.warning(
                        'Server is not responding correctly to our requests.  Waiting for 30 seconds to reconnect.'
                    )
                    sleep(30)
                else:
                    sleep(2)
                continue

            if not self.is_response_valid(result, request_callers):
                try_cnt += 1
                if try_cnt > 3:
                    self.logger.warning(
                        'Server seems to be busy or offline - try again - {}/{}'
                        .format(try_cnt, max_retry))
                if try_cnt >= max_retry:
                    raise ServerBusyOrOfflineException()
                sleep(1)
            else:
                break

        self.last_api_request_time = request_timestamp
        return result
Esempio n. 3
0
    def request(self, endpoint, subrequests, player_position):

        if not self._auth_provider or self._auth_provider.is_login() is False:
            raise NotLoggedInException()

        request_proto = self._build_main_request(subrequests, player_position)
        response = self._make_rpc(endpoint, request_proto)

        response_dict = self._parse_main_response(response, subrequests)

        self.check_authentication(response_dict)

        """ 
        some response validations 
        """
        if isinstance(response_dict, dict) and 'status_code' in response_dict:
            sc = response_dict['status_code']
            if sc == 102:
                raise NotLoggedInException()
            elif sc == 52:
                raise ServerSideRequestThrottlingException("Request throttled by server... slow down man")

        return response_dict