OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 import UIKit |
| 35 |
| 36 class ViewController: UIViewController { |
| 37 |
| 38 override func viewDidLoad() { |
| 39 super.viewDidLoad() |
| 40 |
| 41 let RemoteHost = "grpc-test.sandbox.googleapis.com" |
| 42 |
| 43 let request = RMTSimpleRequest() |
| 44 request.responseSize = 10 |
| 45 request.fillUsername = true |
| 46 request.fillOauthScope = true |
| 47 |
| 48 |
| 49 // Example gRPC call using a generated proto client library: |
| 50 |
| 51 let service = RMTTestService(host: RemoteHost) |
| 52 service.unaryCallWithRequest(request) { response, error in |
| 53 if let response = response { |
| 54 NSLog("1. Finished successfully with response:\n\(response)") |
| 55 } else { |
| 56 NSLog("1. Finished with error: \(error!)") |
| 57 } |
| 58 } |
| 59 |
| 60 |
| 61 // Same but manipulating headers: |
| 62 |
| 63 var RPC : ProtoRPC! // Needed to convince Swift to capture by reference (__b
lock) |
| 64 RPC = service.RPCToUnaryCallWithRequest(request) { response, error in |
| 65 if let response = response { |
| 66 NSLog("2. Finished successfully with response:\n\(response)") |
| 67 } else { |
| 68 NSLog("2. Finished with error: \(error!)") |
| 69 } |
| 70 NSLog("2. Response headers: \(RPC.responseHeaders)") |
| 71 NSLog("2. Response trailers: \(RPC.responseTrailers)") |
| 72 } |
| 73 |
| 74 RPC.requestHeaders["My-Header"] = "My value" |
| 75 |
| 76 RPC.start() |
| 77 |
| 78 |
| 79 // Same example call using the generic gRPC client library: |
| 80 |
| 81 let method = ProtoMethod(package: "grpc.testing", service: "TestService", me
thod: "UnaryCall") |
| 82 |
| 83 let requestsWriter = GRXWriter(value: request.data()) |
| 84 |
| 85 let call = GRPCCall(host: RemoteHost, path: method.HTTPPath, requestsWriter:
requestsWriter) |
| 86 |
| 87 call.requestHeaders["My-Header"] = "My value" |
| 88 |
| 89 call.startWithWriteable(GRXWriteable { response, error in |
| 90 if let response = response as? NSData { |
| 91 NSLog("3. Received response:\n\(RMTSimpleResponse(data: response, error:
nil))") |
| 92 } else { |
| 93 NSLog("3. Finished with error: \(error!)") |
| 94 } |
| 95 NSLog("3. Response headers: \(call.responseHeaders)") |
| 96 NSLog("3. Response trailers: \(call.responseTrailers)") |
| 97 }) |
| 98 } |
| 99 } |
OLD | NEW |