git.iko.soy/typeable/http-api-data
on 826eff9 help sign in
typeable/http-api-data/Web/HttpApiData.hs
90 lines 1.9 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 -- * Multiple query params
27 toQueryParams,
28 parseQueryParams,
30 -- * Parsers for @'Bounded'@ @'Enum'@s
31 parseBoundedUrlPiece,
32 parseBoundedQueryParam,
33 parseBoundedHeader,
34 parseBoundedEnumOf,
35 parseBoundedEnumOfI,
36 parseBoundedTextData,
38 -- * Other helpers
39 showTextData,
40 readTextData,
41) where
43import Web.HttpApiData.Internal
45-- $setup
46--
47-- >>> :set -XOverloadedStrings
48-- >>> import Control.Applicative
49-- >>> import Data.Time
50-- >>> import Data.Int
51-- >>> import Data.Text (Text)
52-- >>> import Data.Time (Day)
53-- >>> import Data.Version
55-- $examples
56--
57-- Booleans:
58--
59-- >>> toUrlPiece True
60-- "true"
61-- >>> parseUrlPiece "false" :: Either Text Bool
62-- Right False
63-- >>> parseUrlPieces ["true", "false", "undefined"] :: Either Text [Bool]
64-- Left "could not parse: `undefined'"
65--
66-- Numbers:
67--
68-- >>> toQueryParam 45.2
69-- "45.2"
70-- >>> parseQueryParam "452" :: Either Text Int
71-- Right 452
72-- >>> toQueryParams [1..5]
73-- ["1","2","3","4","5"]
74-- >>> parseQueryParams ["127", "255"] :: Either Text [Int8]
75-- Left "out of bounds: `255' (should be between -128 and 127)"
76--
77-- Strings:
78--
79-- >>> toHeader "hello"
80-- "hello"
81-- >>> parseHeader "world" :: Either Text String
82-- Right "world"
83--
84-- Calendar day:
85--
86-- >>> toQueryParam (fromGregorian 2015 10 03)
87-- "2015-10-03"
88-- >>> toGregorian <$> parseQueryParam "2016-12-01"
89-- Right (2016,12,1)