git.iko.soy/typeable/http-api-data
on master help sign in
Add to/fromQueryParams helpers
875059eNickolay Kudasov
Web/HttpApiData.hs+4
@@ -23,6 +23,10 @@ module Web.HttpApiData (
23 toUrlPieces,
23 toUrlPieces,
24 parseUrlPieces,
24 parseUrlPieces,
25
25
26+ -- * Multiple query params
27+ toQueryParams,
28+ parseQueryParams,
29+
26 -- * Other helpers
30 -- * Other helpers
27 showTextData,
31 showTextData,
28 readTextData,
32 readTextData,
Web/HttpApiData/Internal.hs+17
@@ -11,6 +11,7 @@ module Web.HttpApiData.Internal where
11
11
12#if __GLASGOW_HASKELL__ < 710
12#if __GLASGOW_HASKELL__ < 710
13import Control.Applicative
13import Control.Applicative
14+import Data.Traversable (traverse)
14#endif
15#endif
15import Control.Arrow ((&&&))
16import Control.Arrow ((&&&))
16
17
@@ -87,6 +88,22 @@ toUrlPieces = fmap toUrlPiece
87parseUrlPieces :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)
88parseUrlPieces :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)
88parseUrlPieces = traverse parseUrlPiece
89parseUrlPieces = traverse parseUrlPiece
89
90
91+-- | Convert multiple values to a list of query parameter values.
92+--
93+-- >>> toQueryParams [fromGregorian 2015 10 03, fromGregorian 2015 12 01]
94+-- ["2015-10-03","2015-12-01"]
95+toQueryParams :: (Functor t, ToHttpApiData a) => t a -> t Text
96+toQueryParams = fmap toQueryParam
97+
98+-- | Parse multiple query parameters.
99+--
100+-- >>> parseQueryParams ["1", "2", "3"] :: Either Text [Int]
101+-- Right [1,2,3]
102+-- >>> parseQueryParams ["64", "128", "256"] :: Either Text [Word8]
103+-- Left "out of bounds: `256' (should be between 0 and 255)"
104+parseQueryParams :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)
105+parseQueryParams = traverse parseQueryParam
106+
90-- | Parse URL path piece in a @'Maybe'@.
107-- | Parse URL path piece in a @'Maybe'@.
91--
108--
92-- >>> parseUrlPieceMaybe "12" :: Maybe Int
109-- >>> parseUrlPieceMaybe "12" :: Maybe Int