Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/tests/flask_api.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 swh.scanner.exceptions import LargePayloadExc | from swh.scanner.exceptions import LargePayloadExc | ||||
from swh.scanner.policy import QUERY_LIMIT | |||||
from .data import unknown_swhids | from .data import unknown_swhids | ||||
def create_app(tmp_requests): | def create_app(tmp_requests): | ||||
app = Flask(__name__) | app = Flask(__name__) | ||||
@app.route("/") | @app.route("/") | ||||
def index(): | def index(): | ||||
return "SWH scanner API" | return "SWH scanner API" | ||||
@app.route("/known/", methods=["POST"]) | @app.route("/known/", methods=["POST"]) | ||||
def known(): | def known(): | ||||
swhids = request.get_json() | swhids = request.get_json() | ||||
with open(tmp_requests, "a") as f: | with open(tmp_requests, "a") as f: | ||||
for swhid in swhids: | for swhid in swhids: | ||||
f.write(swhid + "\n") | f.write(swhid + "\n") | ||||
if len(swhids) > 900: | if len(swhids) > QUERY_LIMIT: | ||||
raise LargePayloadExc( | raise LargePayloadExc( | ||||
"The maximum number of SWHIDs this endpoint can receive is 900" | f"The maximum number of SWHIDs this endpoint can receive is " | ||||
f"{QUERY_LIMIT}" | |||||
) | ) | ||||
res = {swhid: {"known": False} for swhid in swhids} | res = {swhid: {"known": False} for swhid in swhids} | ||||
for swhid in swhids: | for swhid in swhids: | ||||
if swhid not in unknown_swhids: | if swhid not in unknown_swhids: | ||||
res[swhid]["known"] = True | res[swhid]["known"] = True | ||||
return res | return res | ||||
return app | return app |