git.iko.soy/typeable/http-api-data
on 63eb450 help sign in
typeable/http-api-data/Web/HttpApiData.hs
76 lines 1.6 kB history raw
1-- |
2-- Convert Haskell values to and from HTTP API data
3-- such as URL pieces, headers and query parameters.
4module Web.HttpApiData (
5 -- * Examples
6 -- $examples
8 -- * Classes
9 ToHttpApiData (..),
10 FromHttpApiData (..),
12 -- * @'Maybe'@ parsers
13 parseUrlPieceMaybe,
14 parseHeaderMaybe,
15 parseQueryParamMaybe,
17 -- * Prefix parsers
18 parseUrlPieceWithPrefix,
19 parseHeaderWithPrefix,
20 parseQueryParamWithPrefix,
22 -- * Multiple URL pieces
23 toUrlPieces,
24 parseUrlPieces,
26 -- * Other helpers
27 showTextData,
28 readTextData,
29 parseBoundedTextData,
30) where
32import Web.HttpApiData.Internal
34-- $setup
35--
36-- >>> :set -XOverloadedStrings
37-- >>> import Control.Applicative
38-- >>> import Data.Time
39-- >>> import Data.Int
40-- >>> import Data.Text (Text)
41-- >>> import Data.Time (Day)
42-- >>> import Data.Version
44-- $examples
45--
46-- Booleans:
47--
48-- >>> toUrlPiece True
49-- "true"
50-- >>> parseUrlPiece "false" :: Either Text Bool
51-- Right False
52-- >>> parseUrlPiece "something else" :: Either Text Bool
53-- Left "could not parse: `something else'"
54--
55-- Numbers:
56--
57-- >>> toUrlPiece 45.2
58-- "45.2"
59-- >>> parseUrlPiece "452" :: Either Text Int
60-- Right 452
61-- >>> parseUrlPiece "256" :: Either Text Int8
62-- Left "out of bounds: `256' (should be between -128 and 127)"
63--
64-- Strings:
65--
66-- >>> toHeader "hello"
67-- "hello"
68-- >>> parseHeader "world" :: Either Text String
69-- Right "world"
70--
71-- Calendar day:
72--
73-- >>> toQueryParam (fromGregorian 2015 10 03)
74-- "2015-10-03"
75-- >>> toGregorian <$> parseQueryParam "2016-12-01"
76-- Right (2016,12,1)