@@ -2,9 +2,17 @@
2{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE TypeSynonymInstances #-}
4{-# LANGUAGE TypeSynonymInstances #-}
6+-- Convert Haskell values to and from HTTP API data
7+-- such as URL pieces, headers and query parameters.
5module Web.HttpApiData (
8module Web.HttpApiData (
6 ToHttpApiData (..),
13 ToHttpApiData (..),
7 FromHttpApiData (..),
14 FromHttpApiData (..),
15+ -- * Utility functions
8 parseMaybeHttpApiData,
16 parseMaybeHttpApiData,
9 showUrlPiece,
17 showUrlPiece,
10 readMaybeUrlPiece,
18 readMaybeUrlPiece,
@@ -29,6 +37,11 @@ import Data.Time (Day)
30import Text.Read (readMaybe)
38import Text.Read (readMaybe)
42+-- >>> :set -XOverloadedStrings
43+-- >>> import Data.Time
32-- | Convert value to HTTP API data.
45-- | Convert value to HTTP API data.
33class ToHttpApiData a where
46class ToHttpApiData a where
34 -- | Convert to URL path piece.
47 -- | Convert to URL path piece.
@@ -117,3 +130,35 @@ instance FromHttpApiData String where parseUrlPiece = Right . Text.unpack
117instance FromHttpApiData Text where parseUrlPiece = Right
130instance FromHttpApiData Text where parseUrlPiece = Right
118instance FromHttpApiData TL.Text where parseUrlPiece = Right . TL.fromStrict
131instance FromHttpApiData TL.Text where parseUrlPiece = Right . TL.fromStrict
119instance FromHttpApiData Day where parseUrlPiece = readEitherUrlPiece
132instance FromHttpApiData Day where parseUrlPiece = readEitherUrlPiece
138+-- >>> toUrlPiece True
140+-- >>> parseUrlPiece "False" :: Either Text Bool
142+-- >>> parseUrlPiece "something else" :: Either Text Bool
143+-- Left "could not convert: `something else'"
147+-- >>> toUrlPiece 45.2
149+-- >>> parseUrlPiece "452" :: Either Text Int
154+-- >>> toHeader "hello"
156+-- >>> parseHeader "world" :: Either Text String
161+-- >>> toQueryParam (fromGregorian 2015 10 03)
163+-- >>> toGregorian <$> parseQueryParam "2016-12-01"