Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/api/server.py
Show All 9 Lines | |||||
from flask import request | from flask import request | ||||
from swh.core.api import RPCServerApp | from swh.core.api import RPCServerApp | ||||
from swh.core.api import encode_data_server as encode_data | from swh.core.api import encode_data_server as encode_data | ||||
from swh.core.api import error_handler | from swh.core.api import error_handler | ||||
from swh.core.config import read as config_read | from swh.core.config import read as config_read | ||||
from swh.core.statsd import statsd | from swh.core.statsd import statsd | ||||
from swh.model import hashutil | |||||
from swh.objstorage.exc import Error, ObjNotFoundError | from swh.objstorage.exc import Error, ObjNotFoundError | ||||
from swh.objstorage.factory import get_objstorage as get_swhobjstorage | from swh.objstorage.factory import get_objstorage as get_swhobjstorage | ||||
from swh.objstorage.interface import ObjStorageInterface | from swh.objstorage.interface import ObjStorageInterface | ||||
from swh.objstorage.objstorage import DEFAULT_LIMIT | from swh.objstorage.objstorage import DEFAULT_LIMIT | ||||
def timed(f): | def timed(f): | ||||
@functools.wraps(f) | @functools.wraps(f) | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | |||||
@app.route("/") | @app.route("/") | ||||
@timed | @timed | ||||
def index(): | def index(): | ||||
return "SWH Objstorage API server" | return "SWH Objstorage API server" | ||||
# Streaming methods | |||||
@app.route("/content/get_stream/<hex_id>") | |||||
def get_stream(hex_id): | |||||
obj_id = hashutil.hash_to_bytes(hex_id) | |||||
def generate(): | |||||
with timed_context("get_stream"): | |||||
yield from get_objstorage().get_stream(obj_id, 2 << 20) | |||||
return app.response_class(generate()) | |||||
@app.route("/content") | @app.route("/content") | ||||
def list_content(): | def list_content(): | ||||
last_obj_id = request.args.get("last_obj_id") | last_obj_id = request.args.get("last_obj_id") | ||||
if last_obj_id: | if last_obj_id: | ||||
last_obj_id = bytes.fromhex(last_obj_id) | last_obj_id = bytes.fromhex(last_obj_id) | ||||
limit = int(request.args.get("limit", DEFAULT_LIMIT)) | limit = int(request.args.get("limit", DEFAULT_LIMIT)) | ||||
def generate(): | def generate(): | ||||
with timed_context("get_stream"): | with timed_context("list_content"): | ||||
yield from get_objstorage().list_content(last_obj_id, limit=limit) | yield from get_objstorage().list_content(last_obj_id, limit=limit) | ||||
return app.response_class(generate()) | return app.response_class(generate()) | ||||
api_cfg = None | api_cfg = None | ||||
▲ Show 20 Lines • Show All 79 Lines • Show Last 20 Lines |