@@ -0,0 +1,37 @@
1+{-# LANGUAGE DefaultSignatures #-}
2+module Web.HttpApiData (
3+ ToHttpApiData (..),
4+ FromHttpApiData (..),
5+) where
6+
7+import Data.ByteString (ByteString)
8+import Data.Text (Text)
9+import Data.Text.Encoding (encodeUtf8, decodeUtf8)
10+
11+-- | Convert value to HTTP API data.
12+class ToHttpApiData a where
13+ -- | Convert to URL path piece.
14+ toUrlPiece :: a -> Text
15+ toUrlPiece = toQueryParam
16+
17+ -- | Convert to HTTP header value.
18+ toHeader :: a -> ByteString
19+ toHeader = encodeUtf8 . toUrlPiece
20+
21+ -- | Convert to query param value.
22+ toQueryParam :: a -> Text
23+ toQueryParam = toUrlPiece
24+
25+-- | Convert value from HTTP API data.
26+class FromHttpApiData a where
27+ -- | Parse URL path piece.
28+ parseUrlPiece :: Text -> Either Text a
29+ parseUrlPiece = parseQueryParam
30+
31+ -- | Parse HTTP header value.
32+ parseHeader :: ByteString -> Either Text a
33+ parseHeader = parseUrlPiece . decodeUtf8
34+
35+ -- | Parse query param value.
36+ parseQueryParam :: Text -> Either Text a
37+ parseQueryParam = parseUrlPiece