git.iko.soy/typeable/http-api-data
on b3774df help sign in
typeable/http-api-data/Web/HttpApiData.hs
37 lines 984 B history raw
1{-# LANGUAGE DefaultSignatures #-}
2module Web.HttpApiData (
3 ToHttpApiData (..),
4 FromHttpApiData (..),
5) where
7import Data.ByteString (ByteString)
8import Data.Text (Text)
9import Data.Text.Encoding (encodeUtf8, decodeUtf8)
11-- | Convert value to HTTP API data.
12class ToHttpApiData a where
13 -- | Convert to URL path piece.
14 toUrlPiece :: a -> Text
15 toUrlPiece = toQueryParam
17 -- | Convert to HTTP header value.
18 toHeader :: a -> ByteString
19 toHeader = encodeUtf8 . toUrlPiece
21 -- | Convert to query param value.
22 toQueryParam :: a -> Text
23 toQueryParam = toUrlPiece
25-- | Convert value from HTTP API data.
26class FromHttpApiData a where
27 -- | Parse URL path piece.
28 parseUrlPiece :: Text -> Either Text a
29 parseUrlPiece = parseQueryParam
31 -- | Parse HTTP header value.
32 parseHeader :: ByteString -> Either Text a
33 parseHeader = parseUrlPiece . decodeUtf8
35 -- | Parse query param value.
36 parseQueryParam :: Text -> Either Text a
37 parseQueryParam = parseUrlPiece