git.iko.soy/typeable/http-api-data
on master help sign in
Add Web.HttpApiData with To/FromHttpApiData classes
commitb3774df89da4a683010d841f87b85aba5a155755 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateSun 4 Oct 2015, 00:35 (11 years ago) parent8bfc605
2 files changed+41−1
Web/HttpApiData.hs+37 http-api-data.cabal+4−1
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:
1818library
1919 build-depends: base >= 4 && < 5
2020 , text >= 0.5
21+ , bytestring
2122 , time
22− exposed-modules: Web.PathPieces
23+ exposed-modules:
24+ Web.PathPieces
25+ Web.HttpApiData
2326 ghc-options: -Wall
2427
2528test-suite test