diff --git a/server/handlers/api/v1_compatability.py b/server/handlers/api/v1_compatability.py index 2c1a07a..054f4a5 100644 --- a/server/handlers/api/v1_compatability.py +++ b/server/handlers/api/v1_compatability.py @@ -1,29 +1,54 @@ -import json - import tornado - -from core.utils import safe_json_dumps - - -class V1GoneHandler(tornado.web.RequestHandler): - """Returns 410 Gone with a message for any endpoints in the old API that have breaking changes in the new one or - have been retired.""" - - def post(self): - self.set_status(410) - self.write(safe_json_dumps( - "This API endpoint has a breaking change or has been removed in the current version of the Spothole API. Please see /apidocs for details of the current API version and the endpoints available.")) - self.set_header("Cache-Control", "no-store") - self.set_header("Content-Type", "application/json") - +from tornado.httpclient import AsyncHTTPClient class V1RedirectHandler(tornado.web.RequestHandler): - """Returns 308 Permanent Redirect from any path in the old API to the new one, where there were no breaking changes.""" + """Transparently proxies requests from the old API to the new one, + returning whatever the v2 endpoint returns, for endpoints with no breaking changes.""" - def get(self, path): - new_url = "/api/v2/" + path + SUPPORTED_METHODS = ("GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS") + + # Methods where an HTTP body isn't valid + _BODYLESS_METHODS = {"GET", "HEAD", "OPTIONS"} + + async def _proxy(self, path): + new_url = f"{self.request.protocol}://{self.request.host}/api/v2/{path}" if self.request.query: new_url += "?" + self.request.query - self.set_status(308) - self.set_header("Location", new_url) + + client = AsyncHTTPClient() + try: + response = await client.fetch( + new_url, + method=self.request.method, + headers=self.request.headers, + body=None if self.request.method in self._BODYLESS_METHODS else (self.request.body or b""), + raise_error=False, + follow_redirects=False, + request_timeout=10.0, + ) + except Exception as e: + raise tornado.web.HTTPError(502, reason=str(e)) + + self.set_status(response.code, response.reason) + for name, value in response.headers.get_all(): + # Let Tornado recompute these for the outgoing response + if name.lower() not in ("content-length", "transfer-encoding", "connection"): + self.add_header(name, value) + if response.body: + self.write(response.body) self.finish() + + async def get(self, path): + await self._proxy(path) + + async def post(self, path): + await self._proxy(path) + + async def put(self, path): + await self._proxy(path) + + async def delete(self, path): + await self._proxy(path) + + async def patch(self, path): + await self._proxy(path) \ No newline at end of file diff --git a/templates/add_spot.html b/templates/add_spot.html index c2afa24..36b7123 100644 --- a/templates/add_spot.html +++ b/templates/add_spot.html @@ -76,7 +76,7 @@ - + diff --git a/templates/alerts.html b/templates/alerts.html index ffdf521..773a07a 100644 --- a/templates/alerts.html +++ b/templates/alerts.html @@ -82,7 +82,7 @@ - + diff --git a/templates/bands.html b/templates/bands.html index 139a31c..e8d9557 100644 --- a/templates/bands.html +++ b/templates/bands.html @@ -79,8 +79,8 @@ - - + + diff --git a/templates/base.html b/templates/base.html index a555c65..79fcf9f 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,6 +1,6 @@ {% extends "skeleton.html" %} {% block head_extra %} - + @@ -10,10 +10,10 @@ - - - - + + + + {% end %} {% block body %}