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 "ViewController.h"
20
21#import <GRPCClient/GRPCCall.h>
22#import <ProtoRPC/ProtoMethod.h>
23#if USE_FRAMEWORKS
24#import <RemoteTest/Messages.pbobjc.h>
25#import <RemoteTest/Test.pbrpc.h>
26#else
27#import "src/objective-c/examples/RemoteTestClient/Messages.pbobjc.h"
28#import "src/objective-c/examples/RemoteTestClient/Test.pbrpc.h"
29#endif
30#import <RxLibrary/GRXWriteable.h>
31#import <RxLibrary/GRXWriter+Immediate.h>
32
33@implementation ViewController
34
35- (void)viewDidLoad {
36  [super viewDidLoad];
37
38  NSString *const kRemoteHost = @"grpc-test.sandbox.googleapis.com";
39
40  RMTSimpleRequest *request = [[RMTSimpleRequest alloc] init];
41  request.responseSize = 10;
42  request.fillUsername = YES;
43  request.fillOauthScope = YES;
44
45  // Example gRPC call using a generated proto client library:
46
47  RMTTestService *service = [[RMTTestService alloc] initWithHost:kRemoteHost];
48  [service unaryCallWithRequest:request
49                        handler:^(RMTSimpleResponse *response, NSError *error) {
50                          if (response) {
51                            NSLog(@"Finished successfully with response:\n%@", response);
52                          } else if (error) {
53                            NSLog(@"Finished with error: %@", error);
54                          }
55                        }];
56
57  // Same example call using the generic gRPC client library:
58
59  GRPCProtoMethod *method = [[GRPCProtoMethod alloc] initWithPackage:@"grpc.testing"
60                                                             service:@"TestService"
61                                                              method:@"UnaryCall"];
62
63  GRXWriter *requestsWriter = [GRXWriter writerWithValue:[request data]];
64
65  GRPCCall *call = [[GRPCCall alloc] initWithHost:kRemoteHost
66                                             path:method.HTTPPath
67                                   requestsWriter:requestsWriter];
68
69  id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc]
70      initWithValueHandler:^(NSData *value) {
71        RMTSimpleResponse *response = [RMTSimpleResponse parseFromData:value error:NULL];
72        NSLog(@"Received response:\n%@", response);
73      }
74      completionHandler:^(NSError *errorOrNil) {
75        if (errorOrNil) {
76          NSLog(@"Finished with error: %@", errorOrNil);
77        } else {
78          NSLog(@"Finished successfully.");
79        }
80      }];
81
82  [call startWithWriteable:responsesWriteable];
83}
84
85@end
86