def delete(self, endpoint, **kwargs): """This method performs a Delete against Halo's API. It will attempt to authenticate using the credentials (required to instantiate the object) if the session has either: 1) Not been authenticated yet 2) OAuth Token has expired This is a primary method, meaning it reaches out directly to the Halo API, and should only be utilized by secondary methods with a more specific purpose, like gathering events from /v1/events. If you're using this method because the SDK doesn't provide a more specific method, please reach out to [email protected] so we can get an enhancement request in place for you. Args: endpoint (str): Path component of URL """ if self.connection.auth_token is None: self.connection.authenticate_client() prefix = self.connection.build_endpoint_prefix() url = prefix + endpoint headers = self.connection.build_header() if "params" in kwargs: response = requests.delete(url, headers=headers, params=kwargs["params"]) else: response = requests.delete(url, headers=headers) success, exception = utility.parse_status(url, response.status_code, response.text) if success is False: # If we get a 401, it could be an expired key. We retry once. if response.status_code == 401: self.connection.authenticate_client() headers = self.connection.build_header() response = requests.delete(url, headers=headers) success, exception = utility.parse_status( url, response.status_code, response.text) if success is True: return response.json() elif response.status_code >= 500: success, response, exception = Retry().delete(url, headers) if success is True: return response.json() raise exception else: # Sometimes we don't get json back... try: return_value = response.json() except ValueError: return_value = response.text return return_value
def put(self, endpoint, reqbody): """This method performs a PUT against Halo's API. As with the GET method, it will attempt to (re)authenticate the session if the key is expired or has not yet been retrieved. Also like the GET method, it is not intended for direct use (though we won't stop you). If you need something that the SDK doesn't already provide, please reach out to [email protected] and let us get an enhancement request submitted for you. Args: endpoint (str): Path component of URL """ if self.connection.auth_token is None: self.connection.authenticate_client() prefix = self.connection.build_endpoint_prefix() url = prefix + endpoint headers = self.connection.build_header() response = requests.put(url, headers=headers, data=json.dumps(reqbody)) success, exception = utility.parse_status(url, response.status_code, response.text) if success is False: # If we get a 401, it could be an expired key. We retry once. if response.status_code == 401: self.connection.authenticate_client() headers = self.connection.build_header() response = requests.put(url, headers=headers, data=json.dumps(reqbody)) success, exception = utility.parse_status( url, response.status_code, response.text) if success is True: return response.json() if response.status_code >= 500: success, response, exception = Retry().put( url, headers, reqbody) if success is True: return response.json() raise exception else: # Sometimes we don't get json back... try: return_value = response.json() except ValueError: return_value = response.text return return_value
def delete(self, endpoint, **kwargs): """This method performs a Delete against Halo's API. It will attempt to authenticate using the credentials (required to instantiate the object) if the session has either: 1) Not been authenticated yet 2) OAuth Token has expired This is a primary method, meaning it reaches out directly to the Halo API, and should only be utilized by secondary methods with a more specific purpose, like gathering events from /v1/events. If you're using this method because the SDK doesn't provide a more specific method, please reach out to [email protected] so we can get an enhancement request in place for you. Args: endpoint (str): Path component of URL """ if self.connection.auth_token is None: self.connection.authenticate_client() prefix = self.connection.build_endpoint_prefix() url = prefix + endpoint headers = self.connection.build_header() if "params" in kwargs: response = requests.delete(url, headers=headers, params=kwargs["params"]) else: response = requests.delete(url, headers=headers) success, exception = utility.parse_status(url, response.status_code, response.text) if success is False: # If we get a 401, it could be an expired key. We retry once. if response.status_code == 401: self.connection.authenticate_client() headers = self.connection.build_header() response = requests.delete(url, headers=headers) success, exception = utility.parse_status(url, response.status_code, response.text) if success is True: return response.json() raise exception else: # Sometimes we don't get json back... try: return_value = response.json() except ValueError: return_value = response.text return return_value
def put(self, endpoint, reqbody): """This method performs a PUT against Halo's API. As with the GET method, it will attempt to (re)authenticate the session if the key is expired or has not yet been retrieved. Also like the GET method, it is not intended for direct use (though we won't stop you). If you need something that the SDK doesn't already provide, please reach out to [email protected] and let us get an enhancement request submitted for you. Args: endpoint (str): Path component of URL """ if self.connection.auth_token is None: self.connection.authenticate_client() prefix = self.connection.build_endpoint_prefix() url = prefix + endpoint headers = self.connection.build_header() response = requests.put(url, headers=headers, data=json.dumps(reqbody)) success, exception = utility.parse_status(url, response.status_code, response.text) if success is False: # If we get a 401, it could be an expired key. We retry once. if response.status_code == 401: self.connection.authenticate_client() headers = self.connection.build_header() response = requests.put(url, headers=headers, data=json.dumps(reqbody)) success, exception = utility.parse_status(url, response.status_code, response.text) if success is True: return response.json() raise exception else: # Sometimes we don't get json back... try: return_value = response.json() except ValueError: return_value = response.text return return_value
def get(self, endpoint, **kwargs): """This method performs a GET against Halo's API. It will attempt to authenticate using the credentials (required to instantiate the object) if the session has either: 1) Not been authenticated yet 2) OAuth Token has expired This is a primary method, meaning it reaches out directly to the Halo API, and should only be utilized by secondary methods with a more specific purpose, like gathering events from /v1/events. If you're using this method because the SDK doesn't provide a more specific method, please reach out to [email protected] so we can get an enhancement request in place for you. Args: endpoint (str): URL- everything between \ api.cloudpassage.com and any parameters to be passed. \ Example: /v1/events Keyword Args: params (list of dict): This is a list of dictionary objects, \ represented like this: [{"k1": "two,too"}] \ which goes into the URL looking like this: ?k1=two,too. \ If you use a list as the value in a dictionary here, you'll get \ two k/v pairs represented in the URL and the CloudPassage API \ doesn't operate like that. Only the last instance of that \ variable will be considered, and your results may be confusing. \ So don't do it. Dictionaries should be {str:str}. """ if self.connection.auth_token is None: self.connection.authenticate_client() prefix = self.connection.build_endpoint_prefix() url = prefix + endpoint headers = self.connection.build_header() if "params" in kwargs: response = requests.get(url, headers=headers, params=kwargs["params"]) else: response = requests.get(url, headers=headers) success, exception = utility.parse_status(url, response.status_code, response.text) if success is True: return response.json() # If we get a 401, it could be an expired key. We retry once. if response.status_code == 401: self.connection.authenticate_client() headers = self.connection.build_header() if "params" in kwargs: response = requests.get(url, headers=headers, params=kwargs["params"]) else: response = requests.get(url, headers=headers) success, exception = utility.parse_status(url, response.status_code, response.text) if success is True: return response.json() if response.status_code >= 500: if "params" in kwargs: success, response, exception = Retry().get( url, headers, kwargs["params"]) if success is True: return response.json() else: success, response, exception = Retry().get(url, headers) if success is True: return response.json() raise exception
def get(self, endpoint, **kwargs): """This method performs a GET against Halo's API. It will attempt to authenticate using the credentials (required to instantiate the object) if the session has either: 1) Not been authenticated yet 2) OAuth Token has expired This is a primary method, meaning it reaches out directly to the Halo API, and should only be utilized by secondary methods with a more specific purpose, like gathering events from /v1/events. If you're using this method because the SDK doesn't provide a more specific method, please reach out to [email protected] so we can get an enhancement request in place for you. Args: endpoint (str): URL- everything between \ api.cloudpassage.com and any parameters to be passed. \ Example: /v1/events Keyword Args: params (list of dict): This is a list of dictionary objects, \ represented like this: [{"k1": "two,too"}] \ which goes into the URL looking like this: ?k1=two,too. \ If you use a list as the value in a dictionary here, you'll get \ two k/v pairs represented in the URL and the CloudPassage API \ doesn't operate like that. Only the last instance of that \ variable will be considered, and your results may be confusing. \ So don't do it. Dictionaries should be {str:str}. """ if self.connection.auth_token is None: self.connection.authenticate_client() prefix = self.connection.build_endpoint_prefix() url = prefix + endpoint headers = self.connection.build_header() if "params" in kwargs: response = requests.get(url, headers=headers, params=kwargs["params"]) else: response = requests.get(url, headers=headers) success, exception = utility.parse_status(url, response.status_code, response.text) if success is True: return response.json() # If we get a 401, it could be an expired key. We retry once. if response.status_code == 401: self.connection.authenticate_client() headers = self.connection.build_header() if "params" in kwargs: response = requests.get(url, headers=headers, params=kwargs["params"]) else: response = requests.get(url, headers=headers) success, exception = utility.parse_status(url, response.status_code, response.text) if success is True: return response.json() raise exception