Skip to content

Commit 0b82b75

Browse files
committed
add api module to contain logic for actually making http requests to keen api
1 parent 9998381 commit 0b82b75

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

keen/api.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import requests
2+
from keen import exceptions
3+
4+
__author__ = 'dkador'
5+
6+
class KeenApi(object):
7+
base_url = "https://api.keen.io"
8+
api_version = "2.0"
9+
10+
def __init__(self, project_id, auth_token, base_url=None,
11+
api_version=None):
12+
super(KeenApi, self).__init__()
13+
self.project_id = project_id
14+
self.auth_token = auth_token
15+
if base_url:
16+
self.base_url = base_url
17+
if api_version:
18+
self.api_version = api_version
19+
20+
def post_event(self, event):
21+
url = "{}/{}/projects/{}/{}".format(self.base_url, self.api_version,
22+
self.project_id,
23+
event.collection_name)
24+
headers = {"Authorization": self.auth_token,
25+
"Content-Type": "application/json"}
26+
payload = event.to_json()
27+
response = requests.post(url, data=payload, headers=headers)
28+
if response.status_code != 201:
29+
error = response.json
30+
raise exceptions.KeenApiError(error)

0 commit comments

Comments
 (0)