Skip to content

Instantly share code, notes, and snippets.

@stillie
Created September 21, 2022 11:59
Show Gist options
  • Save stillie/562cb86b16345a382bf0edc1788f7521 to your computer and use it in GitHub Desktop.
Save stillie/562cb86b16345a382bf0edc1788f7521 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:firebase_remote_config/test_client.dart';
import 'package:flutter/services.dart';
import 'package:googleapis/firebaseremoteconfig/v1.dart';
import 'package:googleapis_auth/auth_io.dart';
import 'package:http/http.dart';
class RemoteConfigViewModel {
final projectName = "projects/fir-admin-control";
Future<void> init() async {
final serviceJson = await rootBundle.loadString(
"assets/fir-admin-control-firebase-adminsdk-hd8f2-1055f01308.json");
final accountConfig =
ServiceAccountCredentials.fromJson(jsonDecode(serviceJson));
final client = TestClient(currentVersion: '', projectNumber: '');
final httpClient = await clientViaServiceAccount(
accountConfig,
[FirebaseRemoteConfigApi.cloudPlatformScope],
baseClient: client,
);
final remoteConfigApi = FirebaseRemoteConfigApi(httpClient);
final projectRemoteConfig =
await remoteConfigApi.projects.getRemoteConfig(projectName);
if (projectRemoteConfig.version == null) return;
final projectVersion = projectRemoteConfig.version!;
final String dateTime = DateTime.now().toUtc().toIso8601String();
Version version = Version(
description: projectVersion.description,
isLegacy: projectVersion.isLegacy,
rollbackSource: projectVersion.rollbackSource,
updateOrigin: projectVersion.updateOrigin,
updateTime: dateTime,
updateType: "REMOTE_CONFIG_UPDATE_TYPE_UNSPECIFIED",
updateUser: RemoteConfigUser(email: "[email protected]", name: "Test"),);
final updateValue = RemoteConfig(
parameters: {
"test2": RemoteConfigParameter(
valueType: "STRING",
description: "test2 please",
defaultValue:
RemoteConfigParameterValue(value: "this is from ap123234"))
},
version: version,
);
try {
final response = await remoteConfigApi.projects
.updateRemoteConfig(updateValue, projectName);
print("success: $response");
} catch (e, s) {
print("$e: $s");
}
print(projectRemoteConfig.toJson());
print(serviceJson);
print(accountConfig.email);
}
}
import 'dart:io' as io;
import 'package:http/http.dart';
import 'package:http/io_client.dart' as http;
class TestClient extends BaseClient {
final String projectNumber;
final String currentVersion;
final io.HttpClient _ioHttpClient;
late http.IOClient _httpClient;
TestClient({required this.projectNumber, required this.currentVersion})
: _ioHttpClient = io.HttpClient() {
_httpClient = http.IOClient(_ioHttpClient);
}
@override
Future<StreamedResponse> send(BaseRequest request) {
request.headers
.putIfAbsent(
"If-Match",
() => "etag-$projectNumber-$currentVersion",
);
return _httpClient.send(request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment