1- import requests
2- from keen import exceptions
3-
41__author__ = 'dkador'
52
63class BasePersistenceStrategy (object ):
@@ -23,43 +20,32 @@ class DirectPersistenceStrategy(BasePersistenceStrategy):
2320 cache.
2421 """
2522
26- base_url = "https://api.keen.io"
27- api_version = "2.0"
28-
29- def __init__ (self , project_id , auth_token , base_url = None ,
30- api_version = None ):
23+ def __init__ (self , api ):
3124 """ Initializer for DirectPersistenceStrategy.
3225
33- :param project_id: the Keen project id
34- :param auth_token: the Keen authorization token
35- :param base_url: the base_url of the Keen API
36- :param api_version: the version of the Keen API to use
37-
26+ :param api: the Keen Api object used to communicate with the Keen API
3827 """
3928 super (DirectPersistenceStrategy , self ).__init__ ()
40- self .project_id = project_id
41- self .auth_token = auth_token
42- if base_url :
43- self .base_url = base_url
44- if api_version :
45- self .api_version = api_version
29+ self .api = api
4630
4731 def persist (self , event ):
48- url = "{}/{}/projects/{}/{}" .format (self .base_url , self .api_version ,
49- self .project_id ,
50- event .collection_name )
51- headers = {"Authorization" : self .auth_token ,
52- "Content-Type" : "application/json" }
53- payload = event .to_json ()
54- response = requests .post (url , data = payload , headers = headers )
55- if response .status_code != 201 :
56- error = response .json
57- raise exceptions .KeenApiError (error )
32+ self .api .post_event (event )
5833
5934
6035class RedisPersistenceStrategy (BasePersistenceStrategy ):
36+ """
37+ A persistence strategy that persists events to Redis for later processing.
38+
39+ Not yet implemented.
40+ """
6141 pass
6242
6343
6444class FilePersistenceStrategy (BasePersistenceStrategy ):
45+ """
46+ A persistence strategy that persists events to the local file system for
47+ later processing.
48+
49+ Not yet implemented.
50+ """
6551 pass
0 commit comments