Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/api/server.py
# Copyright (C) 2016 The Software Heritage developers | # Copyright (C) 2016 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import asyncio | |||||
import aiohttp.web | import aiohttp.web | ||||
import asyncio | |||||
import click | import click | ||||
import collections | |||||
from swh.core import config | from swh.core import config | ||||
from swh.core.api_async import (SWHRemoteAPI, | from swh.core.api_async import (SWHRemoteAPI, | ||||
encode_data_server as encode_data, | encode_data_server as encode_data, | ||||
decode_request) | decode_request) | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.vault.cookers import COOKER_TYPES | from swh.vault.cookers import COOKER_TYPES | ||||
from swh.vault.backend import VaultBackend, NotFoundExc | from swh.vault.backend import VaultBackend, NotFoundExc | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | |||||
@asyncio.coroutine | @asyncio.coroutine | ||||
def send_notif(request): | def send_notif(request): | ||||
obj_type = request.match_info['type'] | obj_type = request.match_info['type'] | ||||
obj_id = request.match_info['id'] | obj_id = request.match_info['id'] | ||||
request.app['backend'].send_all_notifications(obj_type, obj_id) | request.app['backend'].send_all_notifications(obj_type, obj_id) | ||||
return encode_data(True) # FIXME: success value? | return encode_data(True) # FIXME: success value? | ||||
# Batch endpoints | |||||
@asyncio.coroutine | |||||
def batch_cook(request): | |||||
batch = yield from decode_request(request) | |||||
for obj_type, obj_id in batch: | |||||
if obj_type not in COOKER_TYPES: | |||||
raise aiohttp.web.HTTPNotFound | |||||
batch_id = request.app['backend'].batch_cook(batch) | |||||
return encode_data({'id': batch_id}) | |||||
@asyncio.coroutine | |||||
def batch_progress(request): | |||||
batch_id = request.match_info['batch_id'] | |||||
bundles = request.app['backend'].batch_info(batch_id) | |||||
if not bundles: | |||||
raise aiohttp.web.HTTPNotFound | |||||
bundles = [user_info(bundle) for bundle in bundles] | |||||
counter = collections.Counter(b['status'] for b in bundles) | |||||
res = {'bundles': bundles, 'total': len(bundles), | |||||
**{k: 0 for k in ('new', 'pending', 'done', 'failed')}, | |||||
**dict(counter)} | |||||
return encode_data(res) | |||||
# Web server | # Web server | ||||
def make_app(config, **kwargs): | def make_app(config, **kwargs): | ||||
if 'client_max_size' in config: | if 'client_max_size' in config: | ||||
kwargs['client_max_size'] = config['client_max_size'] | kwargs['client_max_size'] = config['client_max_size'] | ||||
app = SWHRemoteAPI(**kwargs) | app = SWHRemoteAPI(**kwargs) | ||||
app.router.add_route('GET', '/', index) | app.router.add_route('GET', '/', index) | ||||
# Endpoints used by the web API | # Endpoints used by the web API | ||||
app.router.add_route('GET', '/fetch/{type}/{id}', vault_fetch) | app.router.add_route('GET', '/fetch/{type}/{id}', vault_fetch) | ||||
app.router.add_route('POST', '/cook/{type}/{id}', vault_cook) | app.router.add_route('POST', '/cook/{type}/{id}', vault_cook) | ||||
app.router.add_route('GET', '/progress/{type}/{id}', vault_progress) | app.router.add_route('GET', '/progress/{type}/{id}', vault_progress) | ||||
# Endpoints used by the Cookers | # Endpoints used by the Cookers | ||||
app.router.add_route('POST', '/set_progress/{type}/{id}', set_progress) | app.router.add_route('POST', '/set_progress/{type}/{id}', set_progress) | ||||
app.router.add_route('POST', '/set_status/{type}/{id}', set_status) | app.router.add_route('POST', '/set_status/{type}/{id}', set_status) | ||||
app.router.add_route('POST', '/put_bundle/{type}/{id}', put_bundle) | app.router.add_route('POST', '/put_bundle/{type}/{id}', put_bundle) | ||||
app.router.add_route('POST', '/send_notif/{type}/{id}', send_notif) | app.router.add_route('POST', '/send_notif/{type}/{id}', send_notif) | ||||
# Endpoints for batch requests | |||||
app.router.add_route('POST', '/batch_cook', batch_cook) | |||||
app.router.add_route('GET', '/batch_progress/{batch_id}', batch_progress) | |||||
app['backend'] = VaultBackend(config) | app['backend'] = VaultBackend(config) | ||||
return app | return app | ||||
def make_app_from_configfile(config_path=DEFAULT_CONFIG_PATH, **kwargs): | def make_app_from_configfile(config_path=DEFAULT_CONFIG_PATH, **kwargs): | ||||
cfg = config.load_named_config(config_path, DEFAULT_CONFIG) | cfg = config.load_named_config(config_path, DEFAULT_CONFIG) | ||||
return make_app(cfg, **kwargs) | return make_app(cfg, **kwargs) | ||||
Show All 15 Lines |