blob: 2a95d2de516903abae8924d58dfb9ca1db553d53 [file] [log] [blame]
Jorge Canizales40205942015-08-31 15:54:59 -07001/*
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
34import UIKit
35
36class ViewController: UIViewController {
37
38 override func viewDidLoad() {
39 super.viewDidLoad()
Jorge Canizales875d1d12015-08-31 18:05:56 -070040
Adele Zhou83576ba2016-01-20 14:37:07 -080041 let RemoteHost = "grpc-test.sandbox.googleapis.com"
Jorge Canizales875d1d12015-08-31 18:05:56 -070042
Jorge Canizales9fffc262015-08-31 19:15:21 -070043 let request = RMTSimpleRequest()
44 request.responseSize = 10
45 request.fillUsername = true
46 request.fillOauthScope = true
47
Jorge Canizalesff6466b2015-09-03 10:10:34 -070048
Jorge Canizales9fffc262015-08-31 19:15:21 -070049 // Example gRPC call using a generated proto client library:
50
51 let service = RMTTestService(host: RemoteHost)
Jorge Canizalesff6466b2015-09-03 10:10:34 -070052 service.unaryCallWithRequest(request) { response, error in
Jorge Canizales9fffc262015-08-31 19:15:21 -070053 if let response = response {
Jorge Canizalesff6466b2015-09-03 10:10:34 -070054 NSLog("1. Finished successfully with response:\n\(response)")
Jorge Canizales9fffc262015-08-31 19:15:21 -070055 } else {
Jorge Canizalesff6466b2015-09-03 10:10:34 -070056 NSLog("1. Finished with error: \(error!)")
Jorge Canizales9fffc262015-08-31 19:15:21 -070057 }
58 }
59
Jorge Canizalesff6466b2015-09-03 10:10:34 -070060
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 Zengd6f8f0b2016-06-23 17:33:01 -070074 // TODO(jcanizales): Revert to using subscript syntax once XCode 8 is released.
Yosuke Ishikawa9a4d0892016-04-29 01:05:39 +090075 RPC.requestHeaders.setObject("My value", forKey: "My-Header")
Jorge Canizalesff6466b2015-09-03 10:10:34 -070076
77 RPC.start()
78
79
Jorge Canizales875d1d12015-08-31 18:05:56 -070080 // Same example call using the generic gRPC client library:
81
82 let method = ProtoMethod(package: "grpc.testing", service: "TestService", method: "UnaryCall")
83
Jorge Canizales9fffc262015-08-31 19:15:21 -070084 let requestsWriter = GRXWriter(value: request.data())
Jorge Canizales875d1d12015-08-31 18:05:56 -070085
86 let call = GRPCCall(host: RemoteHost, path: method.HTTPPath, requestsWriter: requestsWriter)
87
Yosuke Ishikawa9a4d0892016-04-29 01:05:39 +090088 call.requestHeaders.setObject("My value", forKey: "My-Header")
Jorge Canizales875d1d12015-08-31 18:05:56 -070089
Jorge Canizalesff6466b2015-09-03 10:10:34 -070090 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 Canizales40205942015-08-31 15:54:59 -070099 }
100}