http-api-data 0.2 → 0.2.1
raw patch · 5 files changed
+69/−16 lines, 5 files
Files
- CHANGELOG.md +7/−0
- README.md +10/−8
- Web/HttpApiData.hs +17/−6
- Web/HttpApiData/Internal.hs +34/−1
- http-api-data.cabal +1/−1
CHANGELOG.md view
@@ -1,3 +1,10 @@+0.2.1+---++* Add helpers for multiple URL pieces and query params:+ * `toUrlPieces`, `parseUrlPieces`+ * `toQueryParams`, `parseQueryParams`+ 0.2 ---
README.md view
@@ -11,22 +11,24 @@ ``` >>> toUrlPiece True-"True"->>> parseUrlPiece "False" :: Either Text Bool+"true"+>>> parseUrlPiece "false" :: Either Text Bool Right False->>> parseUrlPiece "something else" :: Either Text Bool-Left "could not parse: `something else'"+>>> parseUrlPieces ["true", "false", "undefined"] :: Either Text [Bool]+Left "could not parse: `undefined'" ``` Numbers: ```->>> toUrlPiece 45.2+>>> toQueryParam 45.2 "45.2"->>> parseUrlPiece "452" :: Either Text Int+>>> parseQueryParam "452" :: Either Text Int Right 452->>> parseUrlPiece "256" :: Either Text Int8-Left "out of bounds: `256' (should be between -128 and 127)"+>>> toQueryParams [1..5]+["1","2","3","4","5"]+>>> parseQueryParams ["127", "255"] :: Either Text [Int8]+Left "out of bounds: `255' (should be between -128 and 127)" ``` Strings:
Web/HttpApiData.hs view
@@ -19,6 +19,14 @@ parseHeaderWithPrefix, parseQueryParamWithPrefix, + -- * Multiple URL pieces+ toUrlPieces,+ parseUrlPieces,++ -- * Multiple query params+ toQueryParams,+ parseQueryParams,+ -- * Other helpers showTextData, readTextData,@@ -45,17 +53,19 @@ -- "true" -- >>> parseUrlPiece "false" :: Either Text Bool -- Right False--- >>> parseUrlPiece "something else" :: Either Text Bool--- Left "could not parse: `something else'"+-- >>> parseUrlPieces ["true", "false", "undefined"] :: Either Text [Bool]+-- Left "could not parse: `undefined'" -- -- Numbers: ----- >>> toUrlPiece 45.2+-- >>> toQueryParam 45.2 -- "45.2"--- >>> parseUrlPiece "452" :: Either Text Int+-- >>> parseQueryParam "452" :: Either Text Int -- Right 452--- >>> parseUrlPiece "256" :: Either Text Int8--- Left "out of bounds: `256' (should be between -128 and 127)"+-- >>> toQueryParams [1..5]+-- ["1","2","3","4","5"]+-- >>> parseQueryParams ["127", "255"] :: Either Text [Int8]+-- Left "out of bounds: `255' (should be between -128 and 127)" -- -- Strings: --@@ -70,3 +80,4 @@ -- "2015-10-03" -- >>> toGregorian <$> parseQueryParam "2016-12-01" -- Right (2016,12,1)+
Web/HttpApiData/Internal.hs view
@@ -11,6 +11,7 @@ #if __GLASGOW_HASKELL__ < 710 import Control.Applicative+import Data.Traversable (Traversable(traverse)) #endif import Control.Arrow ((&&&)) @@ -71,6 +72,38 @@ parseQueryParam :: Text -> Either Text a parseQueryParam = parseUrlPiece +-- | Convert multiple values to a list of URL pieces.+--+-- >>> toUrlPieces [1, 2, 3]+-- ["1","2","3"]+toUrlPieces :: (Functor t, ToHttpApiData a) => t a -> t Text+toUrlPieces = fmap toUrlPiece++-- | Parse multiple URL pieces.+--+-- >>> parseUrlPieces ["true", "false"] :: Either Text [Bool]+-- Right [True,False]+-- >>> parseUrlPieces ["123", "hello", "world"] :: Either Text [Int]+-- Left "could not parse: `hello' (input does not start with a digit)"+parseUrlPieces :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)+parseUrlPieces = traverse parseUrlPiece++-- | Convert multiple values to a list of query parameter values.+--+-- >>> toQueryParams [fromGregorian 2015 10 03, fromGregorian 2015 12 01]+-- ["2015-10-03","2015-12-01"]+toQueryParams :: (Functor t, ToHttpApiData a) => t a -> t Text+toQueryParams = fmap toQueryParam++-- | Parse multiple query parameters.+--+-- >>> parseQueryParams ["1", "2", "3"] :: Either Text [Int]+-- Right [1,2,3]+-- >>> parseQueryParams ["64", "128", "256"] :: Either Text [Word8]+-- Left "out of bounds: `256' (should be between 0 and 255)"+parseQueryParams :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)+parseQueryParams = traverse parseQueryParam+ -- | Parse URL path piece in a @'Maybe'@. -- -- >>> parseUrlPieceMaybe "12" :: Maybe Int@@ -271,7 +304,7 @@ runReader :: Reader a -> Text -> Either Text a runReader reader input = case reader input of- Left err -> Left (T.pack err)+ Left err -> Left ("could not parse: `" <> input <> "' (" <> T.pack err <> ")") Right (x, rest) | T.null rest -> Right x | otherwise -> defaultParseError input
http-api-data.cabal view
@@ -1,5 +1,5 @@ name: http-api-data-version: 0.2+version: 0.2.1 license: BSD3 license-file: LICENSE author: Nickolay Kudasov <nickolay.kudasov@gmail.com>