git.iko.soy/typeable/http-api-data
on fa000cc help sign in
92 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 -- * Lenient data
39 LenientData (..),
41 -- * Other helpers
42 showTextData,
43 readTextData,
44) where
46import Web.Internal.HttpApiData
48-- $setup
49--
50-- >>> :set -XOverloadedStrings
51-- >>> import Control.Applicative
52-- >>> import Data.Time.Compat
53-- >>> import Data.Int
54-- >>> import Data.Text (Text)
55-- >>> import Data.Version
57-- $examples
58--
59-- Booleans:
60--
61-- >>> toUrlPiece True
62-- "true"
63-- >>> parseUrlPiece "false" :: Either Text Bool
64-- Right False
65-- >>> parseUrlPieces ["true", "false", "undefined"] :: Either Text [Bool]
66-- Left "could not parse: `undefined'"
67--
68-- Numbers:
69--
70-- >>> toQueryParam 45.2
71-- "45.2"
72-- >>> parseQueryParam "452" :: Either Text Int
73-- Right 452
74-- >>> toQueryParams [1..5] :: [Text]
75-- ["1","2","3","4","5"]
76-- >>> parseQueryParams ["127", "255"] :: Either Text [Int8]
77-- Left "out of bounds: `255' (should be between -128 and 127)"
78--
79-- Strings:
80--
81-- >>> toHeader "hello"
82-- "hello"
83-- >>> parseHeader "world" :: Either Text String
84-- Right "world"
85--
86-- Calendar day:
87--
88-- >>> toQueryParam (fromGregorian 2015 10 03)
89-- "2015-10-03"
90-- >>> toGregorian <$> parseQueryParam "2016-12-01"
91-- Right (2016,12,1)