module Wire.API.ServantProto where
import Data.ByteString.Lazy qualified as LBS
import Data.List.NonEmpty (NonEmpty (..))
import Data.OpenApi
import Imports
import Network.HTTP.Media ((//))
import Servant
data Proto
class FromProto a where
fromProto :: ByteString -> Either String a
class ToProto a where
toProto :: a -> ByteString
instance Accept Proto where
contentTypes :: Proxy Proto -> NonEmpty MediaType
contentTypes Proxy Proto
_ = (ByteString
"application" ByteString -> ByteString -> MediaType
// ByteString
"x-protobuf") MediaType -> [MediaType] -> NonEmpty MediaType
forall a. a -> [a] -> NonEmpty a
:| []
instance (FromProto a) => MimeUnrender Proto a where
mimeUnrender :: Proxy Proto -> ByteString -> Either String a
mimeUnrender Proxy Proto
_ ByteString
bs = ByteString -> Either String a
forall a. FromProto a => ByteString -> Either String a
fromProto (ByteString -> ByteString
LBS.toStrict ByteString
bs)
data RawProto a = RawProto
{ forall a. RawProto a -> ByteString
rpRaw :: ByteString,
forall a. RawProto a -> a
rpValue :: a
}
instance (FromProto a) => FromProto (RawProto a) where
fromProto :: ByteString -> Either String (RawProto a)
fromProto ByteString
x = (a -> RawProto a) -> Either String a -> Either String (RawProto a)
forall a b. (a -> b) -> Either String a -> Either String b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ByteString -> a -> RawProto a
forall a. ByteString -> a -> RawProto a
RawProto ByteString
x) (ByteString -> Either String a
forall a. FromProto a => ByteString -> Either String a
fromProto ByteString
x)
instance (ToSchema a) => ToSchema (RawProto a) where
declareNamedSchema :: Proxy (RawProto a) -> Declare (Definitions Schema) NamedSchema
declareNamedSchema Proxy (RawProto a)
_ = Proxy a -> Declare (Definitions Schema) NamedSchema
forall a.
ToSchema a =>
Proxy a -> Declare (Definitions Schema) NamedSchema
declareNamedSchema (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)