Commit f7ea73f5 authored by Pim Schellart's avatar Pim Schellart
Browse files

Split backup writer off into separate task to allow for other data stores.

parent 76d2c711
......@@ -19,22 +19,36 @@ class ServerError(Exception):
def __str__(self):
return repr(self.value)
"""BackupWriter is responsible for writing backups of requests to disk.
May be reimplemented to write to some other storage type instead.
"""
class BackupWriter(object):
def __init__(self, outdir):
self.outdir = outdir
def write(self, request):
filename = "{0}-{1}-{2}-{3}.json".format(request["method"],
request["auth"]["username"],
request["auth"]["facility"],
datetime.datetime.utcnow().isoformat())
with open(os.path.join(self.outdir, filename), "w") as f:
json.dump(request, f)
class Client(object):
headers = {'content-type': 'application/json'}
def __init__(self, server, facility, username, password, backup=False, backupDir="./backup"):
def __init__(self, server, facility, username, password, backup=None):
self.server = server
self.facility = facility
self.username = username
self.password = password
self.backup = backup
self.backupDir = backupDir
def post(self, request):
if self.backup:
filename = "{0}-{1}-{2}-{3}.json".format(request["method"], self.facility, self.username, datetime.datetime.utcnow().isoformat())
with open(os.path.join(self.backupDir, filename), "w") as f:
json.dump(request, f)
backup.write(request)
response = requests.post(self.server, data=json.dumps(request), headers=self.headers).json()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment