Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/backend.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 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 | ||||
from flask import Flask, request | from flask import Flask, request | ||||
from .db import Db | from .db import Db | ||||
from .exceptions import LargePayloadExc | from .exceptions import LargePayloadExc | ||||
LIMIT = 1000 | LIMIT = 1000 | ||||
def create_app(db: Db): | def create_app(db: Db): | ||||
"""Backend for swh-scanner, implementing the /known endpoint of the | """Backend for swh-scanner, implementing the /known endpoint of the | ||||
Software Heritage Web API""" | Software Heritage Web API""" | ||||
app = Flask(__name__) | app = Flask(__name__) | ||||
@app.route("/api/1/known/", methods=["POST"]) | @app.route("/api/1/known/", methods=["POST"]) | ||||
def known(): | def known(): | ||||
swhids = request.get_json() | swhids = request.get_json() | ||||
if len(swhids) > LIMIT: | if len(swhids) > LIMIT: | ||||
raise LargePayloadExc( | raise LargePayloadExc( | ||||
f"The maximum number of SWHIDs this endpoint can receive is {LIMIT}" | f"The maximum number of SWHIDs this endpoint can receive is {LIMIT}" | ||||
) | ) | ||||
cur = db.conn.cursor() | cur = db.conn.cursor() | ||||
res = {swhid: {"known": db.known(swhid, cur)} for swhid in swhids} | res = {swhid: {"known": db.known(swhid, cur)} for swhid in swhids} | ||||
cur.close() | cur.close() | ||||
return res | return res | ||||
return app | return app | ||||
def run(host: str, port: int, db: Db): | def run(host: str, port: int, db: Db): | ||||
"""Serve the local database | """Serve the local database""" | ||||
""" | |||||
app = create_app(db) | app = create_app(db) | ||||
app.run(host, port, debug=True) | app.run(host, port, debug=True) |