git.iko.soy/typeable/http-api-data
on master help sign in
Add Web.HttpApiData with To/FromHttpApiData classes
b3774dfNickolay Kudasov
Web/HttpApiData.hsnew file+37
@@ -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
http-api-data.cabal+4−1
@@ -18,8 +18,11 @@ extra-source-files:
18library
18library
19 build-depends: base >= 4 && < 5
19 build-depends: base >= 4 && < 5
20 , text >= 0.5
20 , text >= 0.5
21+ , bytestring
21 , time
22 , time
22− exposed-modules: Web.PathPieces
23+ exposed-modules:
24+ Web.PathPieces
25+ Web.HttpApiData
23 ghc-options: -Wall
26 ghc-options: -Wall
24
27
25test-suite test
28test-suite test