2017-10-05 06:30:11 +00:00
|
|
|
import json, requests, time
|
2017-10-01 23:11:39 +00:00
|
|
|
|
2017-10-05 06:30:11 +00:00
|
|
|
from crypt import EncryptionHelper
|
2017-10-01 23:11:39 +00:00
|
|
|
|
|
|
|
class RESTAPI:
|
|
|
|
def __init__(self, base_url):
|
|
|
|
self.base_url = base_url
|
|
|
|
self.headers = {}
|
|
|
|
|
|
|
|
def get(self, route, params=None):
|
|
|
|
url = self.base_url + route
|
|
|
|
return requests.get(url, params, headers=self.headers).json()
|
|
|
|
|
|
|
|
def post(self, route, data=None):
|
|
|
|
url = self.base_url + route
|
2017-10-05 06:30:11 +00:00
|
|
|
return requests.post(url, json=data, headers=self.headers).json()
|
2017-10-01 23:11:39 +00:00
|
|
|
|
|
|
|
def addHeader(self, header):
|
|
|
|
self.headers.update(header)
|
|
|
|
|
|
|
|
class StandardNotesAPI:
|
|
|
|
encryption_helper = EncryptionHelper()
|
|
|
|
sync_token = None
|
|
|
|
|
|
|
|
def getAuthParamsForEmail(self):
|
2017-10-05 02:36:27 +00:00
|
|
|
return self.api.get('/auth/params', dict(email=self.username))
|
2017-10-01 23:11:39 +00:00
|
|
|
|
2017-10-07 04:43:45 +00:00
|
|
|
def genKeys(self, password):
|
2017-10-01 23:11:39 +00:00
|
|
|
pw_info = self.getAuthParamsForEmail()
|
2017-10-07 04:43:45 +00:00
|
|
|
return self.encryption_helper.pure_generatePasswordAndKey(password, pw_info['pw_salt'], pw_info['pw_cost'])
|
|
|
|
|
|
|
|
def signIn(self, keys):
|
|
|
|
self.keys = keys
|
2017-10-05 02:36:27 +00:00
|
|
|
res = self.api.post('/auth/sign_in', dict(email=self.username, password=self.keys['pw']))
|
|
|
|
self.api.addHeader(dict(Authorization='Bearer ' + res['token']))
|
2017-10-01 23:11:39 +00:00
|
|
|
|
2017-10-04 06:34:27 +00:00
|
|
|
def sync(self, dirty_items):
|
2017-10-05 02:36:27 +00:00
|
|
|
items = self.handleDirtyItems(dirty_items)
|
2017-10-05 06:30:11 +00:00
|
|
|
response = self.api.post('/items/sync', dict(sync_token=self.sync_token, items=items))
|
2017-10-05 02:36:27 +00:00
|
|
|
|
|
|
|
self.sync_token = response['sync_token']
|
|
|
|
return self.handleResponseItems(response)
|
2017-10-04 06:34:27 +00:00
|
|
|
|
2017-10-05 02:36:27 +00:00
|
|
|
def handleDirtyItems(self, dirty_items):
|
|
|
|
items = self.encryption_helper.encryptDirtyItems(dirty_items, self.keys)
|
|
|
|
return items
|
2017-10-01 23:11:39 +00:00
|
|
|
|
2017-10-05 02:36:27 +00:00
|
|
|
def handleResponseItems(self, response):
|
|
|
|
response_items = self.encryption_helper.decryptResponseItems(response['retrieved_items'], self.keys)
|
|
|
|
saved_items = self.encryption_helper.decryptResponseItems(response['saved_items'], self.keys)
|
|
|
|
return dict(response_items=response_items, saved_items=saved_items)
|
2017-10-01 23:11:39 +00:00
|
|
|
|
2017-10-07 04:43:45 +00:00
|
|
|
def __init__(self, base_url, username):
|
|
|
|
self.api = RESTAPI(base_url)
|
2017-10-01 23:11:39 +00:00
|
|
|
self.username = username
|