-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I elegantly handle routes with multiple parameters? #34
Comments
Hello, no need to apologize, there is indeed design flaw in As a temporary workaround you can use handler q1 q2 ... qN session = ...
handler' q1 q2 ... qN = cookied (handler q1 q2) -- here session variable can be omitted
-- or when session argument goes first
handler session q1 q2 ... qN = ...
handler' session q1 q2 ... qN = cookied (\s -> handler s q1 q2 ... qN) session Looks ugly, but should work. I'm thinking how to simplify it. The problem is that As a side note, there is one more problem: I have few ideas, I would like try before resorting to template haskell, so it might take the time :) |
Done, it is possible without TH. Here is an example of new type TestApi
= "test-0-0"
:> AuthProtect "cookie-auth"
:> Get '[JSON] (Cookied String)
:<|> "test-1-0"
:> AuthProtect "cookie-auth"
:> QueryParam "q1" String
:> Get '[JSON] (Cookied String)
:<|> "test-1-1"
:> QueryParam "q1" String
:> AuthProtect "cookie-auth"
:> Get '[JSON] (Cookied String)
:<|> "test-2-0"
:> AuthProtect "cookie-auth"
:> QueryParam "q1" String
:> QueryParam "q2" Int
:> Get '[JSON] (Cookied String)
:<|> "test-2-1"
:> QueryParam "q1" String
:> AuthProtect "cookie-auth"
:> QueryParam "q2" Int
:> Get '[JSON] (Cookied String)
:<|> "test-2-2"
:> QueryParam "q1" String
:> QueryParam "q2" Int
:> AuthProtect "cookie-auth"
:> Get '[JSON] (Cookied String)
:<|> "test-3-0"
:> AuthProtect "cookie-auth"
:> QueryParam "q1" String
:> QueryParam "q2" Int
:> QueryParam "q3" Bool
:> Get '[JSON] (Cookied String)
:<|> "test-3-1"
:> QueryParam "q1" String
:> AuthProtect "cookie-auth"
:> QueryParam "q2" Int
:> QueryParam "q3" Bool
:> Get '[JSON] (Cookied String)
:<|> "test-3-2"
:> QueryParam "q1" String
:> QueryParam "q2" Int
:> AuthProtect "cookie-auth"
:> QueryParam "q3" Bool
:> Get '[JSON] (Cookied String)
:<|> "test-3-3"
:> QueryParam "q1" String
:> QueryParam "q2" Int
:> QueryParam "q3" Bool
:> AuthProtect "cookie-auth"
:> Get '[JSON] (Cookied String)
test0 :: Session -> Handler String
test0 session = return . concat $ (["test-0-0::", show session] :: [String])
test10 :: Session -> Maybe String -> Handler String
test10 session q1 = return . concat $ (["test-1-0::", show q1, "::", show session] :: [String])
test11 :: Maybe String -> Session -> Handler String
test11 q1 session = return . concat $ (["test-1-1::", show q1, "::", show session] :: [String])
test20 :: Session -> Maybe String -> Maybe Int -> Handler String
test20 session q1 q2 = return . concat $ (["test-2-0::", show q1, "::", show q2, "::", show session] :: [String])
test21 :: Maybe String -> Session -> Maybe Int -> Handler String
test21 q1 session q2 = return . concat $ (["test-2-1::", show q1, "::", show q2, "::", show session] :: [String])
test22 :: Maybe String -> Maybe Int -> Session -> Handler String
test22 q1 q2 session = return . concat $ (["test-2-2::", show q1, "::", show q2, "::", show session] :: [String])
test30 :: Session -> Maybe String -> Maybe Int -> Maybe Bool -> Handler String
test30 session q1 q2 q3 = return . concat $ (["test-3-0::", show q1, "::", show q2, "::", show q3, "::", show session] :: [String])
test31 :: Maybe String -> Session -> Maybe Int -> Maybe Bool -> Handler String
test31 q1 session q2 q3 = return . concat $ (["test-3-1::", show q1, "::", show q2, "::", show q3, "::", show session] :: [String])
test32 :: Maybe String -> Maybe Int -> Session -> Maybe Bool -> Handler String
test32 q1 q2 session q3 = return . concat $ (["test-3-2::", show q1, "::", show q2, "::", show q3, "::", show session] :: [String])
test33 :: Maybe String -> Maybe Int -> Maybe Bool -> Session -> Handler String
test33 q1 q2 q3 session = return . concat $ (["test-3-3::", show q1, "::", show q2, "::", show q3, "::", show session] :: [String])
serveTest
= cookied' test0
:<|> cookied' test10
:<|> cookied' test11
:<|> cookied' test20
:<|> cookied' test21
:<|> cookied' test22
:<|> cookied' test30
:<|> cookied' test31
:<|> cookied' test32
:<|> cookied' test33
where
cookied' :: CookiedWrapper Session
cookied' = cookied settings rs sks (Proxy :: Proxy Session) So, to use it you should do the following:
The price of such wrapper is obscure compiler error messages when something goes wrong. In case of ambiguity, make sure you provided signature for every function you pass to |
Awesome! Thanks for the quick update. I'll give it a try and let you know how it goes. |
@zohl wanted to let you know that I tried the new |
Alright, thank you for letting me know! |
Hi, first of all, I apologize if this is a general Haskell question but I'm currently trying to use
servant-auth-cookie
to add authentication to an API with several endpoints. The issue is that most of my endpoints have several query parameters and/or captures. I struggled with this for a while, and eventually ended up rolling my own version for handlers of arbitrary arity:It seems like ought to be an easier solution. Am I doing this wrong? Thanks!
The text was updated successfully, but these errors were encountered: