Jorge Canizales | 4020594 | 2015-08-31 15:54:59 -0700 | [diff] [blame] | 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() |
Jorge Canizales | 875d1d1 | 2015-08-31 18:05:56 -0700 | [diff] [blame] | 40 | |
Adele Zhou | 83576ba | 2016-01-20 14:37:07 -0800 | [diff] [blame] | 41 | let RemoteHost = "grpc-test.sandbox.googleapis.com" |
Jorge Canizales | 875d1d1 | 2015-08-31 18:05:56 -0700 | [diff] [blame] | 42 | |
Jorge Canizales | 9fffc26 | 2015-08-31 19:15:21 -0700 | [diff] [blame] | 43 | let request = RMTSimpleRequest() |
| 44 | request.responseSize = 10 |
| 45 | request.fillUsername = true |
| 46 | request.fillOauthScope = true |
| 47 | |
Jorge Canizales | ff6466b | 2015-09-03 10:10:34 -0700 | [diff] [blame] | 48 | |
Jorge Canizales | 9fffc26 | 2015-08-31 19:15:21 -0700 | [diff] [blame] | 49 | // Example gRPC call using a generated proto client library: |
| 50 | |
| 51 | let service = RMTTestService(host: RemoteHost) |
Jorge Canizales | ff6466b | 2015-09-03 10:10:34 -0700 | [diff] [blame] | 52 | service.unaryCallWithRequest(request) { response, error in |
Jorge Canizales | 9fffc26 | 2015-08-31 19:15:21 -0700 | [diff] [blame] | 53 | if let response = response { |
Jorge Canizales | ff6466b | 2015-09-03 10:10:34 -0700 | [diff] [blame] | 54 | NSLog("1. Finished successfully with response:\n\(response)") |
Jorge Canizales | 9fffc26 | 2015-08-31 19:15:21 -0700 | [diff] [blame] | 55 | } else { |
Jorge Canizales | ff6466b | 2015-09-03 10:10:34 -0700 | [diff] [blame] | 56 | NSLog("1. Finished with error: \(error!)") |
Jorge Canizales | 9fffc26 | 2015-08-31 19:15:21 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Jorge Canizales | ff6466b | 2015-09-03 10:10:34 -0700 | [diff] [blame] | 60 | |
| 61 | // Same but manipulating headers: |
| 62 | |
| 63 | var RPC : ProtoRPC! // Needed to convince Swift to capture by reference (__block) |
| 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 | |
Yuchen Zeng | d6f8f0b | 2016-06-23 17:33:01 -0700 | [diff] [blame] | 74 | // TODO(jcanizales): Revert to using subscript syntax once XCode 8 is released. |
Yosuke Ishikawa | 9a4d089 | 2016-04-29 01:05:39 +0900 | [diff] [blame] | 75 | RPC.requestHeaders.setObject("My value", forKey: "My-Header") |
Jorge Canizales | ff6466b | 2015-09-03 10:10:34 -0700 | [diff] [blame] | 76 | |
| 77 | RPC.start() |
| 78 | |
| 79 | |
Jorge Canizales | 875d1d1 | 2015-08-31 18:05:56 -0700 | [diff] [blame] | 80 | // Same example call using the generic gRPC client library: |
| 81 | |
| 82 | let method = ProtoMethod(package: "grpc.testing", service: "TestService", method: "UnaryCall") |
| 83 | |
Jorge Canizales | 9fffc26 | 2015-08-31 19:15:21 -0700 | [diff] [blame] | 84 | let requestsWriter = GRXWriter(value: request.data()) |
Jorge Canizales | 875d1d1 | 2015-08-31 18:05:56 -0700 | [diff] [blame] | 85 | |
| 86 | let call = GRPCCall(host: RemoteHost, path: method.HTTPPath, requestsWriter: requestsWriter) |
| 87 | |
Yosuke Ishikawa | 9a4d089 | 2016-04-29 01:05:39 +0900 | [diff] [blame] | 88 | call.requestHeaders.setObject("My value", forKey: "My-Header") |
Jorge Canizales | 875d1d1 | 2015-08-31 18:05:56 -0700 | [diff] [blame] | 89 | |
Jorge Canizales | ff6466b | 2015-09-03 10:10:34 -0700 | [diff] [blame] | 90 | call.startWithWriteable(GRXWriteable { response, error in |
| 91 | if let response = response as? NSData { |
| 92 | NSLog("3. Received response:\n\(RMTSimpleResponse(data: response, error: nil))") |
| 93 | } else { |
| 94 | NSLog("3. Finished with error: \(error!)") |
| 95 | } |
| 96 | NSLog("3. Response headers: \(call.responseHeaders)") |
| 97 | NSLog("3. Response trailers: \(call.responseTrailers)") |
| 98 | }) |
Jorge Canizales | 4020594 | 2015-08-31 15:54:59 -0700 | [diff] [blame] | 99 | } |
| 100 | } |