start_api.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:parsec-cloud 作者: Scille 项目源码 文件源码
def register_start_api(app, dispatcher, route='/start'):

    async def api_cipherkey_get(request):
        eff = Effect(EPrivKeyGet(request.match_info['hash']))
        try:
            key = await asyncio_perform(dispatcher, eff)
        except PrivKeyNotFound:
            raise web.HTTPNotFound(text='Unknown hash')
        return web.Response(body=key, content_type='application/octet-stream')

    async def api_cipherkey_post(request):
        cipherkey = await request.read()
        hash = request.match_info['hash']
        eff = Effect(EPrivKeyAdd(hash, cipherkey))
        try:
            await asyncio_perform(dispatcher, eff)
        except PrivKeyHashCollision:
            raise web.HTTPConflict(text='This hash already exists...')
        logger.info('New cipherkey `%s` registered' % hash)
        return web.Response()

    async def api_pubkey_post(request):
        pubkey = await request.read()
        # TODO: should provide a token to avoid impersonation
        identity = request.match_info['identity']
        eff = Effect(EPubKeyAdd(identity, pubkey))
        try:
            await asyncio_perform(dispatcher, eff)
        except ParsecError as exc:
            return web.HTTPConflict(text=exc.label)
        logger.info('New identity `%s` registered' % identity)
        return web.Response()

    app.router.add_get(route + '/cipherkey/{hash}', api_cipherkey_get)
    app.router.add_post(route + '/cipherkey/{hash}', api_cipherkey_post)
    app.router.add_post(route + '/pubkey/{identity}', api_pubkey_post)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号