| Safe Haskell | Safe |
|---|---|
| Language | Haskell98 |
Web.HttpApiData
Description
Convert Haskell values to and from HTTP API data such as URL pieces, headers and query parameters.
- class ToHttpApiData a where
- toUrlPiece :: a -> Text
- toHeader :: a -> ByteString
- toQueryParam :: a -> Text
- class FromHttpApiData a where
- parseUrlPiece :: Text -> Either Text a
- parseHeader :: ByteString -> Either Text a
- parseQueryParam :: Text -> Either Text a
Examples
Booleans:
>>>toUrlPiece True"true">>>parseUrlPiece "false" :: Either Text BoolRight False>>>parseUrlPiece "something else" :: Either Text BoolLeft "could not parse: `something else'"
Numbers:
>>>toUrlPiece 45.2"45.2">>>parseUrlPiece "452" :: Either Text IntRight 452>>>parseUrlPiece "256" :: Either Text Int8Left "out of bounds: `256' (should be between -128 and 127)"
Strings:
>>>toHeader "hello""hello">>>parseHeader "world" :: Either Text StringRight "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.
Minimal complete definition
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.
Instances
class FromHttpApiData a where Source
Parse value from HTTP API data.
Minimal complete definition
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.
Instances