Skip to content

[cloud_functions] Add macOS support #1707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cloud_functions/cloud_functions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.2

* Add macOS support

## 0.4.1+9

* Depends on `cloud_functions_web` so that projects importing this plugin will get web support.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "CloudFunctionsPlugin.h"
#import "UserAgent.h"

#import "Firebase/Firebase.h"

@interface CloudFunctionsPlugin ()
@property(nonatomic, retain) FlutterMethodChannel *_channel;
@end

@implementation CloudFunctionsPlugin

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
FlutterMethodChannel *channel =
[FlutterMethodChannel methodChannelWithName:@"cloud_functions"
binaryMessenger:[registrar messenger]];
CloudFunctionsPlugin *instance = [[CloudFunctionsPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];

SEL sel = NSSelectorFromString(@"registerLibrary:withVersion:");
if ([FIRApp respondsToSelector:sel]) {
[FIRApp performSelector:sel withObject:LIBRARY_NAME withObject:LIBRARY_VERSION];
}
}

- (instancetype)init {
self = [super init];
if (self) {
if (![FIRApp appNamed:@"__FIRAPP_DEFAULT"]) {
NSLog(@"Configuring the default Firebase app...");
[FIRApp configure];
NSLog(@"Configured the default Firebase app %@.", [FIRApp defaultApp].name);
}
}
return self;
}

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
if ([@"CloudFunctions#call" isEqualToString:call.method]) {
NSString *functionName = call.arguments[@"functionName"];
NSObject *parameters = call.arguments[@"parameters"];
NSString *appName = call.arguments[@"app"];
NSString *region = call.arguments[@"region"];
NSString *origin = call.arguments[@"origin"];
NSNumber *timeoutMicroseconds = call.arguments[@"timeoutMicroseconds"];
FIRApp *app = [FIRApp appNamed:appName];
FIRFunctions *functions;
if (region != nil && region != (id)[NSNull null]) {
functions = [FIRFunctions functionsForApp:app region:region];
} else {
functions = [FIRFunctions functionsForApp:app];
}
if (origin != nil && origin != (id)[NSNull null]) {
[functions useFunctionsEmulatorOrigin:origin];
}
FIRHTTPSCallable *function = [functions HTTPSCallableWithName:functionName];
if (timeoutMicroseconds != nil && timeoutMicroseconds != [NSNull null]) {
[function setTimeoutInterval:(NSTimeInterval)timeoutMicroseconds.doubleValue / 1000000];
}
[function callWithObject:parameters
completion:^(FIRHTTPSCallableResult *callableResult, NSError *error) {
if (error) {
FlutterError *flutterError;
if (error.domain == FIRFunctionsErrorDomain) {
NSDictionary *details = [NSMutableDictionary dictionary];
[details setValue:[self mapFunctionsErrorCodes:error.code] forKey:@"code"];
if (error.localizedDescription != nil) {
[details setValue:error.localizedDescription forKey:@"message"];
}
if (error.userInfo[FIRFunctionsErrorDetailsKey] != nil) {
[details setValue:error.userInfo[FIRFunctionsErrorDetailsKey]
forKey:@"details"];
}

flutterError =
[FlutterError errorWithCode:@"functionsError"
message:@"Firebase function failed with exception."
details:details];
} else {
flutterError = [FlutterError
errorWithCode:[NSString stringWithFormat:@"%ld", error.code]
message:error.localizedDescription
details:nil];
}
result(flutterError);
} else {
result(callableResult.data);
}
}];
} else {
result(FlutterMethodNotImplemented);
}
}

// Map function error code objects to Strings that match error names on Android.
- (NSString *)mapFunctionsErrorCodes:(FIRFunctionsErrorCode)code {
if (code == FIRFunctionsErrorCodeAborted) {
return @"ABORTED";
} else if (code == FIRFunctionsErrorCodeAlreadyExists) {
return @"ALREADY_EXISTS";
} else if (code == FIRFunctionsErrorCodeCancelled) {
return @"CANCELLED";
} else if (code == FIRFunctionsErrorCodeDataLoss) {
return @"DATA_LOSS";
} else if (code == FIRFunctionsErrorCodeDeadlineExceeded) {
return @"DEADLINE_EXCEEDED";
} else if (code == FIRFunctionsErrorCodeFailedPrecondition) {
return @"FAILED_PRECONDITION";
} else if (code == FIRFunctionsErrorCodeInternal) {
return @"INTERNAL";
} else if (code == FIRFunctionsErrorCodeInvalidArgument) {
return @"INVALID_ARGUMENT";
} else if (code == FIRFunctionsErrorCodeNotFound) {
return @"NOT_FOUND";
} else if (code == FIRFunctionsErrorCodeOK) {
return @"OK";
} else if (code == FIRFunctionsErrorCodeOutOfRange) {
return @"OUT_OF_RANGE";
} else if (code == FIRFunctionsErrorCodePermissionDenied) {
return @"PERMISSION_DENIED";
} else if (code == FIRFunctionsErrorCodeResourceExhausted) {
return @"RESOURCE_EXHAUSTED";
} else if (code == FIRFunctionsErrorCodeUnauthenticated) {
return @"UNAUTHENTICATED";
} else if (code == FIRFunctionsErrorCodeUnavailable) {
return @"UNAVAILABLE";
} else if (code == FIRFunctionsErrorCodeUnimplemented) {
return @"UNIMPLEMENTED";
} else {
return @"UNKNOWN";
}
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Flutter-related
**/Flutter/ephemeral/
**/Pods/

# Xcode-related
**/xcuserdata/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "ephemeral/Flutter-Generated.xcconfig"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "ephemeral/Flutter-Generated.xcconfig"
Loading