(self, path, req_params, req_type='get')
return
Handle HTTP/HTTPS requests to the API
        - Implement error handling in a single location
Handle HTTP/HTTPS requests to the API
        - Implement error handling in a single location
Handle HTTP / HTTPS requests to the API - Implement error handling in a single location
def api_request(self, path, req_params, req_type='get'):
        """
        Handle HTTP/HTTPS requests to the API
        - Implement error handling in a single location
        """
        url = urlparse.urljoin(self.host, path)
        req_params['api_key'] = self.api_key
        req_verify = False  # SSL CERT verification

        if req_type == 'get':
            response = requests.get(url, params=req_params, verify=req_verify)
            # Response handling
            if response.status_code == 200:
                # Success
                result = json.loads(response.content)
                return result
            else:
                # Error reporting
                error = json.loads(response.content)
                for item in error.get('error').get('errors'):
                    code = item.get('code')
                    message = item.get('message')
                    self._info('HTTP Response {}: {}'.format(code, message))
                return
        elif req_type == 'post':
            if 'sample' in req_params:
                # Submit attached samples
                data = req_params.pop('sample')
                response = requests.post(url,
                                         params=req_params,
                                         files={'sample': (req_params.get('filename'), data)},
                                         verify=req_verify)
            else:
                response = requests.post(url,
                                         params=req_params,
                                         verify=req_verify)
            # Response handling
            if response.status_code == 200:
                # Success
                result = json.loads(response.content)
                return result
            else:
                # Error reporting
                error = json.loads(response.content)
                for item in error.get('error').get('errors'):
                    code = item.get('code')
                    message = item.get('message')
                    self._info('HTTP Response {}: {}'.format(code, message))
                return
        return
def api_request ( self , path , req_params , req_type = 'get' ) : url = urlparse . urljoin ( self . host , path ) req_params [ 'api_key' ] = self . api_key req_verify = False # SSL CERT verification if req_type == 'get' : response = requests . get ( url , params = req_params , verify = req_verify ) # Response handling if response . status_code == 200 : # Success result = json . loads ( response . content ) return result else : # Error reporting error = json . loads ( response . content ) for item in error . get ( 'error' ) . get ( 'errors' ) : code = item . get ( 'code' ) message = item . get ( 'message' ) self . _info ( 'HTTP Response {}: {}' . format ( code , message ) ) return elif req_type == 'post' : if 'sample' in req_params : # Submit attached samples data = req_params . pop ( 'sample' ) response = requests . post ( url , params = req_params , files = { 'sample' : ( req_params . get ( 'filename' ) , data ) } , verify = req_verify ) else : response = requests . post ( url , params = req_params , verify = req_verify ) # Response handling if response . status_code == 200 : # Success result = json . loads ( response . content ) return result else : # Error reporting error = json . loads ( response . content ) for item in error . get ( 'error' ) . get ( 'errors' ) : code = item . get ( 'code' ) message = item . get ( 'message' ) self . _info ( 'HTTP Response {}: {}' . format ( code , message ) ) return return