1 /* 2 * 3 * Copyright 2015 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 import UIKit 20 21 import RemoteTest 22 import RxLibrary 23 24 class ViewController: UIViewController { 25 viewDidLoadnull26 override func viewDidLoad() { 27 super.viewDidLoad() 28 29 let RemoteHost = "grpc-test.sandbox.googleapis.com" 30 31 let request = RMTSimpleRequest() 32 request.responseSize = 10 33 request.fillUsername = true 34 request.fillOauthScope = true 35 36 37 // Example gRPC call using a generated proto client library: 38 39 let service = RMTTestService(host: RemoteHost) 40 service.unaryCall(with: request) { response, error in 41 if let response = response { 42 NSLog("1. Finished successfully with response:\n\(response)") 43 } else { 44 NSLog("1. Finished with error: \(error!)") 45 } 46 } 47 48 49 // Same but manipulating headers: 50 51 var RPC : GRPCProtoCall! // Needed to convince Swift to capture by reference (__block) 52 RPC = service.rpcToUnaryCall(with: request) { response, error in 53 if let response = response { 54 NSLog("2. Finished successfully with response:\n\(response)") 55 } else { 56 NSLog("2. Finished with error: \(error!)") 57 } 58 NSLog("2. Response headers: \(String(describing: RPC.responseHeaders))") 59 NSLog("2. Response trailers: \(String(describing: RPC.responseTrailers))") 60 } 61 62 // TODO(jcanizales): Revert to using subscript syntax once XCode 8 is released. 63 RPC.requestHeaders["My-Header"] = "My value" 64 65 RPC.start() 66 67 68 // Same example call using the generic gRPC client library: 69 70 let method = GRPCProtoMethod(package: "grpc.testing", service: "TestService", method: "UnaryCall")! 71 72 let requestsWriter = GRXWriter(value: request.data())! 73 74 let call = GRPCCall(host: RemoteHost, path: method.httpPath, requestsWriter: requestsWriter)! 75 76 call.requestHeaders["My-Header"] = "My value" 77 78 call.start(with: GRXWriteable { response, error in 79 if let response = response as? Data { 80 NSLog("3. Received response:\n\(try! RMTSimpleResponse(data: response))") 81 } else { 82 NSLog("3. Finished with error: \(error!)") 83 } 84 NSLog("3. Response headers: \(String(describing: call.responseHeaders))") 85 NSLog("3. Response trailers: \(String(describing: call.responseTrailers))") 86 }) 87 } 88 } 89