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, abort, request | from flask import Flask, abort, request | ||||
from swh.scanner.exceptions import LargePayloadExc | from swh.scanner.exceptions import LargePayloadExc | ||||
from swh.scanner.policy import QUERY_LIMIT | from swh.scanner.policy import QUERY_LIMIT | ||||
from .data import fake_origin, unknown_swhids | from .data import fake_origin, unknown_swhids | ||||
def create_app(tmp_requests): | def create_app(tmp_requests, tmp_accesses): | ||||
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") | ||||
with open(tmp_accesses, "a") as f: | |||||
f.write(f"{len(swhids)}\n") | |||||
if len(swhids) > QUERY_LIMIT: | if len(swhids) > QUERY_LIMIT: | ||||
raise LargePayloadExc( | raise LargePayloadExc( | ||||
f"The maximum number of SWHIDs this endpoint can receive is " | f"The maximum number of SWHIDs this endpoint can receive is " | ||||
f"{QUERY_LIMIT}" | 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: | ||||
Show All 13 Lines |