{-# language DataKinds        #-}
{-# language TypeApplications #-}
{-# language TypeFamilies     #-}
{-|
Description : Examples for gRPC clients

Look at the source code of this module.
-}
module Mu.GRpc.Client.Examples where

import           Data.Conduit
import           Data.Conduit.Combinators as C
import           Data.Conduit.List        (consume)
import qualified Data.Text                as T
import           Network.HTTP2.Client     (HostName, PortNumber)

import           Mu.Adapter.ProtoBuf
import           Mu.GRpc.Client.TyApps
import           Mu.Rpc.Examples
import           Mu.Schema

type instance AnnotatedSchema ProtoBufAnnotation QuickstartSchema
  = '[ 'AnnField "HelloRequest" "name" ('ProtoBufId 1)
     , 'AnnField "HelloResponse" "message" ('ProtoBufId 1)
     , 'AnnField "HiRequest" "number" ('ProtoBufId 1) ]

type M a = a Maybe

sayHello' :: HostName -> PortNumber -> T.Text -> IO (GRpcReply (Maybe T.Text))
sayHello' :: HostName -> PortNumber -> Text -> IO (GRpcReply (Maybe Text))
sayHello' host :: HostName
host port :: PortNumber
port req :: Text
req
  = do Right c :: GrpcClient
c <- GrpcClientConfig -> IO (Either ClientError GrpcClient)
setupGrpcClient' (HostName -> PortNumber -> UseTlsOrNot -> GrpcClientConfig
grpcClientConfigSimple HostName
host PortNumber
port UseTlsOrNot
False)
       (HelloResponse Maybe -> Maybe Text)
-> GRpcReply (HelloResponse Maybe) -> GRpcReply (Maybe Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(HelloResponse r :: Maybe Text
r) -> Maybe Text
r) (GRpcReply (HelloResponse Maybe) -> GRpcReply (Maybe Text))
-> IO (GRpcReply (HelloResponse Maybe))
-> IO (GRpcReply (Maybe Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GrpcClient
-> M HelloRequest -> IO (GRpcReply (HelloResponse Maybe))
sayHello GrpcClient
c (Maybe Text -> M HelloRequest
forall (f :: * -> *). f Text -> HelloRequest f
HelloRequest (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
req))

sayHello :: GrpcClient -> M HelloRequest -> IO (GRpcReply (M HelloResponse))
sayHello :: GrpcClient
-> M HelloRequest -> IO (GRpcReply (HelloResponse Maybe))
sayHello = forall h.
GRpcServiceMethodCall
  'MsgProtoBuf
  QuickStartService
  (QuickStartService :-->: "SayHello")
  h =>
GrpcClient -> h
forall snm mnm (pro :: GRpcMessageProtocol) (s :: Service snm mnm)
       (methodName :: mnm) h.
GRpcServiceMethodCall pro s (s :-->: methodName) h =>
GrpcClient -> h
gRpcCall @'MsgProtoBuf @QuickStartService @"SayHello"

sayHi' :: HostName -> PortNumber -> Int -> IO [GRpcReply (Maybe T.Text)]
sayHi' :: HostName -> PortNumber -> Int -> IO [GRpcReply (Maybe Text)]
sayHi' host :: HostName
host port :: PortNumber
port n :: Int
n
  = do Right c :: GrpcClient
c <- GrpcClientConfig -> IO (Either ClientError GrpcClient)
setupGrpcClient' (HostName -> PortNumber -> UseTlsOrNot -> GrpcClientConfig
grpcClientConfigSimple HostName
host PortNumber
port UseTlsOrNot
False)
       ConduitT () (GRpcReply (HelloResponse Maybe)) IO ()
cndt <- GrpcClient
-> M HiRequest
-> IO (ConduitT () (GRpcReply (HelloResponse Maybe)) IO ())
sayHi GrpcClient
c (Maybe Int -> M HiRequest
forall (f :: * -> *). f Int -> HiRequest f
HiRequest (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n))
       ConduitT () Void IO [GRpcReply (Maybe Text)]
-> IO [GRpcReply (Maybe Text)]
forall (m :: * -> *) r. Monad m => ConduitT () Void m r -> m r
runConduit (ConduitT () Void IO [GRpcReply (Maybe Text)]
 -> IO [GRpcReply (Maybe Text)])
-> ConduitT () Void IO [GRpcReply (Maybe Text)]
-> IO [GRpcReply (Maybe Text)]
forall a b. (a -> b) -> a -> b
$ ConduitT () (GRpcReply (HelloResponse Maybe)) IO ()
cndt ConduitT () (GRpcReply (HelloResponse Maybe)) IO ()
-> ConduitM
     (GRpcReply (HelloResponse Maybe)) Void IO [GRpcReply (Maybe Text)]
-> ConduitT () Void IO [GRpcReply (Maybe Text)]
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| (GRpcReply (HelloResponse Maybe) -> GRpcReply (Maybe Text))
-> ConduitT
     (GRpcReply (HelloResponse Maybe)) (GRpcReply (Maybe Text)) IO ()
forall (m :: * -> *) a b. Monad m => (a -> b) -> ConduitT a b m ()
C.map ((HelloResponse Maybe -> Maybe Text)
-> GRpcReply (HelloResponse Maybe) -> GRpcReply (Maybe Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(HelloResponse r :: Maybe Text
r) -> Maybe Text
r)) ConduitT
  (GRpcReply (HelloResponse Maybe)) (GRpcReply (Maybe Text)) IO ()
-> ConduitM
     (GRpcReply (Maybe Text)) Void IO [GRpcReply (Maybe Text)]
-> ConduitM
     (GRpcReply (HelloResponse Maybe)) Void IO [GRpcReply (Maybe Text)]
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
.| ConduitM (GRpcReply (Maybe Text)) Void IO [GRpcReply (Maybe Text)]
forall (m :: * -> *) a o. Monad m => ConduitT a o m [a]
consume

sayHi :: GrpcClient -> M HiRequest -> IO (ConduitT () (GRpcReply (M HelloResponse)) IO ())
sayHi :: GrpcClient
-> M HiRequest
-> IO (ConduitT () (GRpcReply (HelloResponse Maybe)) IO ())
sayHi = forall h.
GRpcServiceMethodCall
  'MsgProtoBuf
  QuickStartService
  (QuickStartService :-->: "SayHi")
  h =>
GrpcClient -> h
forall snm mnm (pro :: GRpcMessageProtocol) (s :: Service snm mnm)
       (methodName :: mnm) h.
GRpcServiceMethodCall pro s (s :-->: methodName) h =>
GrpcClient -> h
gRpcCall @'MsgProtoBuf @QuickStartService @"SayHi"