From ee3b54ca7b4563f0cf8e0650e06229668869dd1a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 29 May 2023 21:05:16 +0200 Subject: [PATCH] Continued: - added .json to all JSON responses - added response class JSONResponse (I hope it is not overstated this way? - cleaned up imports a bit (only used once can be referenced directly) --- api.py | 77 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/api.py b/api.py index 7b18ead..520cad0 100644 --- a/api.py +++ b/api.py @@ -14,7 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from fastapi import Request, HTTPException, responses, Query +from fastapi import Request, HTTPException, Query +from fastapi.responses import JSONResponse from fastapi.responses import PlainTextResponse from fastapi.templating import Jinja2Templates from datetime import datetime @@ -29,7 +30,7 @@ import fba router = fastapi.FastAPI(docs_url=fba.config["base_url"] + "/docs", redoc_url=fba.config["base_url"] + "/redoc") templates = Jinja2Templates(directory="templates") -@router.get(fba.config["base_url"] + "/api/info") +@router.get(fba.config["base_url"] + "/api/info.json", response_class=JSONResponse) def info(): fba.cursor.execute("SELECT (SELECT COUNT(domain) FROM instances), (SELECT COUNT(domain) FROM instances WHERE software IN ('pleroma', 'mastodon', 'misskey', 'gotosocial', 'friendica', 'bookwyrm', 'takahe', 'peertube')), (SELECT COUNT(blocker) FROM blocks), (SELECT COUNT(domain) FROM instances WHERE last_status_code IS NOT NULL)") known, indexed, blocks, errorous = fba.cursor.fetchone() @@ -42,7 +43,7 @@ def info(): "slogan" : fba.config["slogan"] } -@router.get(fba.config["base_url"] + "/api/top") +@router.get(fba.config["base_url"] + "/api/top.json", response_class=JSONResponse) def top(blocked: int = None, blockers: int = None, reference: int = None, software: int = None): if blocked != None: if blocked > 500: @@ -75,7 +76,7 @@ def top(blocked: int = None, blockers: int = None, reference: int = None, softwa return scoreboard -@router.get(fba.config["base_url"] + "/api") +@router.get(fba.config["base_url"] + "/api/index.json", response_class=JSONResponse) def blocked(domain: str = None, reason: str = None, reverse: str = None): if domain == None and reason == None and reverse == None: raise HTTPException(status_code=400, detail="No filter specified") @@ -113,18 +114,42 @@ def blocked(domain: str = None, reason: str = None, reverse: str = None): return result +@router.get(fba.config["base_url"] + "/api/mutual.json", response_class=JSONResponse) +def mutual(domains: list[str] = Query()): + """Return 200 if federation is open between the two, 4xx otherwise""" + fba.cursor.execute( + "SELECT block_level FROM blocks " \ + "WHERE ((blocker = :a OR blocker = :b) AND (blocked = :b OR blocked = :a OR blocked = :aw OR blocked = :bw)) " \ + "AND block_level = 'reject' " \ + "LIMIT 1", + { + "a" : domains[0], + "b" : domains[1], + "aw": "*." + domains[0], + "bw": "*." + domains[1], + }, + ) + res = fba.cursor.fetchone() + + if res is not None: + # Blocks found + return JSONResponse(status_code=418, content={}) + + # No known blocks + return JSONResponse(status_code=200, content={}) + @router.get(fba.config["base_url"] + "/scoreboard") def index(request: Request, blockers: int = None, blocked: int = None, reference: int = None, software: int = None): scores = None if blockers != None and blockers > 0: - res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top?blockers={blockers}") + res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top.json?blockers={blockers}") elif blocked != None and blocked > 0: - res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top?blocked={blocked}") + res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top.json?blocked={blocked}") elif reference != None and reference > 0: - res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top?reference={reference}") + res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top.json?reference={reference}") elif software != None and software > 0: - res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top?software={software}") + res = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/top.json?software={software}") else: raise HTTPException(status_code=400, detail="No filter specified") @@ -148,24 +173,24 @@ def index(request: Request, blockers: int = None, blocked: int = None, reference @router.get(fba.config["base_url"] + "/") def index(request: Request, domain: str = None, reason: str = None, reverse: str = None): if domain == "" or reason == "" or reverse == "": - return responses.RedirectResponse("/") + return fastapi.responses.RedirectResponse("/") info = None blocks = None if domain == None and reason == None and reverse == None: - info = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/info") + info = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/info.json") if not info.ok: raise HTTPException(status_code=info.status_code, detail=info.text) info = info.json() elif domain != None: - blocks = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api?domain={domain}") + blocks = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/index.json?domain={domain}") elif reason != None: - blocks = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api?reason={reason}") + blocks = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/index.json?reason={reason}") elif reverse != None: - blocks = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api?reverse={reverse}") + blocks = requests.get(f"http://{fba.config['host']}:{fba.config['port']}{fba.config['base_url']}/api/index.json?reverse={reverse}") if blocks != None: if not blocks.ok: @@ -185,30 +210,6 @@ def index(request: Request, domain: str = None, reason: str = None, reverse: str "info" : info }) -@router.get(fba.config["base_url"] + "/api/mutual") -def mutual(domains: list[str] = Query()): - """Return 200 if federation is open between the two, 4xx otherwise""" - fba.cursor.execute( - "SELECT block_level FROM blocks " \ - "WHERE ((blocker = :a OR blocker = :b) AND (blocked = :b OR blocked = :a OR blocked = :aw OR blocked = :bw)) " \ - "AND block_level = 'reject' " \ - "LIMIT 1", - { - "a" : domains[0], - "b" : domains[1], - "aw": "*." + domains[0], - "bw": "*." + domains[1], - }, - ) - res = fba.cursor.fetchone() - - if res is not None: - # Blocks found - return responses.JSONResponse(status_code=418, content={}) - - # No known blocks - return responses.JSONResponse(status_code=200, content={}) - @router.get(fba.config["base_url"] + "/rss") def rss(request: Request, domain: str = None): if domain != None: @@ -253,8 +254,6 @@ def robots(request: Request): return templates.TemplateResponse("robots.txt", { "request" : request, "base_url": fba.config["base_url"] - }, headers={ - "Content-Type": "text/plain" }) if __name__ == "__main__": -- 2.39.5