http-api-data-0.1.1: Converting to/from HTTP API data like URL pieces, headers and query parameters.

Safe HaskellSafe
LanguageHaskell98

Web.HttpApiData.Internal

Description

Convert Haskell values to and from HTTP API data such as URL pieces, headers and query parameters.

Synopsis

Documentation

class ToHttpApiData a where Source

Convert value to HTTP API data.

Minimal complete definition

toUrlPiece | toQueryParam

Methods

toUrlPiece :: a -> Text Source

Convert to URL path piece.

toHeader :: a -> ByteString Source

Convert to HTTP header value.

toQueryParam :: a -> Text Source

Convert to query param value.

class FromHttpApiData a where Source

Parse value from HTTP API data.

Minimal complete definition

parseUrlPiece | parseQueryParam

Methods

parseUrlPiece :: Text -> Either Text a Source

Parse URL path piece.

parseHeader :: ByteString -> Either Text a Source

Parse HTTP header value.

parseQueryParam :: Text -> Either Text a Source

Parse query param value.

parseUrlPieceMaybe :: FromHttpApiData a => Text -> Maybe a Source

Parse URL path piece in a Maybe.

>>> parseUrlPieceMaybe "12" :: Maybe Int
Just 12

parseHeaderMaybe :: FromHttpApiData a => ByteString -> Maybe a Source

Parse HTTP header value in a Maybe.

>>> parseHeaderMaybe "hello" :: Maybe Text
Just "hello"

parseQueryParamMaybe :: FromHttpApiData a => Text -> Maybe a Source

Parse query param value in a Maybe.

>>> parseQueryParamMaybe "true" :: Maybe Bool
Just True

defaultParseError :: Text -> Either Text a Source

Default parsing error.

parseMaybeTextData :: (Text -> Maybe a) -> Text -> Either Text a Source

Convert Maybe parser into Either Text parser with default error message.

showTextData :: Show a => a -> Text Source

Convert to URL piece using Show instance. The result is always lower cased.

>>> showTextData True
"true"

This can be used as a default implementation for enumeration types:

>>> data MyData = Foo | Bar | Baz deriving (Show)
>>> instance ToHttpApiData MyData where toUrlPiece = showTextData
>>> toUrlPiece Foo
"foo"

showt :: Show a => a -> Text Source

Like show, but returns Text.

parseUrlPieceWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a Source

Parse given text case insensitive and return the rest of the input.

>>> parseUrlPieceWithPrefix "Just " "just 10" :: Either Text Int
Right 10
>>> parseUrlPieceWithPrefix "Left " "left" :: Either Text Bool
Left "could not parse: `left'"

This can be used to implement FromHttpApiData for single field constructors:

>>> data Foo = Foo Int deriving (Show)
>>> instance FromHttpApiData Foo where parseUrlPiece s = Foo <$> parseUrlPieceWithPrefix "Foo " s
>>> parseUrlPiece "foo 1" :: Either Text Foo
Right (Foo 1)

parseBoundedCaseInsensitiveTextData :: forall a. (Show a, Bounded a, Enum a) => Text -> Either Text a Source

Parse values case insensitively based on Show instance.

>>> parseBoundedCaseInsensitiveTextData "true" :: Either Text Bool
Right True
>>> parseBoundedCaseInsensitiveTextData "FALSE" :: Either Text Bool
Right False

This can be used as a default implementation for enumeration types:

>>> data MyData = Foo | Bar | Baz deriving (Show, Bounded, Enum)
>>> instance FromHttpApiData MyData where parseUrlPiece = parseBoundedCaseInsensitiveTextData
>>> parseUrlPiece "foo" :: Either Text MyData
Right Foo

readMaybeTextData :: Read a => Text -> Maybe a Source

Parse URL piece using Read instance.

readEitherTextData :: Read a => Text -> Either Text a Source

Parse URL piece using Read instance.

runReader :: Reader a -> Text -> Either Text a Source

Run Reader as HTTP API data parser.

parseBounded :: forall a. (Bounded a, Integral a) => Reader Integer -> Text -> Either Text a Source

Run Reader to parse bounded integral value with bounds checking.

>>> parseBounded decimal "256" :: Either Text Word8
Left "out of bounds: `256' (should be between 0 and 255)"