From f08edbb85c5cbde74c354c478ac9433d2417d748 Mon Sep 17 00:00:00 2001 From: Bryan Klimt Date: Thu, 21 Jun 2018 15:25:13 -0400 Subject: Add a couple of new small Functions APIs. (#1434) * Add a Functions constructor that allows setting a region. * Add a method to set an emulator origin to use. * Update the changelog --- Functions/CHANGELOG.md | 4 ++++ Functions/Example/Tests/FIRFunctionsTests.m | 8 ++------ Functions/FirebaseFunctions/FIRFunctions.m | 19 +++++++++++-------- Functions/FirebaseFunctions/Public/FIRFunctions.h | 13 ++++++++++--- 4 files changed, 27 insertions(+), 17 deletions(-) (limited to 'Functions') diff --git a/Functions/CHANGELOG.md b/Functions/CHANGELOG.md index e8755c5..a8e61c8 100644 --- a/Functions/CHANGELOG.md +++ b/Functions/CHANGELOG.md @@ -1,3 +1,7 @@ +# v2.1.0 +- Add a constructor to set the region. +- Add a method to set a Cloud Functions emulator origin to use, for testing. + # v2.0.0 - Remove FIR prefix on FIRFunctionsErrorCode in Swift. diff --git a/Functions/Example/Tests/FIRFunctionsTests.m b/Functions/Example/Tests/FIRFunctionsTests.m index 5d11601..fdce514 100644 --- a/Functions/Example/Tests/FIRFunctionsTests.m +++ b/Functions/Example/Tests/FIRFunctionsTests.m @@ -33,17 +33,13 @@ } - (void)testURLWithName { - // TODO(klimt): Add this test back when we add the constructor back. - /* id app = [[FUNFakeApp alloc] initWithProjectID:@"my-project"]; FIRFunctions *functions = [FIRFunctions functionsForApp:app region:@"my-region"]; NSString *url = [functions URLWithName:@"my-endpoint"]; XCTAssertEqualObjects(@"https://my-region-my-project.cloudfunctions.net/my-endpoint", url); - */ - id app = [[FUNFakeApp alloc] initWithProjectID:@"my-project"]; - FIRFunctions *functions = [FIRFunctions functionsForApp:app]; - NSString *url = [functions URLWithName:@"my-endpoint"]; + functions = [FIRFunctions functionsForApp:app]; + url = [functions URLWithName:@"my-endpoint"]; XCTAssertEqualObjects(@"https://us-central1-my-project.cloudfunctions.net/my-endpoint", url); } diff --git a/Functions/FirebaseFunctions/FIRFunctions.m b/Functions/FirebaseFunctions/FIRFunctions.m index 274d058..5a67d86 100644 --- a/Functions/FirebaseFunctions/FIRFunctions.m +++ b/Functions/FirebaseFunctions/FIRFunctions.m @@ -43,9 +43,8 @@ NSString *const kFUNInstanceIDTokenHeader = @"Firebase-Instance-ID-Token"; 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; + // For testing only. If this is set, functions will be called against it instead of Firebase. + NSString *_emulatorOrigin; } /** @@ -86,13 +85,17 @@ NSString *const kFUNInstanceIDTokenHeader = @"Firebase-Instance-ID-Token"; _region = [region copy]; _serializer = [[FUNSerializer alloc] init]; _contextProvider = [[FUNContextProvider alloc] initWithApp:app]; - _useLocalhost = NO; + _emulatorOrigin = nil; } return self; } - (void)useLocalhost { - _useLocalhost = YES; + [self useFunctionsEmulatorOrigin:@"http://localhost:5005"]; +} + +- (void)useFunctionsEmulatorOrigin:(NSString *)origin { + _emulatorOrigin = origin; } - (NSString *)URLWithName:(NSString *)name { @@ -103,8 +106,8 @@ NSString *const kFUNInstanceIDTokenHeader = @"Firebase-Instance-ID-Token"; if (!projectID) { FUNThrowInvalidArgument(@"FIRFunctions app projectID cannot be nil."); } - if (_useLocalhost) { - return [NSString stringWithFormat:@"http://localhost:5005/%@/%@/%@", projectID, _region, name]; + if (_emulatorOrigin) { + return [NSString stringWithFormat:@"%@/%@/%@/%@", _emulatorOrigin, projectID, _region, name]; } return [NSString stringWithFormat:@"https://%@-%@.cloudfunctions.net/%@", _region, projectID, name]; @@ -166,7 +169,7 @@ NSString *const kFUNInstanceIDTokenHeader = @"Firebase-Instance-ID-Token"; } // Override normal security rules if this is a local test. - if (_useLocalhost) { + if (_emulatorOrigin) { fetcher.allowLocalhostRequest = YES; fetcher.allowedInsecureSchemes = @[ @"http" ]; } diff --git a/Functions/FirebaseFunctions/Public/FIRFunctions.h b/Functions/FirebaseFunctions/Public/FIRFunctions.h index d01175c..98d7a67 100644 --- a/Functions/FirebaseFunctions/Public/FIRFunctions.h +++ b/Functions/FirebaseFunctions/Public/FIRFunctions.h @@ -42,7 +42,7 @@ NS_SWIFT_NAME(Functions) * Creates a Cloud Functions client with the default app and given region. * @param region The region for the http trigger, such as "us-central1". */ -// + (instancetype)functionsForRegion:(NSString *)region NS_SWIFT_NAME(functions(region:)); ++ (instancetype)functionsForRegion:(NSString *)region NS_SWIFT_NAME(functions(region:)); /** * Creates a Cloud Functions client with the given app and region. @@ -51,8 +51,8 @@ NS_SWIFT_NAME(Functions) */ // clang-format off // because it incorrectly breaks this NS_SWIFT_NAME. -// + (instancetype)functionsForApp:(FIRApp *)app -// region:(NSString *)region NS_SWIFT_NAME(functions(app:region:)); ++ (instancetype)functionsForApp:(FIRApp *)app + region:(NSString *)region NS_SWIFT_NAME(functions(app:region:)); // clang-format on /** @@ -61,6 +61,13 @@ NS_SWIFT_NAME(Functions) */ - (FIRHTTPSCallable *)HTTPSCallableWithName:(NSString *)name NS_SWIFT_NAME(httpsCallable(_:)); +/** + * Changes this instance to point to a Cloud Functions emulator running locally. + * See https://firebase.google.com/docs/functions/local-emulator + * @param origin The origin of the local emulator, such as "http://localhost:5005". + */ +- (void)useFunctionsEmulatorOrigin:(NSString *)origin NS_SWIFT_NAME(useFunctionsEmulator(origin:)); + @end NS_ASSUME_NONNULL_END -- cgit v1.2.3