Browse Source

Be a bit more consistent about api_post return types

Getty Ritter 2 years ago
parent
commit
1fc613b33a
3 changed files with 18 additions and 16 deletions
  1. 8 14
      lc/app.py
  2. 9 1
      lc/web.py
  3. 1 1
      templates/login.mustache

+ 8 - 14
lc/app.py

@@ -30,8 +30,9 @@ class Index(Endpoint):
 @endpoint("/auth")
 class Auth(Endpoint):
     def api_post(self):
-        _, token = m.User.login(self.request_data(r.User))
-        return {"token": token}
+        u, token = m.User.login(self.request_data(r.User))
+        flask.session["auth"] = token
+        return self.api_ok(u.base_url(), {"token": token})
 
 
 @endpoint("/login")
@@ -39,12 +40,6 @@ class Login(Endpoint):
     def html(self):
         return render("main", title="login", content=render("login"), user=self.user)
 
-    def api_post(self):
-        req = self.request_data(r.User)
-        u, token = m.User.login(req)
-        flask.session["auth"] = token
-        raise e.LCRedirect(u.base_url())
-
 
 @endpoint("/logout")
 class Logout(Endpoint):
@@ -56,7 +51,7 @@ class Logout(Endpoint):
     def api_post(self):
         if "auth" in flask.session:
             del flask.session["auth"]
-        raise e.LCRedirect("/")
+        return self.api_ok("/")
 
 
 @endpoint("/u")
@@ -81,7 +76,7 @@ class CreateUser(Endpoint):
         req = self.request_data(r.NewUser).to_user_request()
         u = m.User.from_invite(req, token)
         flask.session["auth"] = req.to_token()
-        raise e.LCRedirect(u.base_url())
+        return self.api_ok(u.base_url(), u)
 
 
 @endpoint("/u/<string:slug>")
@@ -117,8 +112,8 @@ class UserConfig(Endpoint):
 class CreateInvite(Endpoint):
     def api_post(self, user: str):
         u = self.require_authentication(user)
-        m.UserInvite.manufacture(u)
-        raise e.LCRedirect(f"/u/{user}/config")
+        invite = m.UserInvite.manufacture(u)
+        return self.api_ok(f"/u/{user}/config", {"invite": invite.token})
 
 
 @endpoint("/u/<string:user>/l")
@@ -130,7 +125,7 @@ class CreateLink(Endpoint):
         u = self.require_authentication(user)
         req = self.request_data(r.Link)
         l = m.Link.from_request(u, req)
-        raise e.LCRedirect(l.link_url())
+        return self.api_ok(l.link_url(), l)
 
 
 @endpoint("/u/<string:user>/l/<string:link>")
@@ -146,7 +141,6 @@ class GetLink(Endpoint):
             content=render("linklist", links=[l]),
             user=self.user,
         )
-        pass
 
 
 @endpoint("/u/<string:user>/t/<path:tag>")

+ 9 - 1
lc/web.py

@@ -38,6 +38,14 @@ class Endpoint:
             if u.authenticate(payload["password"]):
                 self.user = u
 
+    def api_ok(self, redirect: str, data: dict = {"status": "ok"}):
+        if flask.request.content_type == "application/json":
+            return flask.jsonify(data)
+        elif flask.request.content_type == "application/x-www-form-urlencoded":
+            raise e.LCRedirect(redirect)
+        else:
+            raise e.BadContentType(flask.request.content_type or "unknown")
+
     def request_data(self, cls: Type[T]) -> T:
         """Construct a Request model from either a JSON payload or a urlencoded payload"""
         if flask.request.content_type == "application/json":
@@ -67,7 +75,7 @@ class Endpoint:
                 # should redirect to the page where that information
                 # can be viewed instead of returning that
                 # information. (I think.)
-                return flask.jsonify(self.api_post(*args, **kwargs))
+                return self.api_post(*args, **kwargs)
             elif (
                 flask.request.method in ["GET", "HEAD"]
                 and flask.request.content_type == "application/json"

+ 1 - 1
templates/login.mustache

@@ -1,5 +1,5 @@
 <div class="loginform">
-  <form name="login" method="POST">
+  <form name="login" method="POST" action="/auth">
 
     <div class="userform">
       <label for="username">User</label>