Skip to content

Commit 46e6bee

Browse files
connect python api to ioriodb backend
1 parent bedb052 commit 46e6bee

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

examples/sendjson.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def parse_args():
3232
help='Path to the JSON file you want to send')
3333
parser.add_argument('--url', '-U', metavar='URL',
3434
help='URL for Event Fabric API',
35-
default="https://event-fabric.com/ef/api/")
35+
default="https://event-fabric.com/")
3636

3737
return parser.parse_args()
3838

src/eventfabric.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@
1414
class Client(object):
1515
"""API Client"""
1616
def __init__(self, username, password,
17-
root_url="https://event-fabric.com/api/"):
17+
root_url="https://event-fabric.com/"):
1818
self.root_url = root_url if root_url.endswith("/") else root_url + "/"
1919
self.username = username
20+
self.token = None
21+
self.session_header_name = "x-session"
2022
self.password = password
21-
self.cookies = None
2223

2324
def login(self, requester=requests.post):
2425
"""login to the service with the specified credentials, return a tuple
2526
with a boolean specifying if the login was successful and the response
2627
object"""
2728
headers = {'content-type': 'application/json'}
28-
response = requester(self.endpoint("session"),
29+
response = requester(self.endpoint("sessions"),
30+
verify = False,
2931
data=json.dumps(self.credentials), headers=headers)
3032

31-
self.cookies = response.cookies
33+
self.token = response.json().get("token")
3234

3335
status_ok = response.status_code in (200, 201)
3436
return status_ok, response
@@ -50,8 +52,12 @@ def send_event(self, event, requester=requests.post):
5052
the login was successful and the response object"""
5153

5254
headers = {'content-type': 'application/json'}
53-
response = requester(self.endpoint("event"),
54-
data=json.dumps(event.json), cookies=self.cookies,
55+
if self.token:
56+
headers[self.session_header_name] = self.token
57+
url = self.endpoint("streams") + "/" + self.username + "/" + event.channel + "/"
58+
response = requester(url,
59+
verify = False,
60+
data=json.dumps(event.value),
5561
headers=headers)
5662

5763
status_ok = response.status_code in (200, 201)

tests/eventfabric_tests.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
TEST_DIR = os.path.dirname(__file__)
88
BASE_DIR = os.path.join(TEST_DIR, '..', 'src')
99
sys.path.append(BASE_DIR)
10+
USERNAME = "admin"
11+
PASSWORD = "secret"
1012

1113
import eventfabric as ef
1214

@@ -31,25 +33,25 @@ class TestEventFabric(unittest.TestCase):
3133

3234
def test_client_creation(self):
3335
"test that the client is created and parameters are set correctly"
34-
client = ef.Client("username", "password",
35-
"http://localhost:8080/ef/api")
36-
self.assertEqual(client.username, "username")
37-
self.assertEqual(client.password, "password")
38-
self.assertEqual(client.root_url, "http://localhost:8080/ef/api/")
36+
client = ef.Client(USERNAME, PASSWORD,
37+
"http://localhost:8080/")
38+
self.assertEqual(client.username, USERNAME)
39+
self.assertEqual(client.password, PASSWORD)
40+
self.assertEqual(client.root_url, "http://localhost:8080/")
3941
self.assertEqual(client.cookies, None)
40-
self.assertEqual(client.credentials["username"], "username")
41-
self.assertEqual(client.credentials["password"], "password")
42+
self.assertEqual(client.credentials["username"], USERNAME)
43+
self.assertEqual(client.credentials["password"], PASSWORD)
4244

4345
def test_endpoint(self):
4446
"tests that endpoints are created correctly"
45-
client = ef.Client("username", "password",
46-
"http://localhost:8080/ef/api")
47-
self.assertEqual(client.endpoint("session"),
48-
"http://localhost:8080/ef/api/session")
47+
client = ef.Client(USERNAME, PASSWORD,
48+
"http://localhost:8080/")
49+
self.assertEqual(client.endpoint("sessions"),
50+
"http://localhost:8080/sessions")
4951

5052
def test_login(self):
51-
client = ef.Client("username", "password",
52-
"http://localhost:8080/ef/api")
53+
client = ef.Client(USERNAME, PASSWORD,
54+
"http://localhost:8080/")
5355
storage = []
5456
requester = fake_post(storage, FakeResponse(200, "cookies!"))
5557
status, response = client.login(requester)
@@ -58,17 +60,17 @@ def test_login(self):
5860
data_arg = kwargs["data"]
5961
headers = kwargs["headers"]
6062

61-
self.assertTrue(status)
63+
self.assertTrue(status)
6264
self.assertEqual(response.status_code, 200)
6365
self.assertEqual(response.cookies, "cookies!")
6466
self.assertEqual(response.cookies, client.cookies)
6567
self.assertEqual(data_arg, json.dumps(client.credentials))
6668
self.assertEqual(headers["content-type"], "application/json")
67-
self.assertEqual(endpoint, "http://localhost:8080/ef/api/session")
69+
self.assertEqual(endpoint, "http://localhost:8080/sessions")
6870

6971
def test_send_event(self):
70-
client = ef.Client("username", "password",
71-
"http://localhost:8080/ef/api")
72+
client = ef.Client(USERNAME, PASSWORD,
73+
"http://localhost:8080/")
7274
storage = []
7375
requester = fake_post(storage, FakeResponse(201))
7476
data = {"name": "bob", "count": 10}
@@ -84,7 +86,7 @@ def test_send_event(self):
8486
self.assertEqual(response.status_code, 201)
8587
self.assertEqual(data_arg, json.dumps(event.json))
8688
self.assertEqual(headers["content-type"], "application/json")
87-
self.assertEqual(endpoint, "http://localhost:8080/ef/api/event")
89+
self.assertEqual(endpoint, "http://localhost:8080/streams/" + USERNAME + "/" + channel + "/")
8890

8991
if __name__ == '__main__':
9092
unittest.main()

0 commit comments

Comments
 (0)