{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_HADDOCK not-home #-}
module Servant.API.ContentTypes
(
JSON
, PlainText
, FormUrlEncoded
, OctetStream
, Accept(..)
, MimeRender(..)
, MimeUnrender(..)
, NoContent(..)
, AcceptHeader(..)
, AllCTRender(..)
, AllCTUnrender(..)
, AllMime(..)
, AllMimeRender(..)
, AllMimeUnrender(..)
, eitherDecodeLenient
, canHandleAcceptH
) where
import Control.Arrow
(left)
import Control.DeepSeq
(NFData)
import Data.Aeson
(FromJSON (..), ToJSON (..), encode, eitherDecode)
import Data.Bifunctor
(bimap)
import qualified Data.ByteString as BS
import Data.ByteString.Lazy
(ByteString, fromStrict, toStrict)
import Data.Kind
(Type)
import qualified Data.List.NonEmpty as NE
import Data.Maybe
(isJust)
import qualified Data.Text as TextS
import qualified Data.Text.Encoding as TextS
import qualified Data.Text.Lazy as TextL
import qualified Data.Text.Lazy.Encoding as TextL
import Data.Typeable
import GHC.Generics
(Generic)
import qualified GHC.TypeLits as TL
import qualified Network.HTTP.Media as M
import Web.FormUrlEncoded
(FromForm, ToForm, urlDecodeAsForm, urlEncodeAsForm)
data JSON deriving Typeable
data PlainText deriving Typeable
data FormUrlEncoded deriving Typeable
data OctetStream deriving Typeable
class Accept ctype where
contentType :: Proxy ctype -> M.MediaType
contentType = NonEmpty MediaType -> MediaType
forall a. NonEmpty a -> a
NE.head (NonEmpty MediaType -> MediaType)
-> (Proxy ctype -> NonEmpty MediaType) -> Proxy ctype -> MediaType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy ctype -> NonEmpty MediaType
forall {k} (ctype :: k).
Accept ctype =>
Proxy ctype -> NonEmpty MediaType
contentTypes
contentTypes :: Proxy ctype -> NE.NonEmpty M.MediaType
contentTypes = (MediaType -> [MediaType] -> NonEmpty MediaType
forall a. a -> [a] -> NonEmpty a
NE.:| []) (MediaType -> NonEmpty MediaType)
-> (Proxy ctype -> MediaType) -> Proxy ctype -> NonEmpty MediaType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy ctype -> MediaType
forall {k} (ctype :: k). Accept ctype => Proxy ctype -> MediaType
contentType
{-# MINIMAL contentType | contentTypes #-}
instance Accept JSON where
contentTypes :: Proxy JSON -> NonEmpty MediaType
contentTypes Proxy JSON
_ =
ByteString
"application" ByteString -> ByteString -> MediaType
M.// ByteString
"json" MediaType -> (ByteString, ByteString) -> MediaType
M./: (ByteString
"charset", ByteString
"utf-8") MediaType -> [MediaType] -> NonEmpty MediaType
forall a. a -> [a] -> NonEmpty a
NE.:|
[ ByteString
"application" ByteString -> ByteString -> MediaType
M.// ByteString
"json" ]
instance Accept FormUrlEncoded where
contentType :: Proxy FormUrlEncoded -> MediaType
contentType Proxy FormUrlEncoded
_ = ByteString
"application" ByteString -> ByteString -> MediaType
M.// ByteString
"x-www-form-urlencoded"
instance Accept PlainText where
contentType :: Proxy PlainText -> MediaType
contentType Proxy PlainText
_ = ByteString
"text" ByteString -> ByteString -> MediaType
M.// ByteString
"plain" MediaType -> (ByteString, ByteString) -> MediaType
M./: (ByteString
"charset", ByteString
"utf-8")
instance Accept OctetStream where
contentType :: Proxy OctetStream -> MediaType
contentType Proxy OctetStream
_ = ByteString
"application" ByteString -> ByteString -> MediaType
M.// ByteString
"octet-stream"
newtype = BS.ByteString
deriving (AcceptHeader -> AcceptHeader -> Bool
(AcceptHeader -> AcceptHeader -> Bool)
-> (AcceptHeader -> AcceptHeader -> Bool) -> Eq AcceptHeader
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AcceptHeader -> AcceptHeader -> Bool
== :: AcceptHeader -> AcceptHeader -> Bool
$c/= :: AcceptHeader -> AcceptHeader -> Bool
/= :: AcceptHeader -> AcceptHeader -> Bool
Eq, Int -> AcceptHeader -> ShowS
[AcceptHeader] -> ShowS
AcceptHeader -> String
(Int -> AcceptHeader -> ShowS)
-> (AcceptHeader -> String)
-> ([AcceptHeader] -> ShowS)
-> Show AcceptHeader
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AcceptHeader -> ShowS
showsPrec :: Int -> AcceptHeader -> ShowS
$cshow :: AcceptHeader -> String
show :: AcceptHeader -> String
$cshowList :: [AcceptHeader] -> ShowS
showList :: [AcceptHeader] -> ShowS
Show, ReadPrec [AcceptHeader]
ReadPrec AcceptHeader
Int -> ReadS AcceptHeader
ReadS [AcceptHeader]
(Int -> ReadS AcceptHeader)
-> ReadS [AcceptHeader]
-> ReadPrec AcceptHeader
-> ReadPrec [AcceptHeader]
-> Read AcceptHeader
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS AcceptHeader
readsPrec :: Int -> ReadS AcceptHeader
$creadList :: ReadS [AcceptHeader]
readList :: ReadS [AcceptHeader]
$creadPrec :: ReadPrec AcceptHeader
readPrec :: ReadPrec AcceptHeader
$creadListPrec :: ReadPrec [AcceptHeader]
readListPrec :: ReadPrec [AcceptHeader]
Read, Typeable, (forall x. AcceptHeader -> Rep AcceptHeader x)
-> (forall x. Rep AcceptHeader x -> AcceptHeader)
-> Generic AcceptHeader
forall x. Rep AcceptHeader x -> AcceptHeader
forall x. AcceptHeader -> Rep AcceptHeader x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. AcceptHeader -> Rep AcceptHeader x
from :: forall x. AcceptHeader -> Rep AcceptHeader x
$cto :: forall x. Rep AcceptHeader x -> AcceptHeader
to :: forall x. Rep AcceptHeader x -> AcceptHeader
Generic)
class Accept ctype => MimeRender ctype a where
mimeRender :: Proxy ctype -> a -> ByteString
class (AllMime list) => AllCTRender (list :: [Type]) a where
handleAcceptH :: Proxy list -> AcceptHeader -> a -> Maybe (ByteString, ByteString)
instance {-# OVERLAPPABLE #-}
(Accept ct, AllMime cts, AllMimeRender (ct ': cts) a) => AllCTRender (ct ': cts) a where
handleAcceptH :: Proxy (ct : cts)
-> AcceptHeader -> a -> Maybe (ByteString, ByteString)
handleAcceptH Proxy (ct : cts)
_ (AcceptHeader ByteString
accept) a
val = [(MediaType, (ByteString, ByteString))]
-> ByteString -> Maybe (ByteString, ByteString)
forall b. [(MediaType, b)] -> ByteString -> Maybe b
M.mapAcceptMedia [(MediaType, (ByteString, ByteString))]
lkup ByteString
accept
where pctyps :: Proxy (ct : cts)
pctyps = Proxy (ct : cts)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ct ': cts)
amrs :: [(MediaType, ByteString)]
amrs = Proxy (ct : cts) -> a -> [(MediaType, ByteString)]
forall (list :: [Type]) a.
AllMimeRender list a =>
Proxy list -> a -> [(MediaType, ByteString)]
allMimeRender Proxy (ct : cts)
pctyps a
val
lkup :: [(MediaType, (ByteString, ByteString))]
lkup = ((MediaType, ByteString) -> (MediaType, (ByteString, ByteString)))
-> [(MediaType, ByteString)]
-> [(MediaType, (ByteString, ByteString))]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(MediaType
a,ByteString
b) -> (MediaType
a, (ByteString -> ByteString
fromStrict (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ MediaType -> ByteString
forall h. RenderHeader h => h -> ByteString
M.renderHeader MediaType
a, ByteString
b))) [(MediaType, ByteString)]
amrs
instance TL.TypeError ('TL.Text "No instance for (), use NoContent instead.")
=> AllCTRender '[] () where
handleAcceptH :: Proxy '[] -> AcceptHeader -> () -> Maybe (ByteString, ByteString)
handleAcceptH Proxy '[]
_ AcceptHeader
_ ()
_ = String -> Maybe (ByteString, ByteString)
forall a. HasCallStack => String -> a
error String
"unreachable"
class Accept ctype => MimeUnrender ctype a where
mimeUnrender :: Proxy ctype -> ByteString -> Either String a
mimeUnrender Proxy ctype
p = Proxy ctype -> MediaType -> ByteString -> Either String a
forall {k} (ctype :: k) a.
MimeUnrender ctype a =>
Proxy ctype -> MediaType -> ByteString -> Either String a
mimeUnrenderWithType Proxy ctype
p (Proxy ctype -> MediaType
forall {k} (ctype :: k). Accept ctype => Proxy ctype -> MediaType
contentType Proxy ctype
p)
mimeUnrenderWithType :: Proxy ctype -> M.MediaType -> ByteString -> Either String a
mimeUnrenderWithType Proxy ctype
p MediaType
_ = Proxy ctype -> ByteString -> Either String a
forall {k} (ctype :: k) a.
MimeUnrender ctype a =>
Proxy ctype -> ByteString -> Either String a
mimeUnrender Proxy ctype
p
{-# MINIMAL mimeUnrender | mimeUnrenderWithType #-}
class AllCTUnrender (list :: [Type]) a where
canHandleCTypeH
:: Proxy list
-> ByteString
-> Maybe (ByteString -> Either String a)
handleCTypeH :: Proxy list
-> ByteString
-> ByteString
-> Maybe (Either String a)
handleCTypeH Proxy list
p ByteString
ctypeH ByteString
body = ((ByteString -> Either String a) -> ByteString -> Either String a
forall a b. (a -> b) -> a -> b
$ ByteString
body) ((ByteString -> Either String a) -> Either String a)
-> Maybe (ByteString -> Either String a) -> Maybe (Either String a)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Proxy list -> ByteString -> Maybe (ByteString -> Either String a)
forall (list :: [Type]) a.
AllCTUnrender list a =>
Proxy list -> ByteString -> Maybe (ByteString -> Either String a)
canHandleCTypeH Proxy list
p ByteString
ctypeH
instance ( AllMimeUnrender ctyps a ) => AllCTUnrender ctyps a where
canHandleCTypeH :: Proxy ctyps -> ByteString -> Maybe (ByteString -> Either String a)
canHandleCTypeH Proxy ctyps
p ByteString
ctypeH =
[(MediaType, ByteString -> Either String a)]
-> ByteString -> Maybe (ByteString -> Either String a)
forall b. [(MediaType, b)] -> ByteString -> Maybe b
M.mapContentMedia (Proxy ctyps -> [(MediaType, ByteString -> Either String a)]
forall (list :: [Type]) a.
AllMimeUnrender list a =>
Proxy list -> [(MediaType, ByteString -> Either String a)]
allMimeUnrender Proxy ctyps
p) (ByteString -> ByteString
toStrict ByteString
ctypeH)
class AllMime (list :: [Type]) where
allMime :: Proxy list -> [M.MediaType]
instance AllMime '[] where
allMime :: Proxy '[] -> [MediaType]
allMime Proxy '[]
_ = []
instance (Accept ctyp, AllMime ctyps) => AllMime (ctyp ': ctyps) where
allMime :: Proxy (ctyp : ctyps) -> [MediaType]
allMime Proxy (ctyp : ctyps)
_ = NonEmpty MediaType -> [MediaType]
forall a. NonEmpty a -> [a]
NE.toList (Proxy ctyp -> NonEmpty MediaType
forall {k} (ctype :: k).
Accept ctype =>
Proxy ctype -> NonEmpty MediaType
contentTypes Proxy ctyp
pctyp) [MediaType] -> [MediaType] -> [MediaType]
forall a. [a] -> [a] -> [a]
++ Proxy ctyps -> [MediaType]
forall (list :: [Type]). AllMime list => Proxy list -> [MediaType]
allMime Proxy ctyps
pctyps
where
pctyp :: Proxy ctyp
pctyp = Proxy ctyp
forall {k} (t :: k). Proxy t
Proxy :: Proxy ctyp
pctyps :: Proxy ctyps
pctyps = Proxy ctyps
forall {k} (t :: k). Proxy t
Proxy :: Proxy ctyps
canHandleAcceptH :: AllMime list => Proxy list -> AcceptHeader -> Bool
canHandleAcceptH :: forall (list :: [Type]).
AllMime list =>
Proxy list -> AcceptHeader -> Bool
canHandleAcceptH Proxy list
p (AcceptHeader ByteString
h ) = Maybe MediaType -> Bool
forall a. Maybe a -> Bool
isJust (Maybe MediaType -> Bool) -> Maybe MediaType -> Bool
forall a b. (a -> b) -> a -> b
$ [MediaType] -> ByteString -> Maybe MediaType
forall a. Accept a => [a] -> ByteString -> Maybe a
M.matchAccept (Proxy list -> [MediaType]
forall (list :: [Type]). AllMime list => Proxy list -> [MediaType]
allMime Proxy list
p) ByteString
h
class (AllMime list) => AllMimeRender (list :: [Type]) a where
allMimeRender :: Proxy list
-> a
-> [(M.MediaType, ByteString)]
instance {-# OVERLAPPABLE #-} ( MimeRender ctyp a ) => AllMimeRender '[ctyp] a where
allMimeRender :: Proxy '[ctyp] -> a -> [(MediaType, ByteString)]
allMimeRender Proxy '[ctyp]
_ a
a = (MediaType -> (MediaType, ByteString))
-> [MediaType] -> [(MediaType, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map (, ByteString
bs) ([MediaType] -> [(MediaType, ByteString)])
-> [MediaType] -> [(MediaType, ByteString)]
forall a b. (a -> b) -> a -> b
$ NonEmpty MediaType -> [MediaType]
forall a. NonEmpty a -> [a]
NE.toList (NonEmpty MediaType -> [MediaType])
-> NonEmpty MediaType -> [MediaType]
forall a b. (a -> b) -> a -> b
$ Proxy ctyp -> NonEmpty MediaType
forall {k} (ctype :: k).
Accept ctype =>
Proxy ctype -> NonEmpty MediaType
contentTypes Proxy ctyp
pctyp
where
bs :: ByteString
bs = Proxy ctyp -> a -> ByteString
forall {k} (ctype :: k) a.
MimeRender ctype a =>
Proxy ctype -> a -> ByteString
mimeRender Proxy ctyp
pctyp a
a
pctyp :: Proxy ctyp
pctyp = Proxy ctyp
forall {k} (t :: k). Proxy t
Proxy :: Proxy ctyp
instance {-# OVERLAPPABLE #-}
( MimeRender ctyp a
, AllMimeRender (ctyp' ': ctyps) a
) => AllMimeRender (ctyp ': ctyp' ': ctyps) a where
allMimeRender :: Proxy (ctyp : ctyp' : ctyps) -> a -> [(MediaType, ByteString)]
allMimeRender Proxy (ctyp : ctyp' : ctyps)
_ a
a =
(MediaType -> (MediaType, ByteString))
-> [MediaType] -> [(MediaType, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map (, ByteString
bs) (NonEmpty MediaType -> [MediaType]
forall a. NonEmpty a -> [a]
NE.toList (NonEmpty MediaType -> [MediaType])
-> NonEmpty MediaType -> [MediaType]
forall a b. (a -> b) -> a -> b
$ Proxy ctyp -> NonEmpty MediaType
forall {k} (ctype :: k).
Accept ctype =>
Proxy ctype -> NonEmpty MediaType
contentTypes Proxy ctyp
pctyp)
[(MediaType, ByteString)]
-> [(MediaType, ByteString)] -> [(MediaType, ByteString)]
forall a. [a] -> [a] -> [a]
++ Proxy (ctyp' : ctyps) -> a -> [(MediaType, ByteString)]
forall (list :: [Type]) a.
AllMimeRender list a =>
Proxy list -> a -> [(MediaType, ByteString)]
allMimeRender Proxy (ctyp' : ctyps)
pctyps a
a
where
bs :: ByteString
bs = Proxy ctyp -> a -> ByteString
forall {k} (ctype :: k) a.
MimeRender ctype a =>
Proxy ctype -> a -> ByteString
mimeRender Proxy ctyp
pctyp a
a
pctyp :: Proxy ctyp
pctyp = Proxy ctyp
forall {k} (t :: k). Proxy t
Proxy :: Proxy ctyp
pctyps :: Proxy (ctyp' : ctyps)
pctyps = Proxy (ctyp' : ctyps)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ctyp' ': ctyps)
instance {-# OVERLAPPING #-} ( Accept ctyp ) => AllMimeRender '[ctyp] NoContent where
allMimeRender :: Proxy '[ctyp] -> NoContent -> [(MediaType, ByteString)]
allMimeRender Proxy '[ctyp]
_ NoContent
NoContent = (MediaType -> (MediaType, ByteString))
-> [MediaType] -> [(MediaType, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map (, ByteString
"") ([MediaType] -> [(MediaType, ByteString)])
-> [MediaType] -> [(MediaType, ByteString)]
forall a b. (a -> b) -> a -> b
$ NonEmpty MediaType -> [MediaType]
forall a. NonEmpty a -> [a]
NE.toList (NonEmpty MediaType -> [MediaType])
-> NonEmpty MediaType -> [MediaType]
forall a b. (a -> b) -> a -> b
$ Proxy ctyp -> NonEmpty MediaType
forall {k} (ctype :: k).
Accept ctype =>
Proxy ctype -> NonEmpty MediaType
contentTypes Proxy ctyp
pctyp
where
pctyp :: Proxy ctyp
pctyp = Proxy ctyp
forall {k} (t :: k). Proxy t
Proxy :: Proxy ctyp
instance {-# OVERLAPPING #-}
( AllMime (ctyp ': ctyp' ': ctyps)
) => AllMimeRender (ctyp ': ctyp' ': ctyps) NoContent where
allMimeRender :: Proxy (ctyp : ctyp' : ctyps)
-> NoContent -> [(MediaType, ByteString)]
allMimeRender Proxy (ctyp : ctyp' : ctyps)
p NoContent
_ = (MediaType -> (MediaType, ByteString))
-> [MediaType] -> [(MediaType, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map (, ByteString
"") (Proxy (ctyp : ctyp' : ctyps) -> [MediaType]
forall (list :: [Type]). AllMime list => Proxy list -> [MediaType]
allMime Proxy (ctyp : ctyp' : ctyps)
p)
class (AllMime list) => AllMimeUnrender (list :: [Type]) a where
allMimeUnrender :: Proxy list
-> [(M.MediaType, ByteString -> Either String a)]
instance AllMimeUnrender '[] a where
allMimeUnrender :: Proxy '[] -> [(MediaType, ByteString -> Either String a)]
allMimeUnrender Proxy '[]
_ = []
instance ( MimeUnrender ctyp a
, AllMimeUnrender ctyps a
) => AllMimeUnrender (ctyp ': ctyps) a where
allMimeUnrender :: Proxy (ctyp : ctyps)
-> [(MediaType, ByteString -> Either String a)]
allMimeUnrender Proxy (ctyp : ctyps)
_ =
(MediaType -> (MediaType, ByteString -> Either String a))
-> [MediaType] -> [(MediaType, ByteString -> Either String a)]
forall a b. (a -> b) -> [a] -> [b]
map MediaType -> (MediaType, ByteString -> Either String a)
mk (NonEmpty MediaType -> [MediaType]
forall a. NonEmpty a -> [a]
NE.toList (NonEmpty MediaType -> [MediaType])
-> NonEmpty MediaType -> [MediaType]
forall a b. (a -> b) -> a -> b
$ Proxy ctyp -> NonEmpty MediaType
forall {k} (ctype :: k).
Accept ctype =>
Proxy ctype -> NonEmpty MediaType
contentTypes Proxy ctyp
pctyp)
[(MediaType, ByteString -> Either String a)]
-> [(MediaType, ByteString -> Either String a)]
-> [(MediaType, ByteString -> Either String a)]
forall a. [a] -> [a] -> [a]
++ Proxy ctyps -> [(MediaType, ByteString -> Either String a)]
forall (list :: [Type]) a.
AllMimeUnrender list a =>
Proxy list -> [(MediaType, ByteString -> Either String a)]
allMimeUnrender Proxy ctyps
pctyps
where
mk :: MediaType -> (MediaType, ByteString -> Either String a)
mk MediaType
ct = (MediaType
ct, Proxy ctyp -> MediaType -> ByteString -> Either String a
forall {k} (ctype :: k) a.
MimeUnrender ctype a =>
Proxy ctype -> MediaType -> ByteString -> Either String a
mimeUnrenderWithType Proxy ctyp
pctyp MediaType
ct)
pctyp :: Proxy ctyp
pctyp = Proxy ctyp
forall {k} (t :: k). Proxy t
Proxy :: Proxy ctyp
pctyps :: Proxy ctyps
pctyps = Proxy ctyps
forall {k} (t :: k). Proxy t
Proxy :: Proxy ctyps
instance {-# OVERLAPPABLE #-}
ToJSON a => MimeRender JSON a where
mimeRender :: Proxy JSON -> a -> ByteString
mimeRender Proxy JSON
_ = a -> ByteString
forall a. ToJSON a => a -> ByteString
encode
instance {-# OVERLAPPABLE #-}
ToForm a => MimeRender FormUrlEncoded a where
mimeRender :: Proxy FormUrlEncoded -> a -> ByteString
mimeRender Proxy FormUrlEncoded
_ = a -> ByteString
forall a. ToForm a => a -> ByteString
urlEncodeAsForm
instance MimeRender PlainText TextL.Text where
mimeRender :: Proxy PlainText -> Text -> ByteString
mimeRender Proxy PlainText
_ = Text -> ByteString
TextL.encodeUtf8
instance MimeRender PlainText TextS.Text where
mimeRender :: Proxy PlainText -> Text -> ByteString
mimeRender Proxy PlainText
_ = ByteString -> ByteString
fromStrict (ByteString -> ByteString)
-> (Text -> ByteString) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TextS.encodeUtf8
instance MimeRender PlainText String where
mimeRender :: Proxy PlainText -> String -> ByteString
mimeRender Proxy PlainText
_ = Text -> ByteString
TextL.encodeUtf8 (Text -> ByteString) -> (String -> Text) -> String -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
TextL.pack
instance MimeRender OctetStream ByteString where
mimeRender :: Proxy OctetStream -> ByteString -> ByteString
mimeRender Proxy OctetStream
_ = ByteString -> ByteString
forall a. a -> a
id
instance MimeRender OctetStream BS.ByteString where
mimeRender :: Proxy OctetStream -> ByteString -> ByteString
mimeRender Proxy OctetStream
_ = ByteString -> ByteString
fromStrict
data NoContent = NoContent
deriving (Int -> NoContent -> ShowS
[NoContent] -> ShowS
NoContent -> String
(Int -> NoContent -> ShowS)
-> (NoContent -> String)
-> ([NoContent] -> ShowS)
-> Show NoContent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NoContent -> ShowS
showsPrec :: Int -> NoContent -> ShowS
$cshow :: NoContent -> String
show :: NoContent -> String
$cshowList :: [NoContent] -> ShowS
showList :: [NoContent] -> ShowS
Show, NoContent -> NoContent -> Bool
(NoContent -> NoContent -> Bool)
-> (NoContent -> NoContent -> Bool) -> Eq NoContent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NoContent -> NoContent -> Bool
== :: NoContent -> NoContent -> Bool
$c/= :: NoContent -> NoContent -> Bool
/= :: NoContent -> NoContent -> Bool
Eq, ReadPrec [NoContent]
ReadPrec NoContent
Int -> ReadS NoContent
ReadS [NoContent]
(Int -> ReadS NoContent)
-> ReadS [NoContent]
-> ReadPrec NoContent
-> ReadPrec [NoContent]
-> Read NoContent
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS NoContent
readsPrec :: Int -> ReadS NoContent
$creadList :: ReadS [NoContent]
readList :: ReadS [NoContent]
$creadPrec :: ReadPrec NoContent
readPrec :: ReadPrec NoContent
$creadListPrec :: ReadPrec [NoContent]
readListPrec :: ReadPrec [NoContent]
Read, (forall x. NoContent -> Rep NoContent x)
-> (forall x. Rep NoContent x -> NoContent) -> Generic NoContent
forall x. Rep NoContent x -> NoContent
forall x. NoContent -> Rep NoContent x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. NoContent -> Rep NoContent x
from :: forall x. NoContent -> Rep NoContent x
$cto :: forall x. Rep NoContent x -> NoContent
to :: forall x. Rep NoContent x -> NoContent
Generic)
instance NFData NoContent
eitherDecodeLenient :: FromJSON a => ByteString -> Either String a
eitherDecodeLenient :: forall a. FromJSON a => ByteString -> Either String a
eitherDecodeLenient = ByteString -> Either String a
forall a. FromJSON a => ByteString -> Either String a
eitherDecode
{-# DEPRECATED eitherDecodeLenient "use eitherDecode instead" #-}
instance FromJSON a => MimeUnrender JSON a where
mimeUnrender :: Proxy JSON -> ByteString -> Either String a
mimeUnrender Proxy JSON
_ = ByteString -> Either String a
forall a. FromJSON a => ByteString -> Either String a
eitherDecode
instance FromForm a => MimeUnrender FormUrlEncoded a where
mimeUnrender :: Proxy FormUrlEncoded -> ByteString -> Either String a
mimeUnrender Proxy FormUrlEncoded
_ = (Text -> String) -> Either Text a -> Either String a
forall b c d. (b -> c) -> Either b d -> Either c d
forall (a :: Type -> Type -> Type) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left Text -> String
TextS.unpack (Either Text a -> Either String a)
-> (ByteString -> Either Text a) -> ByteString -> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text a
forall a. FromForm a => ByteString -> Either Text a
urlDecodeAsForm
instance MimeUnrender PlainText TextL.Text where
mimeUnrender :: Proxy PlainText -> ByteString -> Either String Text
mimeUnrender Proxy PlainText
_ = (UnicodeException -> String)
-> Either UnicodeException Text -> Either String Text
forall b c d. (b -> c) -> Either b d -> Either c d
forall (a :: Type -> Type -> Type) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left UnicodeException -> String
forall a. Show a => a -> String
show (Either UnicodeException Text -> Either String Text)
-> (ByteString -> Either UnicodeException Text)
-> ByteString
-> Either String Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
TextL.decodeUtf8'
instance MimeUnrender PlainText TextS.Text where
mimeUnrender :: Proxy PlainText -> ByteString -> Either String Text
mimeUnrender Proxy PlainText
_ = (UnicodeException -> String)
-> Either UnicodeException Text -> Either String Text
forall b c d. (b -> c) -> Either b d -> Either c d
forall (a :: Type -> Type -> Type) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left UnicodeException -> String
forall a. Show a => a -> String
show (Either UnicodeException Text -> Either String Text)
-> (ByteString -> Either UnicodeException Text)
-> ByteString
-> Either String Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
TextS.decodeUtf8' (ByteString -> Either UnicodeException Text)
-> (ByteString -> ByteString)
-> ByteString
-> Either UnicodeException Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
toStrict
instance MimeUnrender PlainText String where
mimeUnrender :: Proxy PlainText -> ByteString -> Either String String
mimeUnrender Proxy PlainText
_ = (UnicodeException -> String)
-> (Text -> String)
-> Either UnicodeException Text
-> Either String String
forall a b c d. (a -> b) -> (c -> d) -> Either a c -> Either b d
forall (p :: Type -> Type -> Type) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap UnicodeException -> String
forall a. Show a => a -> String
show Text -> String
TextL.unpack (Either UnicodeException Text -> Either String String)
-> (ByteString -> Either UnicodeException Text)
-> ByteString
-> Either String String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
TextL.decodeUtf8'
instance MimeUnrender OctetStream ByteString where
mimeUnrender :: Proxy OctetStream -> ByteString -> Either String ByteString
mimeUnrender Proxy OctetStream
_ = ByteString -> Either String ByteString
forall a b. b -> Either a b
Right
instance MimeUnrender OctetStream BS.ByteString where
mimeUnrender :: Proxy OctetStream -> ByteString -> Either String ByteString
mimeUnrender Proxy OctetStream
_ = ByteString -> Either String ByteString
forall a b. b -> Either a b
Right (ByteString -> Either String ByteString)
-> (ByteString -> ByteString)
-> ByteString
-> Either String ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
toStrict