17
17
package freestyle .rpc .demo
18
18
package greeting
19
19
20
- import freestyle .rpc .demo .echo .EchoServiceGrpc
21
- import freestyle .rpc .demo .echo .EchoServiceGrpc .EchoServiceStub
22
- import freestyle .rpc .demo .echo_messages .EchoRequest
23
- import io .grpc .ManagedChannelBuilder
24
-
25
- import scala .concurrent .Await
20
+ import cats .implicits ._
21
+ import freestyle ._
22
+ import freestyle .implicits ._
23
+ import freestyle .rpc .demo .echo_messages ._
24
+ import runtime .client .implicits ._
25
+ import greeting .client ._
26
+ import io .grpc ._
27
+
28
+ import scala .concurrent .{Await , Future }
26
29
import scala .concurrent .duration .Duration
27
30
31
+ @ module
32
+ trait ClientAPP {
33
+ val greetingClientM : GreetingClientM
34
+ val echoClientM : EchoClientM
35
+ }
36
+
28
37
object GreetingClientApp {
29
38
30
- def main (args : Array [String ]): Unit = {
39
+ type UnaryDemoResponse = (MessageReply , MessageReply , EchoResponse )
40
+
41
+ val messageRequest = MessageRequest (" Freestyle" )
42
+ val echoRequest = EchoRequest (" echo..." )
43
+
44
+ def unaryDemo [F [_]](implicit APP : ClientAPP [F ]): FreeS [F , UnaryDemoResponse ] = {
45
+
46
+ val greetingClientM : GreetingClientM [F ] = APP .greetingClientM
47
+ val echoClientM : EchoClientM [F ] = APP .echoClientM
48
+
49
+ val defaultOptions = CallOptions .DEFAULT
50
+
51
+ val tupled = (
52
+ greetingClientM.sayHello(messageRequest, defaultOptions) |@|
53
+ greetingClientM.sayGoodbye(messageRequest, defaultOptions) |@|
54
+ echoClientM.echo(echoRequest, defaultOptions)
55
+ ).tupled
31
56
32
- val request = MessageRequest (" Freestyle" )
33
- val client = new GreetingClient (host, portNode1)
34
- // val client = new GreetingClient(host, portNode2)
57
+ tupled.map {
58
+ case (hi : MessageReply , bye : MessageReply , echo : EchoResponse ) =>
59
+ println(s " Received -> ( ${hi.message}, ${bye.message}, ${echo.message}) " )
60
+ (hi, bye, echo)
61
+ }
62
+ }
63
+
64
+ def main (args : Array [String ]): Unit = {
35
65
36
- // http://www.grpc.io/docs/guides/concepts.html
66
+ Await .result(unaryDemo[ ClientAPP . Op ].interpret[ Future ], Duration . Inf )
37
67
38
- // Unary RPCs where the client sends a single request to the server and
39
- // gets a single response back, just like a normal function call:
40
- client.unaryDemo(request)
68
+ val client = new GreetingClient (host, portNode1)
41
69
42
70
// Server streaming RPCs where the client sends a request to the server and
43
71
// gets a stream to read a sequence of messages back. The client reads from
44
72
// the returned stream until there are no more messages.
45
73
46
- client.serverStreamingDemo(request )
74
+ client.serverStreamingDemo(messageRequest )
47
75
48
76
// Client streaming RPCs where the client writes a sequence of messages and sends them
49
77
// to the server, again using a provided stream. Once the client has finished writing the messages,
@@ -59,21 +87,6 @@ object GreetingClientApp {
59
87
60
88
client.biStreamingDemo()
61
89
62
- // EchoDemo using the same server where the greeting service is deployed.
63
- echoDemo(EchoRequest (" echo..." ))
64
-
65
90
(): Unit
66
91
}
67
-
68
- def echoDemo (request : EchoRequest ): Unit = {
69
-
70
- val channel =
71
- ManagedChannelBuilder .forAddress(host, portNode1).usePlaintext(true ).build
72
-
73
- val asyncEchoClient : EchoServiceStub = EchoServiceGrpc .stub(channel)
74
-
75
- println(" " )
76
- println(s " Received -> ${Await .result(asyncEchoClient.echo(request), Duration .Inf )}" )
77
- println(" " )
78
- }
79
92
}
0 commit comments