fortifyapi/fortify.py
def _request(self, method, url, params=None, files=None, json=None, data=None, headers=None, stream=False):
        """Common handler for all HTTP requests."""
        if not params:
            params = {}

        if not headers:
            headers = {
                'Accept': 'application/json'
            }
            if method == 'GET' or method == 'POST' or method == 'PUT':
                headers.update({'Content-Type': 'application/json'})
        headers.update({'User-Agent': self.user_agent})

        try:

            if self.auth_type == 'basic':
                response = requests.request(method=method, url=self.host + url, params=params, files=files,
                                            headers=headers, json=json, data=data,
                                            timeout=self.timeout, verify=self.verify_ssl,
                                            auth=(self.username, self.password), stream=stream)
            elif self.auth_type == 'token':
                response = requests.request(method=method, url=self.host + url, params=params, files=files,
                                            headers=headers, json=json, data=data, timeout=self.timeout, 
                                            verify=self.verify_ssl, auth=FortifyTokenAuth(self.token), stream=stream)
            else:
                response = requests.request(method=method, url=self.host + url, params=params, files=files,
                                            headers=headers, json=json, data=data,
                                            timeout=self.timeout, verify=self.verify_ssl, stream=stream)

            try:
                response.raise_for_status()

                # two flavors of response are successful, GETs return 200, PUTs return 204 with empty response text
                response_code = response.status_code
                success = True if response_code // 100 == 2 else False
                if response.text:
                    try:
                        data = response.json()
                    except ValueError:  # Sometimes the returned data isn't JSON, so return raw
                        data = response.content

                return FortifyResponse(success=success, response_code=response_code, data=data,
                                       headers=response.headers)
            except ValueError as e:
                return FortifyResponse(success=False, message="JSON response could not be decoded {0}.".format(e))
        except requests.exceptions.SSLError as e:
            return FortifyResponse(message='An SSL error occurred. {0}'.format(e), success=False)
        except requests.exceptions.ConnectionError as e:
            return FortifyResponse(message='A connection error occurred. {0}'.format(e), success=False)
        except requests.exceptions.Timeout:
            return FortifyResponse(message='The request timed out after ' + str(self.timeout) + ' seconds.',
                                   success=False)
        except requests.exceptions.RequestException as e:
            return FortifyResponse(
                message='There was an error while handling the request. {0}'.format(e), success=False)
Common handler for all HTTP requests.
Common handler for all HTTP requests.
Common handler for all HTTP requests .
def _request(self, method, url, params=None, files=None, json=None, data=None, headers=None, stream=False):
        if not params:
            params = {}
        if not headers:
            headers = {
                'Accept': 'application/json'
            }
            if method == 'GET' or method == 'POST' or method == 'PUT':
                headers.update({'Content-Type': 'application/json'})
        headers.update({'User-Agent': self.user_agent})
        try:
            if self.auth_type == 'basic':
                response = requests.request(method=method, url=self.host + url, params=params, files=files,
                                            headers=headers, json=json, data=data,
                                            timeout=self.timeout, verify=self.verify_ssl,
                                            auth=(self.username, self.password), stream=stream)
            elif self.auth_type == 'token':
                response = requests.request(method=method, url=self.host + url, params=params, files=files,
                                            headers=headers, json=json, data=data, timeout=self.timeout, 
                                            verify=self.verify_ssl, auth=FortifyTokenAuth(self.token), stream=stream)
            else:
                response = requests.request(method=method, url=self.host + url, params=params, files=files,
                                            headers=headers, json=json, data=data,
                                            timeout=self.timeout, verify=self.verify_ssl, stream=stream)
            try:
                response.raise_for_status()
                response_code = response.status_code
                success = True if response_code // 100 == 2 else False
                if response.text:
                    try:
                        data = response.json()
                    except ValueError:  
                        data = response.content
                return FortifyResponse(success=success, response_code=response_code, data=data,
                                       headers=response.headers)
            except ValueError as e:
                return FortifyResponse(success=False, message="JSON response could not be decoded {0}.".format(e))
        except requests.exceptions.SSLError as e:
            return FortifyResponse(message='An SSL error occurred. {0}'.format(e), success=False)
        except requests.exceptions.ConnectionError as e:
            return FortifyResponse(message='A connection error occurred. {0}'.format(e), success=False)
        except requests.exceptions.Timeout:
            return FortifyResponse(message='The request timed out after ' + str(self.timeout) + ' seconds.',
                                   success=False)
        except requests.exceptions.RequestException as e:
            return FortifyResponse(
                message='There was an error while handling the request. {0}'.format(e), success=False)
def _request ( self , method , url , params = None , files = None , json = None , data = None , headers = None , stream = False ) : if not params : params = { } if not headers : headers = { 'Accept' : 'application/json' } if method == 'GET' or method == 'POST' or method == 'PUT' : headers . update ( { 'Content-Type' : 'application/json' } ) headers . update ( { 'User-Agent' : self . user_agent } ) try : if self . auth_type == 'basic' : response = requests . request ( method = method , url = self . host + url , params = params , files = files , headers = headers , json = json , data = data , timeout = self . timeout , verify = self . verify_ssl , auth = ( self . username , self . password ) , stream = stream ) elif self . auth_type == 'token' : response = requests . request ( method = method , url = self . host + url , params = params , files = files , headers = headers , json = json , data = data , timeout = self . timeout , verify = self . verify_ssl , auth = FortifyTokenAuth ( self . token ) , stream = stream ) else : response = requests . request ( method = method , url = self . host + url , params = params , files = files , headers = headers , json = json , data = data , timeout = self . timeout , verify = self . verify_ssl , stream = stream ) try : response . raise_for_status ( ) response_code = response . status_code success = True if response_code // 100 == 2 else False if response . text : try : data = response . json ( ) except ValueError : data = response . content return FortifyResponse ( success = success , response_code = response_code , data = data , headers = response . headers ) except ValueError as e : return FortifyResponse ( success = False , message = "JSON response could not be decoded {0}." . format ( e ) ) except requests . exceptions . SSLError as e : return FortifyResponse ( message = 'An SSL error occurred. {0}' . format ( e ) , success = False ) except requests . exceptions . ConnectionError as e : return FortifyResponse ( message = 'A connection error occurred. {0}' . format ( e ) , success = False ) except requests . exceptions . Timeout : return FortifyResponse ( message = 'The request timed out after ' + str ( self . timeout ) + ' seconds.' , success = False ) except requests . exceptions . RequestException as e : return FortifyResponse ( message = 'There was an error while handling the request. {0}' . format ( e ) , success = False )
Common handler for all HTTP requests.
Common handler for all HTTP requests .
"""Common handler for all HTTP requests.""" # two flavors of response are successful, GETs return 200, PUTs return 204 with empty response text # Sometimes the returned data isn't JSON, so return raw
self
method
url
params
files
json
data
headers
stream