Skip to content

Commit

Permalink
Support partial patching of config object via API
Browse files Browse the repository at this point in the history
  • Loading branch information
lordi committed Jul 29, 2017
1 parent 7f0f0aa commit d61cff2
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions syncrypt/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,37 @@ def get_config(self, request):

cfg = self.app.config.as_dict()

# prepare certain config values for json
# These guards/conversions should be done transparently in
# Config class
cfg['gui']['is_first_launch'] = cfg['gui']['is_first_launch'] in ('1',)

return JSONResponse(cfg)

@asyncio.coroutine
@require_auth_token
def patch_config(self, request):

content = yield from request.content.read()
params = json.loads(content.decode())

with self.app.config.update_context():
for obj_key, obj in params.items():
for key, value in obj.items():
setting = '{0}.{1}'.format(obj_key, key)
logger.debug('Setting %s.%s to %s', obj_key, key, value)

# These guards/conversions should be done transparently in
# Config class
if setting == 'gui.is_first_launch':
value = '1' if value else '0'

self.app.config.set(setting, value)

return (yield from self.get_config(request))

cfg = self.app.config.as_dict()

# prepare certain config values for json
cfg['gui']['is_first_launch'] = cfg['gui']['is_first_launch'] in ('1', 'yes')

Expand Down Expand Up @@ -213,6 +244,7 @@ def start(self):

self.web_app.router.add_route('GET', '/v1/stats/', self.get_stats)
self.web_app.router.add_route('GET', '/v1/config/', self.get_config)
self.web_app.router.add_route('PATCH', '/v1/config/', self.patch_config)
self.web_app.router.add_route('GET', '/v1/pull', self.get_pull)
self.web_app.router.add_route('GET', '/v1/push', self.get_push)

Expand Down

0 comments on commit d61cff2

Please sign in to comment.