Пример #1
0
    def _parse_main_response(self, response_raw, subrequests, use_dict=True):
        self.log.debug('Parsing main RPC response...')

        if response_raw.status_code == 400:
            raise BadRequestException("400: Bad Request")
        if response_raw.status_code == 403:
            raise NianticIPBannedException(
                "Seems your IP Address is banned or something else went badly wrong..."
            )
        elif response_raw.status_code in (502, 503, 504):
            raise NianticOfflineException('{} Server Error'.format(
                response_raw.status_code))
        elif response_raw.status_code != 200:
            error = 'Unexpected HTTP server response - needs 200 got {}'.format(
                response_raw.status_code)
            self.log.warning(error)
            self.log.debug('HTTP output: \n%s',
                           response_raw.content.decode('utf-8'))
            raise UnexpectedResponseException(error)

        if not response_raw.content:
            self.log.warning('Empty server response!')
            raise MalformedNianticResponseException('Empty server response!')

        response_proto = ResponseEnvelope()
        try:
            response_proto.ParseFromString(response_raw.content)
        except message.DecodeError as e:
            self.log.error('Could not parse response: %s', e)
            raise MalformedNianticResponseException(
                'Could not decode response.')

        self.log.debug('Protobuf structure of rpc response:\n\r%s',
                       response_proto)
        try:
            self.log.debug(
                'Decode raw over protoc (protoc has to be in your PATH):\n\r%s',
                self.decode_raw(response_raw.content).decode('utf-8'))
        except Exception:
            self.log.debug('Error during protoc parsing - ignored.')

        if use_dict:
            response_proto_dict = protobuf_to_dict(response_proto)
            if 'returns' in response_proto_dict:
                del response_proto_dict['returns']
        else:
            response_proto_dict = {'envelope': response_proto}

        if not response_proto_dict:
            raise MalformedNianticResponseException(
                'Could not convert protobuf to dict.')

        response_proto_dict = self._parse_sub_responses(
            response_proto, subrequests, response_proto_dict, use_dict)

        #It can't be done before
        if not use_dict:
            del response_proto_dict['envelope'].returns[:]

        return response_proto_dict
Пример #2
0
    def _make_rpc(self, endpoint, request_proto_plain):
        self.log.debug('Execution of RPC')

        request_proto_serialized = request_proto_plain.SerializeToString()
        try:
            http_response = self._session.post(endpoint, data=request_proto_serialized, timeout=30)
        except requests.exceptions.Timeout:
            raise NianticTimeoutException('RPC request timed out.')
        except requests.exceptions.ConnectionError as e:
            raise NianticOfflineException(e)

        return http_response