Skip to content
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

feat(fdc): Updated to allow for sdktype to be set (core vs gen) #13392

Merged
merged 8 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import 'package:firebase_auth/firebase_auth.dart';
part 'dataconnect_error.dart';
part 'dataconnect_options.dart';

enum CallerSDKType { core, generated }

String getGoogApiVal(CallerSDKType sdkType, String packageVersion) {
String apiClientValue = 'gl-dart/$packageVersion fire/$packageVersion';
maneesht marked this conversation as resolved.
Show resolved Hide resolved
if (sdkType == CallerSDKType.generated) {
apiClientValue += ' dart/gen';
}
return apiClientValue;
}

/// Transport Options for connecting to a specific host.
class TransportOptions {
/// Constructor
Expand All @@ -30,7 +40,7 @@ class TransportOptions {
/// Interface for transports connecting to the DataConnect backend.
abstract class DataConnectTransport {
/// Constructor.
DataConnectTransport(this.transportOptions, this.options);
DataConnectTransport(this.transportOptions, this.options, this.sdkType);

/// Transport options.
TransportOptions transportOptions;
Expand All @@ -44,6 +54,9 @@ abstract class DataConnectTransport {
/// FirebaseAppCheck to use to get app check token.
FirebaseAppCheck? appCheck;

/// Core or generated SDK being used.
CallerSDKType sdkType;

/// Invokes corresponding query endpoint.
Future<Data> invokeQuery<Data, Variables>(
String queryName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@ part of firebase_data_connect;
class FirebaseDataConnect extends FirebasePluginPlatform {
/// Constructor for initializing Data Connect
@visibleForTesting
FirebaseDataConnect({
required this.app,
required this.connectorConfig,
this.auth,
this.appCheck,
}) : options = DataConnectOptions(
FirebaseDataConnect(
{required this.app,
required this.connectorConfig,
this.auth,
this.appCheck,
CallerSDKType? sdkType})
: options = DataConnectOptions(
app.options.projectId,
connectorConfig.location,
connectorConfig.connector,
connectorConfig.serviceId),
super(app.name, 'plugins.flutter.io/firebase_data_connect') {
_queryManager = QueryManager(this);
if (sdkType != null) {
this._sdkType = sdkType;
}
}

/// QueryManager manages ongoing queries, and their subscriptions.
late QueryManager _queryManager;

/// Type of SDK the user is currently calling.
CallerSDKType _sdkType = CallerSDKType.core;

/// FirebaseApp
FirebaseApp app;

Expand Down Expand Up @@ -54,7 +61,8 @@ class FirebaseDataConnect extends FirebasePluginPlatform {
void checkTransport() {
transportOptions ??=
TransportOptions('firebasedataconnect.googleapis.com', null, true);
transport = getTransport(transportOptions!, options, auth, appCheck);
transport =
getTransport(transportOptions!, options, _sdkType, auth, appCheck);
}

/// Returns a [QueryRef] object.
Expand Down Expand Up @@ -99,6 +107,7 @@ class FirebaseDataConnect extends FirebasePluginPlatform {
FirebaseApp? app,
FirebaseAuth? auth,
FirebaseAppCheck? appCheck,
CallerSDKType? sdkType,
required ConnectorConfig connectorConfig,
}) {
app ??= Firebase.app();
Expand All @@ -114,7 +123,8 @@ class FirebaseDataConnect extends FirebasePluginPlatform {
app: app,
auth: auth,
appCheck: appCheck,
connectorConfig: connectorConfig);
connectorConfig: connectorConfig,
sdkType: sdkType);
if (cachedInstances[app.name] == null) {
cachedInstances[app.name] = <String, FirebaseDataConnect>{};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ part of firebase_data_connect_grpc;
/// Transport used for Android/iOS. Uses a GRPC transport instead of REST.
class GRPCTransport implements DataConnectTransport {
/// GRPCTransport creates a new channel
GRPCTransport(this.transportOptions, this.options, this.auth, this.appCheck) {
GRPCTransport(this.transportOptions, this.options, this.sdkType, this.auth,
this.appCheck) {
bool isSecure =
transportOptions.isSecure == null || transportOptions.isSecure == true;
channel = ClientChannel(transportOptions.host,
Expand All @@ -29,6 +30,9 @@ class GRPCTransport implements DataConnectTransport {
@override
FirebaseAppCheck? appCheck;

@override
CallerSDKType sdkType;

/// Name of the endpoint.
late String name;

Expand Down Expand Up @@ -61,7 +65,7 @@ class GRPCTransport implements DataConnectTransport {
}
Map<String, String> metadata = {
'x-goog-request-params': 'location=${options.location}&frontend=data',
'x-goog-api-client': 'gl-dart/flutter fire/$packageVersion'
'x-goog-api-client': getGoogApiVal(sdkType, packageVersion)
};

if (authToken != null) {
Expand Down Expand Up @@ -134,6 +138,7 @@ class GRPCTransport implements DataConnectTransport {
DataConnectTransport getTransport(
TransportOptions transportOptions,
DataConnectOptions options,
CallerSDKType sdkType,
FirebaseAuth? auth,
FirebaseAppCheck? appCheck) =>
GRPCTransport(transportOptions, options, auth, appCheck);
GRPCTransport(transportOptions, options, sdkType, auth, appCheck);
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ part of firebase_data_connect_rest;
/// RestTransport makes requests out to the REST endpoints of the configured backend.
class RestTransport implements DataConnectTransport {
/// Initializes necessary protocol and port.
RestTransport(this.transportOptions, this.options, this.auth, this.appCheck) {
RestTransport(
this.transportOptions,
this.options,
this.sdkType,
this.auth,
this.appCheck,
) {
String protocol = 'http';
if (transportOptions.isSecure == null ||
transportOptions.isSecure == true) {
Expand All @@ -29,6 +35,8 @@ class RestTransport implements DataConnectTransport {
@override
FirebaseAppCheck? appCheck;

CallerSDKType sdkType;

/// Current endpoint URL.
@visibleForTesting
late String url;
Expand Down Expand Up @@ -62,7 +70,7 @@ class RestTransport implements DataConnectTransport {
Map<String, String> headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'x-goog-api-client': 'gl-dart/flutter fire/$packageVersion'
'x-goog-api-client': getGoogApiVal(sdkType, packageVersion)
};
String? authToken;
try {
Expand Down Expand Up @@ -148,6 +156,7 @@ class RestTransport implements DataConnectTransport {
DataConnectTransport getTransport(
TransportOptions transportOptions,
DataConnectOptions options,
CallerSDKType sdkType,
FirebaseAuth? auth,
FirebaseAppCheck? appCheck) =>
RestTransport(transportOptions, options, auth, appCheck);
RestTransport(transportOptions, options, sdkType, auth, appCheck);
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ part of firebase_data_connect_transport;
/// Default TransportStub to satisfy compilation of the library.
class TransportStub implements DataConnectTransport {
/// Constructor.
TransportStub(this.transportOptions, this.options, this.auth, this.appCheck);
TransportStub(this.transportOptions, this.options, this.sdkType, this.auth,
this.appCheck);

/// FirebaseAuth
@override
Expand All @@ -25,6 +26,10 @@ class TransportStub implements DataConnectTransport {
@override
TransportOptions transportOptions;

/// Core or Generated SDK being used.
@override
CallerSDKType sdkType;

/// Stub for invoking a mutation.
@override
Future<Data> invokeMutation<Data, Variables>(
Expand All @@ -49,8 +54,10 @@ class TransportStub implements DataConnectTransport {
}

DataConnectTransport getTransport(
TransportOptions transportOptions,
DataConnectOptions options,
FirebaseAuth? auth,
FirebaseAppCheck? appCheck) =>
TransportStub(transportOptions, options, auth, appCheck);
TransportOptions transportOptions,
DataConnectOptions options,
CallerSDKType sdkType,
FirebaseAuth? auth,
FirebaseAppCheck? appCheck,
) =>
TransportStub(transportOptions, options, sdkType, auth, appCheck);
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ class MockFirebaseAuth extends Mock implements FirebaseAuth {}
class MockFirebaseAppCheck extends Mock implements FirebaseAppCheck {}

void main() {
group('GoogApiClient', () {
test('should return no codegen suffix if using core sdk', () {
final packageVersion = "1.0.0";
expect(getGoogApiVal(CallerSDKType.core, packageVersion),
'gl-dart/$packageVersion fire/$packageVersion');
});
test('should return codegen suffix if using gen sdk', () {
final packageVersion = "1.0.0";
expect(getGoogApiVal(CallerSDKType.generated, packageVersion),
'gl-dart/$packageVersion fire/$packageVersion dart/gen');
});
});
group('TransportOptions', () {
test('should properly initialize with given parameters', () {
final transportOptions = TransportOptions('localhost', 8080, true);
Expand Down Expand Up @@ -65,6 +77,7 @@ void main() {
transport = TestDataConnectTransport(
transportOptions,
dataConnectOptions,
CallerSDKType.core,
auth: mockFirebaseAuth,
appCheck: mockFirebaseAppCheck,
);
Expand Down Expand Up @@ -98,10 +111,10 @@ void main() {

// Test class extending DataConnectTransport for testing purposes
class TestDataConnectTransport extends DataConnectTransport {
TestDataConnectTransport(
TransportOptions transportOptions, DataConnectOptions options,
TestDataConnectTransport(TransportOptions transportOptions,
DataConnectOptions options, CallerSDKType sdkType,
{FirebaseAuth? auth, FirebaseAppCheck? appCheck})
: super(transportOptions, options) {
: super(transportOptions, options, sdkType) {
this.auth = auth;
this.appCheck = appCheck;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void main() {
'testConnector',
'testService',
),
CallerSDKType.core,
mockAuth,
mockAppCheck,
);
Expand All @@ -64,6 +65,7 @@ void main() {
'testConnector',
'testService',
),
CallerSDKType.core,
mockAuth,
mockAppCheck,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_data_connect/firebase_data_connect.dart';
import 'package:firebase_data_connect/src/network/transport_library.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:grpc/service_api.dart';
import 'package:mockito/mockito.dart';

// Create mock classes for FirebaseAuth, FirebaseAppCheck, and other dependencies.
Expand Down Expand Up @@ -36,6 +37,7 @@ void main() {
final transportStub = TransportStub(
mockTransportOptions,
mockDataConnectOptions,
CallerSDKType.core,
mockAuth,
mockAppCheck,
);
Expand All @@ -50,6 +52,7 @@ void main() {
final transportStub = TransportStub(
mockTransportOptions,
mockDataConnectOptions,
CallerSDKType.core,
mockAuth,
mockAppCheck,
);
Expand All @@ -69,6 +72,7 @@ void main() {
final transportStub = TransportStub(
mockTransportOptions,
mockDataConnectOptions,
CallerSDKType.core,
mockAuth,
mockAppCheck,
);
Expand Down
Loading