git.iko.soy/typeable/http-api-data
on master help sign in
Add parse*WithPrefix helpers
5863731Nickolay Kudasov
Web/HttpApiData.hs+6−2
@@ -14,11 +14,15 @@ module Web.HttpApiData (
14 parseHeaderMaybe,
14 parseHeaderMaybe,
15 parseQueryParamMaybe,
15 parseQueryParamMaybe,
16
16
17− -- * Helpers
17+ -- * Prefix parsers
18+ parseUrlPieceWithPrefix,
19+ parseHeaderWithPrefix,
20+ parseQueryParamWithPrefix,
21+
22+ -- * Other helpers
18 showTextData,
23 showTextData,
19 readTextData,
24 readTextData,
20 parseBoundedTextData,
25 parseBoundedTextData,
21− parseUrlPieceWithPrefix,
22) where
26) where
23
27
24import Web.HttpApiData.Internal
28import Web.HttpApiData.Internal
Web/HttpApiData/Internal.hs+33−1
@@ -16,6 +16,7 @@ import Control.Arrow ((&&&))
16
16
17import Data.Monoid
17import Data.Monoid
18import Data.ByteString (ByteString)
18import Data.ByteString (ByteString)
19+import qualified Data.ByteString as BS
19
20
20import Data.Int
21import Data.Int
21import Data.Word
22import Data.Word
@@ -149,7 +150,8 @@ showt = T.pack . show
149
150
150-- | /Case insensitive/.
151-- | /Case insensitive/.
151--
152--
152−-- Parse given text case insensitive and return the rest of the input.
153+-- Parse given text case insensitive and then parse the rest of the input
154+-- using @'parseUrlPiece'@.
153--
155--
154-- >>> parseUrlPieceWithPrefix "Just " "just 10" :: Either Text Int
156-- >>> parseUrlPieceWithPrefix "Just " "just 10" :: Either Text Int
155-- Right 10
157-- Right 10
@@ -169,6 +171,36 @@ parseUrlPieceWithPrefix pattern input
169 where
171 where
170 (prefix, rest) = T.splitAt (T.length pattern) input
172 (prefix, rest) = T.splitAt (T.length pattern) input
171
173
174+-- | Parse given bytestring then parse the rest of the input using @'parseHeader'@.
175+--
176+-- >>> :{
177+-- newtype BasicAuthToken = BasicAuthToken Text deriving (Show)
178+-- instance FromHttpApiData BasicAuthToken where
179+-- parseHeader h = BasicAuthToken <$> parseHeaderWithPrefix "Basic " h
180+-- parseQueryParam p = BasicAuthToken <$> parseQueryParam p
181+-- :}
182+--
183+-- >>> parseHeader "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" :: Either Text BasicAuthToken
184+-- Right (BasicAuthToken "QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
185+parseHeaderWithPrefix :: FromHttpApiData a => ByteString -> ByteString -> Either Text a
186+parseHeaderWithPrefix pattern input
187+ | pattern `BS.isPrefixOf` input = parseHeader (BS.drop (BS.length pattern) input)
188+ | otherwise = defaultParseError (showt input)
189+
190+-- | /Case insensitive/.
191+--
192+-- Parse given text case insensitive and then parse the rest of the input
193+-- using @'parseQueryParam'@.
194+--
195+-- >>> parseQueryParamWithPrefix "z" "z10" :: Either Text Int
196+-- Right 10
197+parseQueryParamWithPrefix :: FromHttpApiData a => Text -> Text -> Either Text a
198+parseQueryParamWithPrefix pattern input
199+ | T.toLower pattern == T.toLower prefix = parseQueryParam rest
200+ | otherwise = defaultParseError input
201+ where
202+ (prefix, rest) = T.splitAt (T.length pattern) input
203+
172#if USE_TEXT_SHOW
204#if USE_TEXT_SHOW
173-- | /Case insensitive/.
205-- | /Case insensitive/.
174--
206--