Debian Codegraph @

src/objective-c/examples/SwiftSample/ViewController.swift

raw
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
23 class ViewController: UIViewController {
24
25 override func viewDidLoad() {
26 super.viewDidLoad()
27
28 let RemoteHost = "grpc-test.sandbox.googleapis.com"
29
30 let request = RMTSimpleRequest()
31 request.responseSize = 10
32 request.fillUsername = true
33 request.fillOauthScope = true
34
35
36 // Example gRPC call using a generated proto client library:
37
38 let service = RMTTestService(host: RemoteHost)
39 service.unaryCall(with: request) { response, error in
40 if let response = response {
41 NSLog("1. Finished successfully with response:\n\(response)")
42 } else {
43 NSLog("1. Finished with error: \(error!)")
44 }
45 }
46
47
48 // Same but manipulating headers:
49
50 var RPC : GRPCProtoCall! // Needed to convince Swift to capture by reference (__block)
51 RPC = service.rpcToUnaryCall(with: request) { response, error in
52 if let response = response {
53 NSLog("2. Finished successfully with response:\n\(response)")
54 } else {
55 NSLog("2. Finished with error: \(error!)")
56 }
57 NSLog("2. Response headers: \(String(describing: RPC.responseHeaders))")
58 NSLog("2. Response trailers: \(String(describing: RPC.responseTrailers))")
59 }
60
61 // TODO(jcanizales): Revert to using subscript syntax once XCode 8 is released.
62 RPC.requestHeaders["My-Header"] = "My value"
63
64 RPC.start()
65
66
67 // Same example call using the generic gRPC client library:
68
69 let method = GRPCProtoMethod(package: "grpc.testing", service: "TestService", method: "UnaryCall")!
70
71 let requestsWriter = GRXWriter(value: request.data())!
72
73 let call = GRPCCall(host: RemoteHost, path: method.httpPath, requestsWriter: requestsWriter)!
74
75 call.requestHeaders["My-Header"] = "My value"
76
77 call.start(with: GRXWriteable { response, error in
78 if let response = response as? Data {
79 NSLog("3. Received response:\n\(try! RMTSimpleResponse(data: response))")
80 } else {
81 NSLog("3. Finished with error: \(error!)")
82 }
83 NSLog("3. Response headers: \(String(describing: call.responseHeaders))")
84 NSLog("3. Response trailers: \(String(describing: call.responseTrailers))")
85 })
86 }
87 }