Browse Source

Improve makeCall interface (dthurn@)

Tim Burks 9 years ago
parent
commit
21873249d5

+ 4 - 6
Examples/Datastore/Datastore/DatastoreViewController.swift

@@ -112,21 +112,19 @@ class DatastoreViewController : NSViewController, NSTextFieldDelegate {
       guard let client = client else {
         return
       }
-      call = client.makeCall(host: requestHost,
-                               method: "/google.datastore.v1.Datastore/RunQuery",
-                               timeout: 30.0)
+      call = client.makeCall("/google.datastore.v1.Datastore/RunQuery")
       guard let call = call else {
         return
       }
-      _ = call.performNonStreamingCall(messageData: requestMessageData,
-                                       metadata: requestMetadata)
+      try! call.perform(message: requestMessageData,
+                        metadata: requestMetadata)
       { (callResult) in
         print("Received status: \(callResult.statusCode): \(callResult.statusMessage)")
         if let messageData = callResult.resultData,
           let responseMessage = self.fileDescriptorSet.readMessage("RunQueryResponse",
                                                                    data:messageData) {
           responseMessage.display()
-          responseMessage.forOneField("text") {(field) in
+          try! responseMessage.forOneField("text") {(field) in
             DispatchQueue.main.async {
               self.outputField.stringValue = field.string()
             }

+ 8 - 6
Examples/Echo/Swift/Echo/EchoViewController.swift

@@ -98,23 +98,25 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
       let certificates = try! String(contentsOf: certificateURL)
       client = Client(address:address, certificates:certificates, host:host)
     }
+    if let client = client {
+      client.host = host
+    }
   }
 
   func callServer(address:String) throws -> Void {
-    let requestHost = "example.com"
     let requestMetadata = Metadata(["x-goog-api-key":"YOUR_API_KEY",
                                     "x-ios-bundle-identifier":Bundle.main.bundleIdentifier!])
-
+    let host = "example.com"
     if (self.streamingButton.intValue == 0) {
       // NONSTREAMING
       if let requestMessage = self.fileDescriptorSet.makeMessage("EchoRequest") {
         requestMessage.addField("text", value:self.messageField.stringValue)
         let requestMessageData = requestMessage.data()
-        prepareClient(address:address, host:requestHost)
+        prepareClient(address:address, host:host)
         guard let client = client else {
           return
         }
-        call = client.makeCall(host: requestHost, method: "/echo.Echo/Get")
+        call = client.makeCall("/echo.Echo/Get")
         guard let call = call else {
           return
         }
@@ -141,11 +143,11 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
     else {
       // STREAMING
       if (!nowStreaming) {
-        prepareClient(address:address, host:requestHost)
+        prepareClient(address:address, host:host)
         guard let client = client else {
           return
         }
-        call = client.makeCall(host: requestHost, method: "/echo.Echo/Update")
+        call = client.makeCall("/echo.Echo/Update")
         guard let call = call else {
           return
         }

+ 4 - 4
Examples/Sessions/Sessions/Document.swift

@@ -155,7 +155,7 @@ class Document: NSDocument {
       self.log("GRPC version " + gRPC.version())
 
       self.client = gRPC.Client(address:address)
-      let host = "foo.test.google.fr"
+      self.client.host = "foo.test.google.fr"
       let messageData = "hello, server!".data(using: .utf8)
 
       let steps = 10
@@ -165,15 +165,15 @@ class Document: NSDocument {
           break
         }
         let method = (i < steps) ? "/hello" : "/quit"
-        let call = self.client.makeCall(host: host, method: method)
+        let call = self.client.makeCall(method)
 
         let metadata = Metadata([["x": "xylophone"],
                                  ["y": "yu"],
                                  ["z": "zither"]])
 
         do {
-          try call.performNonStreamingCall(message: messageData!,
-                                           metadata: metadata)
+          try call.perform(message: messageData!,
+                           metadata: metadata)
           {(callResult) in
 
             if let initialMetadata = callResult.initialMetadata {

+ 1 - 2
Examples/Speech/Speech/SpeechRecognitionService.swift

@@ -41,8 +41,7 @@ class SpeechRecognitionService {
 
       if (!nowStreaming) {
         // if we aren't already streaming, set up a gRPC connection
-        call = client.makeCall(host: HOST,
-                               method: "/google.cloud.speech.v1beta1.Speech/StreamingRecognize")
+        call = client.makeCall("/google.cloud.speech.v1beta1.Speech/StreamingRecognize")
 
         if let call = call {
           let metadata = Metadata(["x-goog-api-key":API_KEY,

+ 5 - 4
Examples/StickyNotes/StickyNotes/StickyNoteViewController.swift

@@ -79,10 +79,11 @@ class StickyNoteViewController : NSViewController, NSTextFieldDelegate {
                                         ["z":"zither"]])
 
         client = Client(address:address)
-        let call = client.makeCall(host: requestHost, method: requestMethod)
-        try call.performNonStreamingCall(message: requestMessage.data(),
-                                         metadata: requestMetadata,
-                                         completion:
+        client.host = requestHost
+        let call = client.makeCall(requestMethod)
+        try call.perform(message: requestMessage.data(),
+                         metadata: requestMetadata,
+                         completion:
           { (callResult) in
 
             if let initialMetadata = callResult.initialMetadata {

+ 10 - 3
Packages/gRPC/Sources/Client.swift

@@ -44,13 +44,18 @@ public class Client {
   /// Completion queue for client call operations
   private var completionQueue: CompletionQueue
 
-  /// Timeout for client requests
+  /// Timeout for new calls
   public var timeout: TimeInterval = 600.0
 
+  /// Default host to use for new calls
+  public var host: String
+
+
   /// Initializes a gRPC client
   ///
   /// - Parameter address: the address of the server to be called
   public init(address: String) {
+    self.host = address
     underlyingClient = cgrpc_client_create(address)
     completionQueue = CompletionQueue(underlyingCompletionQueue:cgrpc_client_completion_queue(underlyingClient))
     completionQueue.name = "Client" // only for debugging
@@ -61,6 +66,7 @@ public class Client {
   ///
   /// - Parameter address: the address of the server to be called
   public init(address: String, certificates: String?, host: String?) {
+    self.host = address
     if certificates == nil {
       let bundle = Bundle(for: Client.self)
       let url = bundle.url(forResource: "roots", withExtension: "pem")!
@@ -81,11 +87,12 @@ public class Client {
 
   /// Constructs a Call object to make a gRPC API call
   ///
-  /// - Parameter host: the gRPC host name for the call
   /// - Parameter method: the gRPC method name for the call
+  /// - Parameter host: the gRPC host name for the call. If unspecified, defaults to the Client host
   /// - Parameter timeout: a timeout value in seconds
   /// - Returns: a Call object that can be used to perform the request
-  public func makeCall(host:String, method:String) -> Call {
+  public func makeCall(_ method:String, host:String="") -> Call {
+    let host = (host == "") ? self.host : host
     let underlyingCall = cgrpc_client_create_call(underlyingClient, method, host, timeout)!
     return Call(underlyingCall:underlyingCall, owned:true, completionQueue:self.completionQueue)
   }