Esempio n. 1
0
async def render_ncco(request):
    bucket_id = request.match_info['bucket_id']

    buckets: BucketOperations = request.app['buckets']
    bucket = await buckets.lookup(bucket_id)
    if bucket is None:
        raise error.ApiError(status=404,
                             text=f'bucket with id {bucket_id} not found')

    ncco_id = request.match_info['ncco_id']
    ncco = await bucket.lookup(ncco_id)

    if ncco is None:
        raise error.ApiError(status=404,
                             text=f'ncco with id {ncco_id} not found')

    ncco_renderer = request.app['ncco_renderer']
    query_params = request.query

    try:
        result = ncco_renderer.render(ncco, query_params)
    except RenderError:
        raise error.ApiError(
            status=400,
            text=f'missing params while rendering ncco with id {ncco_id}')

    try:
        resp = json.loads(result)
    except json.decoder.JSONDecodeError:
        raise error.ApiError(status=400,
                             text=f'rendered ncco is not valid json')

    return web.Response(status=200,
                        text=json.dumps(resp),
                        content_type='application/json')
Esempio n. 2
0
async def remove_ncco(request):
    bucket_id = request.match_info['bucket_id']

    buckets: BucketOperations = request.app['buckets']
    bucket = await buckets.lookup(bucket_id)
    if bucket is None:
        raise error.ApiError(status=404,
                             text=f'bucket with id {bucket_id} not found')

    ncco_id = request.match_info['ncco_id']
    if await bucket.remove(ncco_id) is None:
        raise error.ApiError(status=404,
                             text=f'ncco with id {ncco_id} not found')

    return web.Response(status=204)
Esempio n. 3
0
async def remove_bucket(request):
    bucket_id = request.match_info['bucket_id']

    buckets: BucketOperations = request.app['buckets']
    bucket_name = await buckets.remove(bucket_id)
    if bucket_name is None:
        raise error.ApiError(status=404,
                             text=f'bucket with id {bucket_id} not found')

    return web.Response(status=204)
Esempio n. 4
0
async def create_bucket(request):
    body = await request.json()
    bucket_name = body.get('id')
    ttl = _validate_ttl(body.get('ttl'))

    if bucket_name is None:
        raise error.ApiError(status=400, text="missing 'id' in request body")

    buckets: BucketOperations = request.app['buckets']
    try:
        await buckets.create(bucket_name, ttl=ttl)
    except DuplicateBucketError:
        raise error.ApiError(
            status=409, text=f'bucket with id {bucket_name} already exists')

    res_body = {'id': bucket_name, 'ttl': ttl}
    return web.Response(status=201,
                        text=json.dumps(res_body),
                        content_type='application/json')
Esempio n. 5
0
async def add_ncco_to_bucket(request):
    bucket_id = request.match_info['bucket_id']

    buckets: BucketOperations = request.app['buckets']
    bucket = await buckets.lookup(bucket_id)
    if bucket is None:
        raise error.ApiError(status=404,
                             text=f'bucket with id {bucket_id} not found')

    body = await request.json()

    ncco = body.get('ncco')
    if ncco is None:
        raise error.ApiError(status=400, text="missing 'ncco' in request body")

    ncco_str = str(ncco)
    ncco_id = await bucket.add(ncco_str)

    res_body = {'ncco_id': ncco_id, 'ncco': ncco_str}

    return web.Response(status=201,
                        text=json.dumps(res_body),
                        content_type='application/json')
Esempio n. 6
0
async def lookup_ncco(request):
    bucket_id = request.match_info['bucket_id']

    buckets: BucketOperations = request.app['buckets']
    bucket = await buckets.lookup(bucket_id)
    if bucket is None:
        raise error.ApiError(status=404,
                             text=f'bucket with id {bucket_id} not found')

    ncco_id = request.match_info['ncco_id']
    ncco = await bucket.lookup(ncco_id)

    if ncco is None:
        raise error.ApiError(status=404,
                             text=f'ncco with id {ncco_id} not found')

    res_body = {
        'ncco_id': ncco_id,
        'ncco': ncco,
    }

    return web.Response(text=json.dumps(res_body),
                        content_type='application/json')