Created
February 18, 2015 00:31
-
-
Save tvh/5e24f4818eb519127f0f to your computer and use it in GitHub Desktop.
application/x-www-form-urlencoded for servant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data FormUrlEncoded deriving Typeable | |
class ToFormUrlEncoded a where | |
toFormUrlEncoded :: a -> [(ByteString, ByteString)] | |
class FromFormUrlEncoded a where | |
fromFormUrlEncoded :: [(ByteString, ByteString)] -> Either String a | |
instance ToFormUrlEncoded [(ByteString, ByteString)] where | |
toFormUrlEncoded = id | |
instance FromFormUrlEncoded [(ByteString, ByteString)] where | |
fromFormUrlEncoded = return | |
-- | @application/x-www-form-urlencoded@ | |
instance Accept FormUrlEncoded where | |
contentType _ = "application" M.// "x-www-form-urlencoded" | |
instance ToFormUrlEncoded a => MimeRender FormUrlEncoded a where | |
toByteString _ = Text.encodeUtf8 . Text.pack | |
. exportParams | |
. map (Text.unpack . Text.decodeUtf8 *** Text.unpack . Text.decodeUtf8) | |
. toFormUrlEncoded | |
instance FromFormUrlEncoded a => MimeUnrender FormUrlEncoded a where | |
fromByteString _ x = do | |
x' <- either (const Nothing) (Just . Text.unpack) . Text.decodeUtf8' $ x | |
x'' <- importParams x' | |
either (const Nothing) Just . fromFormUrlEncoded . map (Text.encodeUtf8 . Text.pack *** Text.encodeUtf8 . Text.pack) $ x'' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment