on master help sign in
rename GetParam to QueryParam everywhere
fc67c3bAlp Mestanogullari
example/greet.hs+5−5
@@ -37,11 +37,11 @@ instance ToCapture (Capture "name" Text) where
37instance ToCapture (Capture "greetid" Text) where
37instance ToCapture (Capture "greetid" Text) where
38 toCapture _ = DocCapture "greetid" "identifier of the greet msg to remove"
38 toCapture _ = DocCapture "greetid" "identifier of the greet msg to remove"
39
39
40−instance ToParam (GetParam "capital" Bool) where
40+instance ToParam (QueryParam "capital" Bool) where
41 toParam _ =
41 toParam _ =
42− DocGetParam "capital"
42+ DocQueryParam "capital"
43− ["true", "false"]
43+ ["true", "false"]
44− "Get the greeting message in uppercase (true) or not (false). Default is false."
44+ "Get the greeting message in uppercase (true) or not (false). Default is false."
45
45
46instance ToSample Greet where
46instance ToSample Greet where
47 toSample Proxy = Just (encode g)
47 toSample Proxy = Just (encode g)
@@ -50,7 +50,7 @@ instance ToSample Greet where
50
50
51-- API specification
51-- API specification
52type TestApi =
52type TestApi =
53− "hello" :> Capture "name" Text :> GetParam "capital" Bool :> Get Greet
53+ "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
54 :<|> "greet" :> RQBody Greet :> Post Greet
54 :<|> "greet" :> RQBody Greet :> Post Greet
55 :<|> "delete" :> Capture "greetid" Text :> Delete
55 :<|> "delete" :> Capture "greetid" Text :> Delete
56
56
servant.cabal+1−1
@@ -22,9 +22,9 @@ library
22 Servant.API.Capture
22 Servant.API.Capture
23 Servant.API.Delete
23 Servant.API.Delete
24 Servant.API.Get
24 Servant.API.Get
25− Servant.API.GetParam
26 Servant.API.Post
25 Servant.API.Post
27 Servant.API.Put
26 Servant.API.Put
27+ Servant.API.QueryParam
28 Servant.API.Raw
28 Servant.API.Raw
29 Servant.API.RQBody
29 Servant.API.RQBody
30 Servant.API.Sub
30 Servant.API.Sub
src/Servant/API.hs+3−3
@@ -9,8 +9,8 @@ module Servant.API (
9 -- * Accessing information from the request
9 -- * Accessing information from the request
10 -- | Capturing parts of the url path as parsed values: @'Capture'@
10 -- | Capturing parts of the url path as parsed values: @'Capture'@
11 module Servant.API.Capture,
11 module Servant.API.Capture,
12− -- | Retrieving parameters from the query part of the 'URI': @'GetParam'@
12+ -- | Retrieving parameters from the query part of the 'URI': @'QueryParam'@
13− module Servant.API.GetParam,
13+ module Servant.API.QueryParam,
14 -- | Accessing the request's body: @'RQBody'@
14 -- | Accessing the request's body: @'RQBody'@
15 module Servant.API.RQBody,
15 module Servant.API.RQBody,
16
16
@@ -28,9 +28,9 @@ module Servant.API (
28import Servant.API.Capture
28import Servant.API.Capture
29import Servant.API.Delete
29import Servant.API.Delete
30import Servant.API.Get
30import Servant.API.Get
31−import Servant.API.GetParam
32import Servant.API.Post
31import Servant.API.Post
33import Servant.API.Put
32import Servant.API.Put
33+import Servant.API.QueryParam
34import Servant.API.RQBody
34import Servant.API.RQBody
35import Servant.API.Sub
35import Servant.API.Sub
36import Servant.API.Union
36import Servant.API.Union
src/Servant/API/GetParam.hs → src/Servant/API/QueryParam.hsrenamed+16−10
@@ -4,7 +4,7 @@
4{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
6{-# LANGUAGE ScopedTypeVariables #-}
6{-# LANGUAGE ScopedTypeVariables #-}
7−module Servant.API.GetParam where
7+module Servant.API.QueryParam where
8
8
9import Data.Proxy
9import Data.Proxy
10import Data.String.Conversions
10import Data.String.Conversions
@@ -18,13 +18,19 @@ import Servant.Docs
18import Servant.Server
18import Servant.Server
19import Servant.Utils.Text
19import Servant.Utils.Text
20
20
21−-- * GET params support (i.e query string arguments)
21+-- * Query String parameter lookup
22+
23+-- | Your must implement:
24+--
25+-- - a @'FromText' a@ instance for serving
26+-- - a @'ToText' a@ instance for (client-side) querying
27+-- - a @'ToParam' ('QueryParam' sym a)@ instance for automatic documentation generation
22−data GetParam sym a
28+data QueryParam sym a
23
29
24instance (KnownSymbol sym, FromText a, HasServer sublayout)
30instance (KnownSymbol sym, FromText a, HasServer sublayout)
25− => HasServer (GetParam sym a :> sublayout) where
31+ => HasServer (QueryParam sym a :> sublayout) where
26
32
27− type Server (GetParam sym a :> sublayout) =
33+ type Server (QueryParam sym a :> sublayout) =
28 Maybe a -> Server sublayout
34 Maybe a -> Server sublayout
29
35
30 route Proxy subserver request respond = do
36 route Proxy subserver request respond = do
@@ -41,9 +47,9 @@ instance (KnownSymbol sym, FromText a, HasServer sublayout)
41 where paramname = cs $ symbolVal (Proxy :: Proxy sym)
47 where paramname = cs $ symbolVal (Proxy :: Proxy sym)
42
48
43instance (KnownSymbol sym, ToText a, HasClient sublayout)
49instance (KnownSymbol sym, ToText a, HasClient sublayout)
44− => HasClient (GetParam sym a :> sublayout) where
50+ => HasClient (QueryParam sym a :> sublayout) where
45
51
46− type Client (GetParam sym a :> sublayout) =
52+ type Client (QueryParam sym a :> sublayout) =
47 Maybe a -> Client sublayout
53 Maybe a -> Client sublayout
48
54
49 -- if mparam = Nothing, we don't add it to the query string
55 -- if mparam = Nothing, we don't add it to the query string
@@ -55,12 +61,12 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
55 pname' = symbolVal (Proxy :: Proxy sym)
61 pname' = symbolVal (Proxy :: Proxy sym)
56 mparamText = fmap toText mparam
62 mparamText = fmap toText mparam
57
63
58−instance (KnownSymbol sym, ToParam (GetParam sym a), HasDocs sublayout)
64+instance (KnownSymbol sym, ToParam (QueryParam sym a), HasDocs sublayout)
59− => HasDocs (GetParam sym a :> sublayout) where
65+ => HasDocs (QueryParam sym a :> sublayout) where
60
66
61 docsFor Proxy (endpoint, action) =
67 docsFor Proxy (endpoint, action) =
62 docsFor sublayoutP (endpoint, action')
68 docsFor sublayoutP (endpoint, action')
63
69
64 where sublayoutP = Proxy :: Proxy sublayout
70 where sublayoutP = Proxy :: Proxy sublayout
65− paramP = Proxy :: Proxy (GetParam sym a)
71+ paramP = Proxy :: Proxy (QueryParam sym a)
66 action' = over params (|> toParam paramP) action
72 action' = over params (|> toParam paramP) action
src/Servant/Docs.hs+16−16
@@ -56,11 +56,11 @@
56-- >
56-- >
57-- > where g = Greet "Hello, haskeller!"
57-- > where g = Greet "Hello, haskeller!"
58-- >
58-- >
59−-- > instance ToParam (GetParam "capital" Bool) where
59+-- > instance ToParam (QueryParam "capital" Bool) where
60-- > toParam _ =
60-- > toParam _ =
61−-- > DocGetParam "capital"
61+-- > DocQueryParam "capital"
62−-- > ["true", "false"]
62+-- > ["true", "false"]
63−-- > "Get the greeting message in uppercase (true) or not (false). Default is false."
63+-- > "Get the greeting message in uppercase (true) or not (false). Default is false."
64-- >
64-- >
65-- > instance ToCapture (Capture "name" Text) where
65-- > instance ToCapture (Capture "name" Text) where
66-- > toCapture _ = DocCapture "name" "name of the person to greet"
66-- > toCapture _ = DocCapture "name" "name of the person to greet"
@@ -70,7 +70,7 @@
70-- >
70-- >
71-- > -- API specification
71-- > -- API specification
72-- > type TestApi =
72-- > type TestApi =
73−-- > "hello" :> Capture "name" Text :> GetParam "capital" Bool :> Get Greet
73+-- > "hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get Greet
74-- > :<|> "greet" :> RQBody Greet :> Post Greet
74-- > :<|> "greet" :> RQBody Greet :> Post Greet
75-- > :<|> "delete" :> Capture "greetid" Text :> Delete
75-- > :<|> "delete" :> Capture "greetid" Text :> Delete
76-- >
76-- >
@@ -95,7 +95,7 @@ module Servant.Docs
95 , Endpoint, path, method, defEndpoint
95 , Endpoint, path, method, defEndpoint
96 , API, emptyAPI
96 , API, emptyAPI
97 , DocCapture(..), capSymbol, capDesc
97 , DocCapture(..), capSymbol, capDesc
98− , DocGetParam(..), paramName, paramValues, paramDesc
98+ , DocQueryParam(..), paramName, paramValues, paramDesc
99 , Response, respStatus, respBody, defResponse
99 , Response, respStatus, respBody, defResponse
100 , Action, captures, params, rqbody, response, defAction
100 , Action, captures, params, rqbody, response, defAction
101 , single
101 , single
@@ -190,12 +190,12 @@ data DocCapture = DocCapture
190 , _capDesc :: String -- user supplied
190 , _capDesc :: String -- user supplied
191 } deriving (Eq, Show)
191 } deriving (Eq, Show)
192
192
193−-- | A type to represent /GET/ parameters. Holds its name,
193+-- | A type to represent a /GET/ parameter from the Query String. Holds its name,
194-- the possible values (leave empty if there isn't a finite number of them),
194-- the possible values (leave empty if there isn't a finite number of them),
195-- and a description of how it influences the output or behavior.
195-- and a description of how it influences the output or behavior.
196--
196--
197-- Write a 'ToParam' instance for your GET parameter types
197-- Write a 'ToParam' instance for your GET parameter types
198−data DocGetParam = DocGetParam
198+data DocQueryParam = DocQueryParam
199 { _paramName :: String -- type supplied
199 { _paramName :: String -- type supplied
200 , _paramValues :: [String] -- user supplied
200 , _paramValues :: [String] -- user supplied
201 , _paramDesc :: String -- user supplied
201 , _paramDesc :: String -- user supplied
@@ -243,7 +243,7 @@ defResponse = Response 200 Nothing
243-- to transform an action and add some information to it.
243-- to transform an action and add some information to it.
244data Action = Action
244data Action = Action
245 { _captures :: [DocCapture] -- type collected + user supplied info
245 { _captures :: [DocCapture] -- type collected + user supplied info
246− , _params :: [DocGetParam] -- type collected + user supplied info
246+ , _params :: [DocQueryParam] -- type collected + user supplied info
247 , _rqbody :: Maybe ByteString -- user supplied
247 , _rqbody :: Maybe ByteString -- user supplied
248 , _response :: Response -- user supplied
248 , _response :: Response -- user supplied
249 } deriving (Eq, Show)
249 } deriving (Eq, Show)
@@ -273,7 +273,7 @@ single = HM.singleton
273-- gimme some lenses
273-- gimme some lenses
274makeLenses ''Endpoint
274makeLenses ''Endpoint
275makeLenses ''DocCapture
275makeLenses ''DocCapture
276−makeLenses ''DocGetParam
276+makeLenses ''DocQueryParam
277makeLenses ''Response
277makeLenses ''Response
278makeLenses ''Action
278makeLenses ''Action
279
279
@@ -319,13 +319,13 @@ class ToJSON a => ToSample a where
319--
319--
320-- Example of an instance:
320-- Example of an instance:
321--
321--
322−-- > instance ToParam (GetParam "capital" Bool) where
322+-- > instance ToParam (QueryParam "capital" Bool) where
323-- > toParam _ =
323-- > toParam _ =
324−-- > DocGetParam "capital"
324+-- > DocQueryParam "capital"
325−-- > ["true", "false"]
325+-- > ["true", "false"]
326−-- > "Get the greeting message in uppercase (true) or not (false). Default is false."
326+-- > "Get the greeting message in uppercase (true) or not (false). Default is false."
327class ToParam t where
327class ToParam t where
328− toParam :: Proxy t -> DocGetParam
328+ toParam :: Proxy t -> DocQueryParam
329
329
330-- | The class that helps us automatically get documentation
330-- | The class that helps us automatically get documentation
331-- for URL captures.
331-- for URL captures.
@@ -364,7 +364,7 @@ printMarkdown = imapM_ printEndpoint
364 captureStr cap =
364 captureStr cap =
365 putStrLn $ "- *" ++ (cap ^. capSymbol) ++ "*: " ++ (cap ^. capDesc)
365 putStrLn $ "- *" ++ (cap ^. capSymbol) ++ "*: " ++ (cap ^. capDesc)
366
366
367− paramsStr :: [DocGetParam] -> IO ()
367+ paramsStr :: [DocQueryParam] -> IO ()
368 paramsStr [] = return ()
368 paramsStr [] = return ()
369 paramsStr l = do
369 paramsStr l = do
370 putStrLn "**GET Parameters**: "
370 putStrLn "**GET Parameters**: "
test/Servant/ServerSpec.hs+12−12
@@ -22,8 +22,8 @@ import Test.Hspec.Wai
22
22
23import Servant.API.Capture
23import Servant.API.Capture
24import Servant.API.Get
24import Servant.API.Get
25−import Servant.API.GetParam
26import Servant.API.Post
25import Servant.API.Post
26+import Servant.API.QueryParam
27import Servant.API.Raw
27import Servant.API.Raw
28import Servant.API.RQBody
28import Servant.API.RQBody
29import Servant.API.Sub
29import Servant.API.Sub
@@ -67,7 +67,7 @@ spec :: Spec
67spec = do
67spec = do
68 captureSpec
68 captureSpec
69 getSpec
69 getSpec
70− getParamSpec
70+ queryParamSpec
71 postSpec
71 postSpec
72 rawSpec
72 rawSpec
73 unionSpec
73 unionSpec
@@ -117,19 +117,19 @@ getSpec = do
117 post "/" "" `shouldRespondWith` 405
117 post "/" "" `shouldRespondWith` 405
118
118
119
119
120−type GetParamApi = GetParam "name" String :> Get Person
120+type QueryParamApi = QueryParam "name" String :> Get Person
121−getParamApi :: Proxy GetParamApi
121+queryParamApi :: Proxy QueryParamApi
122−getParamApi = Proxy
122+queryParamApi = Proxy
123
123
124−getParamServer :: Server GetParamApi
124+queryParamServer :: Server QueryParamApi
125−getParamServer (Just name) = return alice{name = name}
125+queryParamServer (Just name) = return alice{name = name}
126−getParamServer Nothing = return alice
126+queryParamServer Nothing = return alice
127
127
128−getParamSpec :: Spec
128+queryParamSpec :: Spec
129−getParamSpec = do
129+queryParamSpec = do
130− describe "Servant.API.GetParam" $ do
130+ describe "Servant.API.QueryParam" $ do
131 it "allows to retrieve GET parameters" $ do
131 it "allows to retrieve GET parameters" $ do
132− (flip runSession) (serve getParamApi getParamServer) $ do
132+ (flip runSession) (serve queryParamApi queryParamServer) $ do
133 let params = "?name=bob"
133 let params = "?name=bob"
134 response <- Network.Wai.Test.request defaultRequest{
134 response <- Network.Wai.Test.request defaultRequest{
135 rawQueryString = params,
135 rawQueryString = params,