@@ -23,6 +23,7 @@ class HTTPMethods(object):
2323
2424 GET = 'get'
2525 POST = 'post'
26+ DELETE = 'delete'
2627
2728
2829class KeenAdapter (HTTPAdapter ):
@@ -67,7 +68,7 @@ def __init__(self, project_id, write_key=None, read_key=None,
6768 version is used
6869 :param get_timeout: optional, the timeout on GET requests
6970 :param post_timeout: optional, the timeout on POST requests
70- :param master_key: a Keen IO Master API Key
71+ :param master_key: a Keen IO Master API Key, needed for deletes
7172 """
7273 # super? recreates the object with values passed into KeenApi
7374 super (KeenApi , self ).__init__ ()
@@ -161,9 +162,32 @@ def query(self, analysis_type, params):
161162
162163 return response .json ()["result" ]
163164
165+ def delete_events (self , event_collection , params ):
166+ """
167+ Deletes events via the Keen IO API. A master key must be set first.
168+
169+ :param event_collection: string, the event collection from which event are being deleted
170+
171+ """
172+ if not self .master_key :
173+ raise exceptions .InvalidEnvironmentError (
174+ "The Keen IO API requires a master key to run deletes. "
175+ "Please set a 'master_key' when initializing the KeenApi object."
176+ )
177+
178+ url = "{0}/{1}/projects/{2}/events/{3}" .format (self .base_url ,
179+ self .api_version ,
180+ self .project_id ,
181+ event_collection )
182+ headers = {"Content-Type" : "application/json" , "Authorization" : self .master_key }
183+ response = self .fulfill (HTTPMethods .DELETE , url , params = params , headers = headers , timeout = self .post_timeout )
184+
185+ self .error_handling (response )
186+ return True
187+
164188 def error_handling (self , res ):
165189 """
166- Helper function to do the error handling
190+ Helper function to do the error handling
167191
168192 :params res: the response from a request
169193 """
@@ -172,5 +196,8 @@ def error_handling(self, res):
172196 try :
173197 error = res .json ()
174198 except json .JSONDecodeError :
175- error = {'message' : 'The API did not respond with JSON, but: "{0}"' .format (res .text [:1000 ]), "error_code" : "InvalidResponseFormat" }
199+ error = {
200+ 'message' : 'The API did not respond with JSON, but: "{0}"' .format (res .text [:1000 ]),
201+ "error_code" : "InvalidResponseFormat"
202+ }
176203 raise exceptions .KeenApiError (error )
0 commit comments