2014-10-25 01:27:39 +02:00
|
|
|
{-# LANGUAGE PolyKinds #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2014-10-28 09:04:27 +01:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2014-10-25 01:27:39 +02:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
module Servant.API.GetParam where
|
|
|
|
|
|
|
|
import Data.Proxy
|
|
|
|
import Data.String.Conversions
|
|
|
|
import Data.Text
|
|
|
|
import GHC.TypeLits
|
|
|
|
import Network.HTTP.Types
|
|
|
|
import Network.Wai
|
|
|
|
import Servant.API.Sub
|
|
|
|
import Servant.Client
|
2014-10-28 09:04:27 +01:00
|
|
|
import Servant.Docs
|
2014-10-25 01:27:39 +02:00
|
|
|
import Servant.Server
|
|
|
|
import Servant.Text
|
|
|
|
|
|
|
|
-- * GET params support (i.e query string arguments)
|
|
|
|
data GetParam sym a
|
|
|
|
|
|
|
|
instance (KnownSymbol sym, FromText a, HasServer sublayout)
|
|
|
|
=> HasServer (GetParam sym a :> sublayout) where
|
|
|
|
|
|
|
|
type Server (GetParam sym a :> sublayout) =
|
|
|
|
Maybe a -> Server sublayout
|
|
|
|
|
2014-10-28 10:42:49 +01:00
|
|
|
route Proxy subserver request respond = do
|
2014-10-25 01:27:39 +02:00
|
|
|
let querytext = parseQueryText $ rawQueryString request
|
|
|
|
param =
|
2014-10-28 09:04:27 +01:00
|
|
|
case lookup paramname querytext of
|
2014-10-25 01:27:39 +02:00
|
|
|
Nothing -> Nothing -- param absent from the query string
|
|
|
|
Just Nothing -> Nothing -- param present with no value -> Nothing
|
|
|
|
Just (Just v) -> fromText v -- if present, we try to convert to
|
|
|
|
-- the right type
|
|
|
|
|
2014-10-28 10:42:49 +01:00
|
|
|
route (Proxy :: Proxy sublayout) (subserver param) request respond
|
2014-10-25 01:27:39 +02:00
|
|
|
|
2014-10-28 09:04:27 +01:00
|
|
|
where paramname = cs $ symbolVal (Proxy :: Proxy sym)
|
2014-10-25 01:27:39 +02:00
|
|
|
|
|
|
|
instance (KnownSymbol sym, ToText a, HasClient sublayout)
|
|
|
|
=> HasClient (GetParam sym a :> sublayout) where
|
|
|
|
|
|
|
|
type Client (GetParam sym a :> sublayout) =
|
|
|
|
Maybe a -> Client sublayout
|
|
|
|
|
|
|
|
-- if mparam = Nothing, we don't add it to the query string
|
|
|
|
clientWithRoute Proxy req mparam =
|
|
|
|
clientWithRoute (Proxy :: Proxy sublayout) $
|
|
|
|
appendToQueryString pname mparamText req
|
|
|
|
|
|
|
|
where pname = pack pname'
|
|
|
|
pname' = symbolVal (Proxy :: Proxy sym)
|
|
|
|
mparamText = fmap toText mparam
|
2014-10-28 09:04:27 +01:00
|
|
|
|
|
|
|
instance (KnownSymbol sym, ToParam (GetParam sym a), HasDocs sublayout)
|
|
|
|
=> HasDocs (GetParam sym a :> sublayout) where
|
|
|
|
|
|
|
|
docsFor Proxy (endpoint, action) =
|
|
|
|
docsFor sublayoutP (endpoint, action')
|
|
|
|
|
|
|
|
where sublayoutP = Proxy :: Proxy sublayout
|
|
|
|
paramP = Proxy :: Proxy (GetParam sym a)
|
|
|
|
action' = over params (|> toParam paramP) action
|