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 | |
| 41 | let RemoteHost = "grpc-test.sandbox.google.com" |
| 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 | |
| 74 | RPC.requestHeaders["My-Header"] = "My value" |
| 75 | |
| 76 | RPC.start() |
| 77 | |
| 78 | |
Jorge Canizales | 875d1d1 | 2015-08-31 18:05:56 -0700 | [diff] [blame] | 79 | // Same example call using the generic gRPC client library: |
| 80 | |
| 81 | let method = ProtoMethod(package: "grpc.testing", service: "TestService", method: "UnaryCall") |
| 82 | |
Jorge Canizales | 9fffc26 | 2015-08-31 19:15:21 -0700 | [diff] [blame] | 83 | let requestsWriter = GRXWriter(value: request.data()) |
Jorge Canizales | 875d1d1 | 2015-08-31 18:05:56 -0700 | [diff] [blame] | 84 | |
| 85 | let call = GRPCCall(host: RemoteHost, path: method.HTTPPath, requestsWriter: requestsWriter) |
| 86 | |
Jorge Canizales | ff6466b | 2015-09-03 10:10:34 -0700 | [diff] [blame] | 87 | call.requestHeaders["My-Header"] = "My value" |
Jorge Canizales | 875d1d1 | 2015-08-31 18:05:56 -0700 | [diff] [blame] | 88 | |
Jorge Canizales | ff6466b | 2015-09-03 10:10:34 -0700 | [diff] [blame] | 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 | }) |
Jorge Canizales | 4020594 | 2015-08-31 15:54:59 -0700 | [diff] [blame] | 98 | } |
| 99 | } |