Safe Haskell | None |
---|---|
Language | Haskell2010 |
Web.HttpApiData
Contents
Description
Convert Haskell values to and from HTTP API data such as URL pieces, headers and query parameters.
Synopsis
- class ToHttpApiData a where
- class FromHttpApiData a where
- parseUrlPieceMaybe :: FromHttpApiData a => Text -> Maybe a
- parseHeaderMaybe :: FromHttpApiData a => ByteString -> Maybe a
- parseQueryParamMaybe :: FromHttpApiData a => Text -> Maybe a
- parseUrlPieceWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a
- parseHeaderWithPrefix :: FromHttpApiData a => ByteString -> ByteString -> Either Text a
- parseQueryParamWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a
- toUrlPieces :: (Functor t, ToHttpApiData a) => t a -> t Text
- parseUrlPieces :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)
- toQueryParams :: (Functor t, ToHttpApiData a) => t a -> t Text
- parseQueryParams :: (Traversable t, FromHttpApiData a) => t Text -> Either Text (t a)
- parseBoundedUrlPiece :: (ToHttpApiData a, Bounded a, Enum a) => Text -> Either Text a
- parseBoundedQueryParam :: (ToHttpApiData a, Bounded a, Enum a) => Text -> Either Text a
- parseBoundedHeader :: (ToHttpApiData a, Bounded a, Enum a) => ByteString -> Either Text a
- parseBoundedEnumOf :: (Bounded a, Enum a) => (a -> Text) -> Text -> Either Text a
- parseBoundedEnumOfI :: (Bounded a, Enum a) => (a -> Text) -> Text -> Either Text a
- parseBoundedTextData :: (Show a, Bounded a, Enum a) => Text -> Either Text a
- newtype LenientData a = LenientData {
- getLenientData :: Either Text a
- showTextData :: Show a => a -> Text
- readTextData :: Read a => Text -> Either Text a
Examples
Booleans:
>>>
toUrlPiece True
"true">>>
parseUrlPiece "false" :: Either Text Bool
Right False>>>
parseUrlPieces ["true", "false", "undefined"] :: Either Text [Bool]
Left "could not parse: `undefined'"
Numbers:
>>>
toQueryParam 45.2
"45.2">>>
parseQueryParam "452" :: Either Text Int
Right 452>>>
toQueryParams [1..5] :: [Text]
["1","2","3","4","5"]>>>
parseQueryParams ["127", "255"] :: Either Text [Int8]
Left "out of bounds: `255' (should be between -128 and 127)"
Strings:
>>>
toHeader "hello"
"hello">>>
parseHeader "world" :: Either Text String
Right "world"
Calendar day:
>>>
toQueryParam (fromGregorian 2015 10 03)
"2015-10-03">>>
toGregorian <$> parseQueryParam "2016-12-01"
Right (2016,12,1)
Classes
class ToHttpApiData a where Source #
Convert value to HTTP API data.
WARNING: Do not derive this using DeriveAnyClass
as the generated
instance will loop indefinitely.
Minimal complete definition
Methods
toUrlPiece :: a -> Text Source #
Convert to URL path piece.
toEncodedUrlPiece :: a -> Builder Source #
Convert to a URL path piece, making sure to encode any special chars.
The default definition uses encodePathSegmentsRelative
,
but this may be overriden with a more efficient version.
toHeader :: a -> ByteString Source #
Convert to HTTP header value.
toQueryParam :: a -> Text Source #
Convert to query param value.