Skip to content

Commit

Permalink
use grpc method to call
Browse files Browse the repository at this point in the history
  • Loading branch information
jcai committed Jul 2, 2020
1 parent 13be9df commit 9b3b5f7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wechaty.hostie.support

import com.google.protobuf.StringValue
import io.github.wechaty.grpc.PuppetGrpc
import io.github.wechaty.grpc.puppet.Contact
import io.github.wechaty.grpc.puppet.Contact.ContactPayloadRequest
import wechaty.puppet.ResourceBox
Expand Down Expand Up @@ -84,7 +85,7 @@ trait ContactRawSupport {

override protected def contactRawPayload(contactID: String): Future[ContactPayload] = {
val request = ContactPayloadRequest.newBuilder().setId(contactID).build()
asyncCallback(asyncGrpcClient.contactPayload,request){response =>
asyncCallback(PuppetGrpc.getContactPayloadMethod,request){ response =>
val contact = new ContactPayload
contact.id = response.getId
contact.gender = wechaty.puppet.schemas.Contact.ContactGender.apply(response.getGenderValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import io.github.wechaty.grpc.puppet.Contact.ContactPayloadRequest
import io.github.wechaty.grpc.puppet.{Base, Contact}
import io.github.wechaty.grpc.puppet.Event.EventRequest
import io.grpc.stub.ClientCalls.asyncUnaryCall
import io.grpc.stub.StreamObserver
import io.grpc.{ClientCall, ManagedChannel, ManagedChannelBuilder}
import io.grpc.stub.{ClientCalls, StreamObserver}
import io.grpc.{ClientCall, ManagedChannel, ManagedChannelBuilder, MethodDescriptor}
import wechaty.hostie.PuppetHostie
import wechaty.puppet.schemas.Contact.ContactPayload

Expand Down Expand Up @@ -139,14 +139,14 @@ trait GrpcSupport {
}
}

type ClientCall[ReqT,RespT]=(ReqT,StreamObserver[RespT])=>Unit
type ClientCallback[RespT,T]=RespT => T
protected def asyncCall[ReqT,RespT](call: ClientCall[ReqT, RespT], req: ReqT): Unit = {
protected def asyncCall[ReqT,RespT](call: MethodDescriptor[ReqT, RespT], req: ReqT): Unit = {
asyncCallback(call,req)(resp=> resp)
}
protected def asyncCallback[ReqT,RespT,T](call: ClientCall[ReqT, RespT], req: ReqT)(callback:ClientCallback[RespT,T]):Future[T]= {
def asyncCallback[ReqT, RespT,T](callMethod: MethodDescriptor[ReqT, RespT], req: ReqT)(callback:ClientCallback[RespT,T]): Future[T]= {
val call = channel.newCall(callMethod,asyncGrpcClient.getCallOptions)
val promise = Promise[T]
call(req,new StreamObserver[RespT] {
ClientCalls.asyncUnaryCall(call,req,new StreamObserver[RespT] {
override def onNext(value: RespT): Unit = {
val result = callback(value)
promise.success(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import wechaty.padplus.schemas.ModelMessage.PadplusMessagePayload
import wechaty.padplus.schemas.ModelRoom.{PadplusRoomMemberMap, PadplusRoomPayload}
import wechaty.puppet.schemas.Puppet.objectMapper

import scala.language.implicitConversions

/**
*
* @author <a href="mailto:[email protected]">Jun Tsai</a>
Expand Down

0 comments on commit 9b3b5f7

Please sign in to comment.