From dad75dbbc2fc75a4ecff0372b02ef083fb52a15b Mon Sep 17 00:00:00 2001 From: Krateng Date: Fri, 4 Sep 2020 00:08:24 +0200 Subject: [PATCH] Implemented aliases --- maloja/apis/__init__.py | 43 +++++++++++++++++++++++++---------- maloja/apis/audioscrobbler.py | 5 ++++ maloja/apis/listenbrainz.py | 4 ++++ maloja/apis/native_v1.py | 1 + maloja/server.py | 3 --- 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/maloja/apis/__init__.py b/maloja/apis/__init__.py index 10088ec..fb92290 100644 --- a/maloja/apis/__init__.py +++ b/maloja/apis/__init__.py @@ -3,19 +3,38 @@ from .audioscrobbler import Audioscrobbler from .listenbrainz import Listenbrainz import copy +from bottle import redirect, request +from urllib.parse import urlencode - -apis = { - "mlj_1":native_v1.api, - "listenbrainz/1":Listenbrainz().nimrodelapi, - "audioscrobbler/2.0":Audioscrobbler().nimrodelapi -} - -aliases = { - "native_1":"mlj_1" -} +native_apis = [ + native_v1.api +] +thirdparty_apis = [ + Listenbrainz(), + Audioscrobbler() +] def init_apis(server): + for api in native_apis: + api.mount(server=server,path="apis/"+api.__apipath__) - for api in apis: - apis[api].mount(server=server,path="apis/"+api) + for api in thirdparty_apis: + aliases = api.__aliases__ + canonical = aliases[0] + api.nimrodelapi.mount(server=server,path="apis/" + canonical) + + # redirects + for alias in aliases[1:]: + altpath = "/apis/" + alias + "/" + altpath_empty = "/apis/" + alias + altpath_empty_cl = "/apis/" + alias + "/" + + def alias_api(pth=""): + redirect("/apis/" + canonical + "/" + pth + "?" + urlencode(request.query)) + + server.get(altpath)(alias_api) + server.post(altpath)(alias_api) + server.get(altpath_empty)(alias_api) + server.post(altpath_empty)(alias_api) + server.get(altpath_empty_cl)(alias_api) + server.post(altpath_empty_cl)(alias_api) diff --git a/maloja/apis/audioscrobbler.py b/maloja/apis/audioscrobbler.py index 7fff96b..7db9923 100644 --- a/maloja/apis/audioscrobbler.py +++ b/maloja/apis/audioscrobbler.py @@ -5,6 +5,11 @@ from .. import database class Audioscrobbler(APIHandler): __apiname__ = "Audioscrobbler" __doclink__ = "https://www.last.fm/api/scrobbling" + __aliases__ = [ + "audioscrobbler/2.0", + "gnufm/2.0", + "gnukebox/2.0", + ] def init(self): diff --git a/maloja/apis/listenbrainz.py b/maloja/apis/listenbrainz.py index 035aa06..f7e3a16 100644 --- a/maloja/apis/listenbrainz.py +++ b/maloja/apis/listenbrainz.py @@ -7,6 +7,10 @@ import datetime class Listenbrainz(APIHandler): __apiname__ = "Listenbrainz" __doclink__ = "https://listenbrainz.readthedocs.io/en/production/" + __aliases__ = [ + "listenbrainz/1", + "lbrnz/1" + ] def init(self): self.methods = { diff --git a/maloja/apis/native_v1.py b/maloja/apis/native_v1.py index 9ad9b1f..904edf4 100644 --- a/maloja/apis/native_v1.py +++ b/maloja/apis/native_v1.py @@ -12,6 +12,7 @@ from nimrodel import Multi api = API(delay=True) +api.__apipath__ = "mlj_1" @api.get("test") diff --git a/maloja/server.py b/maloja/server.py index 05d72f8..de2bc8e 100755 --- a/maloja/server.py +++ b/maloja/server.py @@ -59,9 +59,6 @@ auth.authapi.mount(server=webserver) from .apis import init_apis init_apis(webserver) - - - # redirects for backwards compatibility @webserver.get("/api/s/") @webserver.post("/api/s/")