| Safe Haskell | Safe |
|---|---|
| Language | Haskell98 |
Web.HttpApiData.Internal
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
- parseUrlPieceMaybe :: FromHttpApiData a => Text -> Maybe a
- parseHeaderMaybe :: FromHttpApiData a => ByteString -> Maybe a
- parseQueryParamMaybe :: FromHttpApiData a => Text -> Maybe a
- defaultParseError :: Text -> Either Text a
- parseMaybeTextData :: (Text -> Maybe a) -> Text -> Either Text a
- showTextData :: Show a => a -> Text
- showt :: Show a => a -> Text
- parseUrlPieceWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a
- parseHeaderWithPrefix :: FromHttpApiData a => ByteString -> ByteString -> Either Text a
- parseQueryParamWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a
- parseBoundedTextData :: (Show a, Bounded a, Enum a) => Text -> Either Text a
- readTextData :: Read a => Text -> Either Text a
- runReader :: Reader a -> Text -> Either Text a
- parseBounded :: forall a. (Bounded a, Integral a) => Reader Integer -> Text -> Either Text a
Documentation
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
parseUrlPieceMaybe :: FromHttpApiData a => Text -> Maybe a Source
Parse URL path piece in a .Maybe
>>>parseUrlPieceMaybe "12" :: Maybe IntJust 12
parseHeaderMaybe :: FromHttpApiData a => ByteString -> Maybe a Source
Parse HTTP header value in a .Maybe
>>>parseHeaderMaybe "hello" :: Maybe TextJust "hello"
parseQueryParamMaybe :: FromHttpApiData a => Text -> Maybe a Source
Parse query param value in a .Maybe
>>>parseQueryParamMaybe "true" :: Maybe BoolJust True
defaultParseError :: Text -> Either Text a Source
Default parsing error.
showTextData :: Show a => a -> Text Source
Lower case.
Convert to URL piece using instance.
The result is always lower cased.Show
>>>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"
parseUrlPieceWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a Source
Case insensitive.
Parse given text case insensitive and then parse the rest of the input
using .parseUrlPiece
>>>parseUrlPieceWithPrefix "Just " "just 10" :: Either Text IntRight 10>>>parseUrlPieceWithPrefix "Left " "left" :: Either Text BoolLeft "could not parse: `left'"
This can be used to implement for single field constructors:FromHttpApiData
>>>data Foo = Foo Int deriving (Show)>>>instance FromHttpApiData Foo where parseUrlPiece s = Foo <$> parseUrlPieceWithPrefix "Foo " s>>>parseUrlPiece "foo 1" :: Either Text FooRight (Foo 1)
>>>data BasicAuthToken = BasicAuthToken Text deriving (Show)>>>instance FromHttpApiData BasicAuthToken where parseHeader h = BasicAuthToken <$> parseHeaderWithPrefix "Basic " h; parseQueryParam p = BasicAuthToken <$> parseQueryParam p
parseHeaderWithPrefix :: FromHttpApiData a => ByteString -> ByteString -> Either Text a Source
Parse given bytestring then parse the rest of the input using .parseHeader
data BasicAuthToken = BasicAuthToken Text deriving (Show) instance FromHttpApiData BasicAuthToken where parseHeader h = BasicAuthToken <$> parseHeaderWithPrefix "Basic " h parseQueryParam p = BasicAuthToken <$> parseQueryParam p
>>>parseHeader "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" :: Either Text BasicAuthTokenRight (BasicAuthToken "QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
parseQueryParamWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a Source
Case insensitive.
Parse given text case insensitive and then parse the rest of the input
using .parseQueryParam
>>>parseQueryParamWithPrefix "z" "z10" :: Either Text IntRight 10
parseBoundedTextData :: (Show a, Bounded a, Enum a) => Text -> Either Text a Source
Case insensitive.
Parse values case insensitively based on instance.Show
>>>parseBoundedTextData "true" :: Either Text BoolRight True>>>parseBoundedTextData "FALSE" :: Either Text BoolRight 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 = parseBoundedTextData>>>parseUrlPiece "foo" :: Either Text MyDataRight Foo
readTextData :: Read a => Text -> Either Text a Source
Parse URL piece using instance.Read
Use for types which do not involve letters:
>>>readTextData "1991-06-02" :: Either Text DayRight 1991-06-02
This parser is case sensitive and will not match
in presense of letters:showTextData
>>>readTextData (showTextData True) :: Either Text BoolLeft "could not parse: `true'"
See .parseBoundedTextData