on master help sign in
Add QParam type for encoding ok/missing/invalid query param
commitdb03e68546f7b515db2317563da568dda5ee0aa8 authorGreg Hale <imalsogreg@gmail.com> dateSat 9 Jul 2016, 16:34 (10 years ago) parent7be71fc
3 files changed+17−6
exec/Example.hs+1−1 src/Servant/Common/Req.hs+13−3 src/Servant/Reflex.hs+3−2
exec/Example.hs+1−1
@@ -53,7 +53,7 @@ run = do
5353 text "Name"
5454 el "br" $ return ()
5555 inp :: Dynamic t String <- fmap value (textInput def)
56− let checkedName = fmap (\i -> bool (Right i) (Left "Need a name") (null i)) (current inp)
56+ let checkedName = fmap (\i -> bool (QParamSome i) (QParamInvalid "Need a name") (null i)) (current inp)
5757 el "br" $ return ()
5858
5959 text "Greetings (space-separated)"
src/Servant/Common/Req.hs+13−3
@@ -14,6 +14,7 @@ import qualified Data.Text.Encoding as TE
1414-- import qualified Data.Foldable as F
1515import qualified Data.List as L
1616import Data.Proxy
17+import qualified Data.Text as T
1718import qualified Data.Map as Map
1819-- import Data.Monoid
1920-- import Data.String
@@ -58,7 +59,15 @@ response (ResponseSuccess _ x) = Just x
5859response (ResponseFailure _ x) = Just x
5960response _ = Nothing
6061
61−data QueryPart t = QueryPartParam (Behavior t (Either String String))
62+
63+data QParam a = QParamSome a | QNone | QParamInvalid String
64+
65+qParamToQueryPart :: ToHttpApiData a => QParam a -> Either String (Maybe String)
66+qParamToQueryPart (QParamSome a) = Right (Just . T.unpack $ toQueryParam a)
67+qParamToQueryPart QNone = Right Nothing
68+qParamToQueryPart (QParamInvalid e) = Left e
69+
70+data QueryPart t = QueryPartParam (Behavior t (Either String (Maybe String)))
6271 | QueryPartParams (Behavior t [String])
6372 | QueryPartFlag (Behavior t Bool)
6473
@@ -117,8 +126,9 @@ performRequest reqMethod req reqHost trigger = do
117126 queryPartString :: (String, QueryPart t) -> Behavior t (Maybe (Either String String))
118127 queryPartString (pName, qp) = case qp of
119128 QueryPartParam p -> ffor p $ \case
120− Left e -> Just (Left e)
121− Right a -> Just (Right $ pName ++ "=" ++ a)
129+ Left e -> Just (Left e)
130+ Right (Just a) -> Just (Right $ pName ++ "=" ++ a)
131+ Right Nothing -> Nothing
122132 QueryPartParams ps -> ffor ps $ \pStrings ->
123133 if null pStrings
124134 then Nothing
src/Servant/Reflex.hs+3−2
@@ -209,6 +209,7 @@ instance HasClient t m sublayout
209209 clientWithRoute Proxy q =
210210 clientWithRoute (Proxy :: Proxy sublayout) q
211211
212+
212213-- | If you use a 'QueryParam' in one of your endpoints in your API,
213214-- the corresponding querying function will automatically take
214215-- an additional argument of the type specified by your 'QueryParam',
@@ -240,7 +241,7 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout, Reflex t)
240241
241242 type Client t m (QueryParam sym a :> sublayout) =
242243 -- TODO (Maybe a), or (Maybe (Maybe a))? (should the user be able to send a Nothing)
243− Behavior t (Either String a) -> Client t m sublayout
244+ Behavior t (QParam a) -> Client t m sublayout
244245
245246 -- if mparam = Nothing, we don't add it to the query string
246247 clientWithRoute Proxy q req baseurl mparam =
@@ -248,7 +249,7 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient t m sublayout, Reflex t)
248249 (req {qParams = paramPair : qParams req}) baseurl
249250
250251 where pname = symbolVal (Proxy :: Proxy sym)
251− p prm = QueryPartParam $ (fmap . fmap) (unpack . toQueryParam) prm
252+ p prm = QueryPartParam $ fmap (qParamToQueryPart) prm -- (fmap . fmap) (unpack . toQueryParam) prm
252253 paramPair = (pname, p mparam)
253254
254255-- | If you use a 'QueryParams' in one of your endpoints in your API,