aboutsummaryrefslogtreecommitdiffhomepage
path: root/Functions/FirebaseFunctions/FIRFunctions.m
blob: 274d0583c87174a5d966eaf26b92c69d32fc78d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2017 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "FIRFunctions.h"
#import "FIRFunctions+Internal.h"

#import "FIRError.h"
#import "FIRHTTPSCallable+Internal.h"
#import "FIRHTTPSCallable.h"
#import "FUNContext.h"
#import "FUNError.h"
#import "FUNSerializer.h"
#import "FUNUsageValidation.h"

#import "FIRApp.h"
#import "FIRAppInternal.h"
#import "FIROptions.h"
#import "GTMSessionFetcherService.h"

NS_ASSUME_NONNULL_BEGIN

NSString *const kFUNInstanceIDTokenHeader = @"Firebase-Instance-ID-Token";

@interface FIRFunctions () {
  // The network client to use for http requests.
  GTMSessionFetcherService *_fetcherService;
  // The projectID to use for all function references.
  FIRApp *_app;
  // The region to use for all function references.
  NSString *_region;
  // A serializer to encode/decode data and return values.
  FUNSerializer *_serializer;
  // A factory for getting the metadata to include with function calls.
  FUNContextProvider *_contextProvider;
  // For testing only. If this is set, functions will be called against localhost instead of
  // Firebase.
  BOOL _useLocalhost;
}

/**
 * Initialize the Cloud Functions client with the given app and region.
 * @param app The app for the Firebase project.
 * @param region The region for the http trigger, such as "us-central1".
 */
- (id)initWithApp:(FIRApp *)app region:(NSString *)region NS_DESIGNATED_INITIALIZER;

@end

@implementation FIRFunctions

+ (instancetype)functions {
  return [[self alloc] initWithApp:[FIRApp defaultApp] region:@"us-central1"];
}

+ (instancetype)functionsForApp:(FIRApp *)app {
  return [[self alloc] initWithApp:app region:@"us-central1"];
}

+ (instancetype)functionsForRegion:(NSString *)region {
  return [[self alloc] initWithApp:[FIRApp defaultApp] region:region];
}

+ (instancetype)functionsForApp:(FIRApp *)app region:(NSString *)region {
  return [[self alloc] initWithApp:app region:region];
}

- (instancetype)initWithApp:(FIRApp *)app region:(NSString *)region {
  self = [super init];
  if (self) {
    if (!region) {
      FUNThrowInvalidArgument(@"FIRFunctions region cannot be nil.");
    }
    _fetcherService = [[GTMSessionFetcherService alloc] init];
    _app = app;
    _region = [region copy];
    _serializer = [[FUNSerializer alloc] init];
    _contextProvider = [[FUNContextProvider alloc] initWithApp:app];
    _useLocalhost = NO;
  }
  return self;
}

- (void)useLocalhost {
  _useLocalhost = YES;
}

- (NSString *)URLWithName:(NSString *)name {
  if (!name) {
    FUNThrowInvalidArgument(@"FIRFunctions function name cannot be nil.");
  }
  NSString *projectID = _app.options.projectID;
  if (!projectID) {
    FUNThrowInvalidArgument(@"FIRFunctions app projectID cannot be nil.");
  }
  if (_useLocalhost) {
    return [NSString stringWithFormat:@"http://localhost:5005/%@/%@/%@", projectID, _region, name];
  }
  return
      [NSString stringWithFormat:@"https://%@-%@.cloudfunctions.net/%@", _region, projectID, name];
}

- (void)callFunction:(NSString *)name
          withObject:(nullable id)data
          completion:(void (^)(FIRHTTPSCallableResult *_Nullable result,
                               NSError *_Nullable error))completion {
  [_contextProvider getContext:^(FUNContext *_Nullable context, NSError *_Nullable error) {
    if (error) {
      if (completion) {
        completion(nil, error);
      }
      return;
    }
    return [self callFunction:name withObject:data context:context completion:completion];
  }];
}

- (void)callFunction:(NSString *)name
          withObject:(nullable id)data
             context:(FUNContext *)context
          completion:(void (^)(FIRHTTPSCallableResult *_Nullable result,
                               NSError *_Nullable error))completion {
  GTMSessionFetcher *fetcher = [_fetcherService fetcherWithURLString:[self URLWithName:name]];

  NSMutableDictionary *body = [NSMutableDictionary dictionary];
  // Encode the data in the body.
  if (!data) {
    data = [NSNull null];
  }
  id encoded = [_serializer encode:data];
  if (!encoded) {
    FUNThrowInvalidArgument(@"FIRFunctions data encoded as nil. This should not happen.");
  }
  body[@"data"] = encoded;

  NSError *error = nil;
  NSData *payload = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
  if (error) {
    if (completion) {
      dispatch_async(dispatch_get_main_queue(), ^{
        completion(nil, error);
      });
    }
    return;
  }
  fetcher.bodyData = payload;

  // Set the headers.
  [fetcher setRequestValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  if (context.authToken) {
    NSString *value = [NSString stringWithFormat:@"Bearer %@", context.authToken];
    [fetcher setRequestValue:value forHTTPHeaderField:@"Authorization"];
  }
  if (context.instanceIDToken) {
    [fetcher setRequestValue:context.instanceIDToken forHTTPHeaderField:kFUNInstanceIDTokenHeader];
  }

  // Override normal security rules if this is a local test.
  if (_useLocalhost) {
    fetcher.allowLocalhostRequest = YES;
    fetcher.allowedInsecureSchemes = @[ @"http" ];
  }

  FUNSerializer *serializer = _serializer;
  [fetcher beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
    // If there was an HTTP error, convert it to our own error domain.
    if (error) {
      if ([error.domain isEqualToString:kGTMSessionFetcherStatusDomain]) {
        error = FUNErrorForResponse(error.code, data, serializer);
      }
    } else {
      // If there wasn't an HTTP error, see if there was an error in the body.
      error = FUNErrorForResponse(200, data, serializer);
    }
    // If there was an error, report it to the user and stop.
    if (error) {
      if (completion) {
        completion(nil, error);
      }
      return;
    }

    id responseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    if (error) {
      if (completion) {
        completion(nil, error);
      }
      return;
    }
    if (![responseJSON isKindOfClass:[NSDictionary class]]) {
      NSDictionary *userInfo = @{NSLocalizedDescriptionKey : @"Response was not a dictionary."};
      error = [NSError errorWithDomain:FIRFunctionsErrorDomain
                                  code:FIRFunctionsErrorCodeInternal
                              userInfo:userInfo];
      if (completion) {
        completion(nil, error);
      }
      return;
    }
    id dataJSON = responseJSON[@"data"];
    // TODO(klimt): Allow "result" instead of "data" for now, for backwards compatibility.
    if (!dataJSON) {
      dataJSON = responseJSON[@"result"];
    }
    if (!dataJSON) {
      NSDictionary *userInfo =
          @{NSLocalizedDescriptionKey : @"Response did not include data field."};
      error = [NSError errorWithDomain:FIRFunctionsErrorDomain
                                  code:FIRFunctionsErrorCodeInternal
                              userInfo:userInfo];
      if (completion) {
        completion(nil, error);
      }
      return;
    }
    id resultData = [serializer decode:dataJSON error:&error];
    if (error) {
      if (completion) {
        completion(nil, error);
      }
      return;
    }
    id result = [[FIRHTTPSCallableResult alloc] initWithData:resultData];
    if (completion) {
      // If there's no result field, this will return nil, which is fine.
      completion(result, nil);
    }
  }];
}

- (FIRHTTPSCallable *)HTTPSCallableWithName:(NSString *)name {
  return [[FIRHTTPSCallable alloc] initWithFunctions:self name:name];
}

@end

NS_ASSUME_NONNULL_END