コード例 #1
0
ファイル: api_caller.py プロジェクト: johnjohnsp1/VxAPI
    def get_response_status_code(self):
        if self.api_response is None:
            raise ResponseObjectNotExistError(
                'It\'s not possible to get response code since API was not called.'
            )

        return self.api_response.status_code
コード例 #2
0
ファイル: api_caller.py プロジェクト: WEPjockey/VxAPI
    def get_response_json(self):
        if self.api_response is None:
            raise ResponseObjectNotExistError(
                'It\'s not possible to get response json since API was not called.'
            )
        elif bool(self.api_response_json) is False:
            try:
                if self.api_response.headers[
                        'Content-Type'] == 'application/json':
                    self.api_response_json = self.api_response.json()
                elif self.api_response.headers['Content-Type'].startswith(
                        'text/html'):
                    # let's be more tolerant and accept situation when content type is not valid, but response has proper json
                    self.api_response_json = json.loads(self.api_response.text)
                else:
                    '''
                    Some of endpoints can return mixed content type - like file type(success) and json(controlled errors).
                    Let's return there empty dictionary, as it's already properly handled by other project parts.
                    '''
                    self.api_response_json = {}
            except json.decoder.JSONDecodeError:
                '''
                When response has status code equal 200 and we're expecting json, there should be json always.
                Let's ignore other cases as for errors like 404, 500, we're getting html page instead.
                That case should be handled in some other place.
                '''
                if self.get_response_status_code(
                ) == 200 and self.request_method_name == self.CONST_EXPECTED_DATA_TYPE_JSON:
                    raise JsonParseError(
                        'Failed to parse response: \'{}\''.format(
                            self.api_response.text))
                else:
                    self.api_response_json = {}

        return self.api_response_json
コード例 #3
0
ファイル: api_caller.py プロジェクト: johnjohnsp1/VxAPI
    def get_response_msg_success_nature(self):
        if self.api_response is None:
            raise ResponseObjectNotExistError(
                'It\'s not possible to define api msg nature since API was not called.'
            )

        return self.response_msg_success_nature
コード例 #4
0
    def get_headers(self):
        if self.api_response is None:
            raise ResponseObjectNotExistError(
                'It\'s not possible to get response headers since API was not called.'
            )

        return self.api_response.headers
コード例 #5
0
    def prepare_response_msg(self) -> str:
        if self.api_response is None:
            raise ResponseObjectNotExistError(
                'It\'s not possible to get response message since API was not called.'
            )

        if self.if_request_success() is True:
            if self.api_expected_data_type == self.CONST_EXPECTED_DATA_TYPE_JSON:
                self.api_response_json = self.get_response_json()

            self.api_result_msg = self.api_success_msg
        else:
            if self.api_response.headers['Content-Type'] == 'application/json':
                self.api_response_json = self.api_response.json()
                self.api_result_msg = self.api_expected_error_msg.format(
                    self.api_response.status_code,
                    self.api_response_json['message'])
            else:
                if self.api_response.status_code == 404:
                    self.api_result_msg = self.api_unexpected_error_404_msg.format(
                        self.api_response.status_code)
                else:
                    self.api_result_msg = self.api_unexpected_error_msg.format(
                        self.api_response.status_code)

        return self.api_result_msg
コード例 #6
0
    def prepare_response_msg(self) -> str:
        if self.api_response is None:
            raise ResponseObjectNotExistError('It\'s not possible to get response message since API was not called.')

        if self.api_response.headers['Content-Type'] == 'text/html':
            self.api_result_msg = self.api_unexpected_error_msg.format(self.api_response.status_code)
        else:
            if self.api_response.status_code == 200:
                if self.api_expected_data_type == self.CONST_EXPECTED_DATA_TYPE_JSON:
                    self.api_response_json = self.api_response.json()
                    if 'response_code' in self.api_response_json:  # Unfortunately not all of endpoints return the unified json.
                        if self.api_response_json['response_code'] == 0:
                            self.api_result_msg = self.api_success_msg
                            self.response_msg_success_nature = True
                        else:
                            self.api_result_msg = self.api_expected_error_msg.format(self.api_response.status_code, self.api_response_json['response_code'], self.api_response_json['response']['error'])
                    else:
                        self.api_result_msg = self.api_success_msg
                        self.response_msg_success_nature = True
                else:  # Few endpoints can return files
                    # Endpoint which is returning file, can also return json file. Then we need to find if we get error msg or that json file.
                    if 'response_code' in self.get_response_json():
                        self.api_result_msg = self.api_expected_error_msg.format(self.api_response.status_code, self.api_response_json['response_code'], self.api_response_json['response']['error'])
                    else:
                        self.api_result_msg = self.api_success_msg
                        self.response_msg_success_nature = True
            else:
                if self.api_expected_data_type == self.CONST_EXPECTED_DATA_TYPE_JSON and bool(self.api_response.json()) is True:  # Sometimes response can has status code different than 200 and store json with error msg
                    self.api_response_json = self.api_response.json()
                    self.api_result_msg = self.api_expected_error_msg.format(self.api_response.status_code, self.api_response_json['response_code'], self.api_response_json['response']['error'])
                else:
                    self.api_result_msg = self.api_unexpected_error_msg.format(self.api_response.status_code)

        return self.api_result_msg
コード例 #7
0
    def get_response_json(self):
        if self.api_response is None:
            raise ResponseObjectNotExistError('It\'s not possible to get response json since API was not called.')
        elif bool(self.api_response_json) is False:
            self.api_response_json = self.api_response.json() if self.api_response.headers['Content-Type'] == 'application/json' else {}

        return self.api_response_json
コード例 #8
0
ファイル: api_caller.py プロジェクト: WEPjockey/VxAPI
    def get_api_response(self):
        if self.api_response is None:
            raise ResponseObjectNotExistError(
                'It\'s not possible to get api response before doing request.')

        return self.api_response