-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_sett.py
42 lines (30 loc) · 1.29 KB
/
db_sett.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
import json
import requests
class Database():
def __init__(self, base_url):
self.base_url = base_url
def close(self):
pass
def initialise(self):
raise NotImplementedError()
def lookup(self, tag):
r = requests.get(self.base_url + "/api/v1/member/" + tag.hex())
if r.status_code != 200:
raise RuntimeError(f"request failed: {r.status_code}")
result = r.json()
return result["name"], result["contactData"]
def update(self, tag, name, comment):
payload = { "fobId": tag.hex(), "name": name, "contactData": comment }
r = requests.put(self.base_url + "/api/v1/member/" + tag.hex(), json=payload)
if r.status_code != 200:
raise RuntimeError(f"request failed: {r.status_code}")
def delete(self, tag):
r = requests.delete(self.base_url + "/api/v1/member/" + tag.hex())
if r.status_code != 204:
raise RuntimeError(f"request failed: {r.status_code}")
def insert(self, tag, name, comment):
payload = { "fobId": tag.hex(), "name": name, "contactData": comment }
r = requests.post(self.base_url + "/api/v1/member", json=payload)
if r.status_code != 201:
raise RuntimeError(f"request failed: {r.status_code}")