tahoe-lafs/src/allmydata/web/magic_folder.py

53 lines
1.5 KiB
Python
Raw Normal View History

2017-01-19 22:39:53 +00:00
import json
from allmydata.web.common import TokenOnlyWebApi, get_arg, WebError
2016-05-23 20:48:07 +00:00
class MagicFolderWebApi(TokenOnlyWebApi):
"""
I provide the web-based API for Magic Folder status etc.
"""
def __init__(self, client):
2016-05-23 20:48:07 +00:00
TokenOnlyWebApi.__init__(self, client)
self.client = client
2016-05-23 20:48:07 +00:00
def post_json(self, req):
req.setHeader("content-type", "application/json")
nick = get_arg(req, 'name', 'default')
try:
magic_folder = self.client._magic_folders[nick]
except KeyError:
raise WebError(
"No such magic-folder '{}'".format(nick),
404,
)
data = []
for item in magic_folder.uploader.get_status():
2016-08-11 01:44:26 +00:00
d = dict(
path=item.relpath_u,
status=item.status_history()[-1][0],
kind='upload',
)
for (status, ts) in item.status_history():
d[status + '_at'] = ts
d['percent_done'] = item.progress.progress
2018-01-16 01:10:43 +00:00
d['size'] = item.size
2016-08-11 01:44:26 +00:00
data.append(d)
for item in magic_folder.downloader.get_status():
2016-08-11 01:44:26 +00:00
d = dict(
path=item.relpath_u,
status=item.status_history()[-1][0],
kind='download',
)
for (status, ts) in item.status_history():
d[status + '_at'] = ts
d['percent_done'] = item.progress.progress
d['size'] = item.size
2016-08-11 01:44:26 +00:00
data.append(d)
2017-01-19 22:39:53 +00:00
return json.dumps(data)