def __request(self, selector, get_params={}, post_data={}, files=[], without_login=False,
                  text_response=True):
        """ General request for :term:`API`.

        :keyword selector: End part of URL which completes self.url parameter
                           set during class initialization.
                           E.g.: ``ticket/123456/show``
        :keyword post_data: Dictionary with POST method fields
        :keyword files: List of pairs (filename, file-like object) describing
                        files to attach as multipart/form-data
                        (list is necessary to keep files ordered)
        :keyword without_login: Turns off checking last login result
                                (usually needed just for login itself)
        :keyword text_response: If set to false the received message will be
                                returned without decoding (useful for attachments)
        :returns: Requested messsage including state line in form
                  ``RT/3.8.7 200 Ok\\n``
        :rtype: string or bytes if text_response is False
        :raises AuthorizationError: In case that request is called without previous
                                    login or login attempt failed.
        :raises ConnectionError: In case of connection error.
        """
        try:
            if (not self.login_result) and (not without_login):
                raise AuthorizationError('First login by calling method `login`.')
            url = str(urljoin(self.url, selector))
            if not files:
                if post_data:
                    response = self.session.post(url, data=post_data)
                else:
                    response = self.session.get(url, params=get_params)
            else:
                files_data = {}
                for i, file_pair in enumerate(files):
                    files_data['attachment_{:d}'.format(i + 1)] = file_pair
                response = self.session.post(url, data=post_data, files=files_data)
            if self.debug_mode or DEBUG_MODE:
                method = "GET"
                if post_data or files:
                    method = "POST"
                print("### {0}".format(datetime.datetime.now().isoformat()))
                print("Request URL: {0}".format(url))
                print("Request method: {0}".format(method))
                print("Respone status code: {0}".format(response.status_code))
                print("Response content:")
                print(response.content.decode())

            if response.status_code == 401:
                raise AuthorizationError('Server could not verify that you are authorized to access the requested document.')
            if response.status_code != 200:
                raise UnexpectedResponse('Received status code {:d} instead of 200.'.format(response.status_code))
            try:
                if response.encoding:
                    result = response.content.decode(response.encoding.lower())
                else:
                    # try utf-8 if encoding is not filled
                    result = response.content.decode('utf-8')
            except LookupError:
                raise UnexpectedResponse('Unknown response encoding: {}.'.format(response.encoding))
            except UnicodeError:
                if text_response:
                    raise UnexpectedResponse('Unknown response encoding (UTF-8 does not work).')
                else:
                    # replace errors - we need decoded content just to check for error codes in __check_response
                    result = response.content.decode('utf-8', 'replace')
            self.__check_response(result)
            if not text_response:
                return response.content
            return result
        except requests.exceptions.ConnectionError as e:
            raise ConnectionError("Connection error", e)
def __request ( self , selector , get_params = { } , post_data = { } , files = [ ] , without_login = False , text_response = True ) : try : if ( not self . login_result ) and ( not without_login ) : raise AuthorizationError ( 'First login by calling method `login`.' ) url = str ( urljoin ( self . url , selector ) ) if not files : if post_data : response = self . session . post ( url , data = post_data ) else : response = self . session . get ( url , params = get_params ) else : files_data = { } for i , file_pair in enumerate ( files ) : files_data [ 'attachment_{:d}' . format ( i + 1 ) ] = file_pair response = self . session . post ( url , data = post_data , files = files_data ) if self . debug_mode or DEBUG_MODE : method = "GET" if post_data or files : method = "POST" print ( "### {0}" . format ( datetime . datetime . now ( ) . isoformat ( ) ) ) print ( "Request URL: {0}" . format ( url ) ) print ( "Request method: {0}" . format ( method ) ) print ( "Respone status code: {0}" . format ( response . status_code ) ) print ( "Response content:" ) print ( response . content . decode ( ) ) if response . status_code == 401 : raise AuthorizationError ( 'Server could not verify that you are authorized to access the requested document.' ) if response . status_code != 200 : raise UnexpectedResponse ( 'Received status code {:d} instead of 200.' . format ( response . status_code ) ) try : if response . encoding : result = response . content . decode ( response . encoding . lower ( ) ) else : result = response . content . decode ( 'utf-8' ) except LookupError : raise UnexpectedResponse ( 'Unknown response encoding: {}.' . format ( response . encoding ) ) except UnicodeError : if text_response : raise UnexpectedResponse ( 'Unknown response encoding (UTF-8 does not work).' ) else : result = response . content . decode ( 'utf-8' , 'replace' ) self . __check_response ( result ) if not text_response : return response . content return result except requests . exceptions . ConnectionError as e : raise ConnectionError ( "Connection error" , e )
General request for :term:`API`.

        :keyword selector: End part of URL which completes self.url parameter
                           set during class initialization.
                           E.g.: ``ticket/123456/show``
        :keyword post_data: Dictionary with POST method fields
        :keyword files: List of pairs (filename, file-like object) describing
                        files to attach as multipart/form-data
                        (list is necessary to keep files ordered)
        :keyword without_login: Turns off checking last login result
                                (usually needed just for login itself)
        :keyword text_response: If set to false the received message will be
                                returned without decoding (useful for attachments)
        :returns: Requested messsage including state line in form
                  ``RT/3.8.7 200 Ok\\n``
        :rtype: string or bytes if text_response is False
        :raises AuthorizationError: In case that request is called without previous
                                    login or login attempt failed.
        :raises ConnectionError: In case of connection error.