generated from mwc/lab_subrosa
Initial commit
This commit is contained in:
52
client/api.py
Normal file
52
client/api.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import requests
|
||||
|
||||
class APIError(Exception):
|
||||
"A custom error we'll use when something goes wrong with the API"
|
||||
|
||||
class SubRosaAPI:
|
||||
"""An API for the SubRosa server.
|
||||
"""
|
||||
|
||||
def __init__(self, url):
|
||||
self.url = url
|
||||
|
||||
def get_user(self, username):
|
||||
route = "/users"
|
||||
params = {'name': username}
|
||||
return self.get(route, params)
|
||||
|
||||
def create_user(self, username, public_key):
|
||||
route = "/users/new"
|
||||
params = {'name': username, 'public_key': public_key}
|
||||
return self.post(route, params)
|
||||
|
||||
def get_messages(self, username):
|
||||
route = "/messages"
|
||||
params = {'name': username}
|
||||
return self.get(route, params)
|
||||
|
||||
def send_message(self, sender, recipient, ciphertext, time_sent, time_sent_signature):
|
||||
route = "/messages/send"
|
||||
params = {
|
||||
'sender': sender,
|
||||
'recipient': recipient,
|
||||
'ciphertext': ciphertext,
|
||||
'time_sent': time_sent,
|
||||
'time_sent_signature': time_sent_signature,
|
||||
}
|
||||
return self.get(route, params)
|
||||
|
||||
def get(self, route, params):
|
||||
response = requests.get(self.url + route, json=params)
|
||||
if response.ok:
|
||||
return response.json()
|
||||
else:
|
||||
raise APIError(response.json()['error'])
|
||||
|
||||
def post(self, route, params):
|
||||
response = requests.post(self.url + route, json=params)
|
||||
if response.ok:
|
||||
return response.json()
|
||||
else:
|
||||
raise APIError(response.json()['error'])
|
||||
|
||||
Reference in New Issue
Block a user