Skip to content

Commit 95b7547

Browse files
committed
change persistence strategy to use KeenApi object, change client to use new persistence strategy initializer
1 parent 0b82b75 commit 95b7547

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

keen/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from keen import persistence_strategies, exceptions
3+
from keen.api import KeenApi
34
from keen.persistence_strategies import BasePersistenceStrategy
45

56
__author__ = 'dkador'
@@ -40,8 +41,9 @@ def __init__(self, project_id, auth_token, persistence_strategy=None):
4041
if not isinstance(persistence_strategy, BasePersistenceStrategy):
4142
raise exceptions.InvalidPersistenceStrategyError()
4243
if not persistence_strategy:
44+
keen_api = KeenApi(project_id, auth_token)
4345
persistence_strategy = persistence_strategies\
44-
.DirectPersistenceStrategy(project_id, auth_token)
46+
.DirectPersistenceStrategy(keen_api)
4547

4648
self.project_id = project_id
4749
self.auth_token = auth_token

keen/persistence_strategies.py

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import requests
2-
from keen import exceptions
3-
41
__author__ = 'dkador'
52

63
class 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

6035
class 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

6444
class 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

Comments
 (0)