From cd97306ad792d5aeb76caa22f04cbc72d4b79abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Fri, 18 Mar 2022 10:26:21 +0100 Subject: [PATCH] Added ApplicationConfiguration --- lib/src/application_configuration.dart | 52 +++++++++++++++++++++ lib/src/cli/metrics/metrics_command.dart | 4 +- lib/src/native/realm_core.dart | 57 +++++++++++++++++++++++- src/realm_dart.cpp | 1 + test/application_configuration_test.dart | 34 ++++++++++++++ 5 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 lib/src/application_configuration.dart create mode 100644 test/application_configuration_test.dart diff --git a/lib/src/application_configuration.dart b/lib/src/application_configuration.dart new file mode 100644 index 0000000000..9a11cfb38d --- /dev/null +++ b/lib/src/application_configuration.dart @@ -0,0 +1,52 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright 2021 Realm Inc. +// +// 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 'dart:io'; + +import 'package:meta/meta.dart'; +import 'package:package_config/package_config_types.dart'; +import 'package:pub_semver/pub_semver.dart'; + +import 'cli/common/utils.dart'; +import 'native/realm_core.dart'; + +@immutable +class ApplicationConfiguration { + final RealmAppConfigHandle _handle; + + final String appId; + final Uri? baseUrl; + final Duration? defaultRequestTimeout; + final String? localAppName; + final Version? localAppVersion; + + ApplicationConfiguration( + this.appId, { + this.baseUrl, + this.defaultRequestTimeout, + this.localAppName, + this.localAppVersion, + }) : _handle = realmCore.createAppConfig(appId, realmCore.createHttpTransport(HttpClient())) { + if (baseUrl != null) realmCore.setAppConfigBaseUrl(_handle, baseUrl!); + if (defaultRequestTimeout != null) realmCore.setAppConfigDefaultRequestTimeout(_handle, defaultRequestTimeout!); + if (localAppName != null) realmCore.setAppConfigLocalAppName(_handle, localAppName!); + if (localAppVersion != null) realmCore.setAppConfigLocalAppVersion(_handle, localAppVersion!); + realmCore.setAppConfigPlatform(_handle, Platform.operatingSystem); + realmCore.setAppConfigPlatformVersion(_handle, Platform.operatingSystemVersion); + realmCore.setAppConfigSdkVersion(_handle, Version.parse(Platform.version.takeUntil(' '))); + } +} diff --git a/lib/src/cli/metrics/metrics_command.dart b/lib/src/cli/metrics/metrics_command.dart index 1d9af251a2..3cba46b0fa 100644 --- a/lib/src/cli/metrics/metrics_command.dart +++ b/lib/src/cli/metrics/metrics_command.dart @@ -216,7 +216,7 @@ Future getInfo(Options options) async { // Sanity check full info, if we have it if (info != null && (version == null || version == info.frameworkVersion) && flutterVersionConstraints.allows(info.frameworkVersion)) { // The returned info match both the projects constraints and the - // flutter version of the lastest flutter command run on the project + // flutter version of the latest flutter command run on the project return info; } @@ -224,6 +224,6 @@ Future getInfo(Options options) async { // secondly the min constraint of the flutter SDK used return FlutterInfo( frameworkVersion: version ?? (await safe(() => (flutterVersionConstraints as VersionRange).min!)) ?? Version.none, - dartSdkVersion: Version.parse(Platform.version.toString().takeUntil(' ')), + dartSdkVersion: Version.parse(Platform.version.takeUntil(' ')), ); } diff --git a/lib/src/native/realm_core.dart b/lib/src/native/realm_core.dart index d7e41fe3af..a360180bdb 100644 --- a/lib/src/native/realm_core.dart +++ b/lib/src/native/realm_core.dart @@ -26,6 +26,7 @@ import 'dart:typed_data'; // Hide StringUtf8Pointer.toNativeUtf8 and StringUtf16Pointer since these allows silently allocating memory. Use toUtf8Ptr instead import 'package:ffi/ffi.dart' hide StringUtf8Pointer, StringUtf16Pointer; +import 'package:pub_semver/pub_semver.dart'; import '../collections.dart'; import '../configuration.dart'; @@ -187,7 +188,8 @@ class _RealmCore { } static void request_callback(Object userData, realm_http_request request, Pointer request_context) { - () async { // TODO: Use another isolate, perhaps? + () async { + // TODO: Use another isolate, perhaps? final client = userData as HttpClient; client.connectionTimeout = Duration(milliseconds: request.timeout_ms); try { @@ -709,6 +711,55 @@ class _RealmCore { return out_modified.asTypedList(count).toList(); }); } + + RealmAppConfigHandle createAppConfig(String appId, RealmHttpTransportHandle httpTransport) { + return using((arena) { + final app_id = appId.toUtf8Ptr(arena); + return RealmAppConfigHandle._(_realmLib.realm_app_config_new(app_id, httpTransport._pointer)); + }); + } + + void setAppConfigBaseUrl(RealmAppConfigHandle handle, Uri baseUrl) { + using((arena) { + _realmLib.realm_app_config_set_base_url(handle._pointer, baseUrl.toString().toUtf8Ptr(arena)); + }); + } + + void setAppConfigDefaultRequestTimeout(RealmAppConfigHandle handle, Duration defaultRequestTimeout) { + _realmLib.realm_app_config_set_default_request_timeout(handle._pointer, defaultRequestTimeout.inMilliseconds); + } + + void setAppConfigLocalAppName(RealmAppConfigHandle handle, String localAppName) { + using((arena) { + _realmLib.realm_app_config_set_local_app_name(handle._pointer, localAppName.toUtf8Ptr(arena)); + }); + } + + void setAppConfigLocalAppVersion(RealmAppConfigHandle handle, Version localAppVersion) { + using((arena) { + final versionString = localAppVersion.toString(); + _realmLib.realm_app_config_set_local_app_version(handle._pointer, versionString.toUtf8Ptr(arena)); + }); + } + + void setAppConfigPlatform(RealmAppConfigHandle handle, String platform) { + using((arena) { + _realmLib.realm_app_config_set_platform(handle._pointer, platform.toUtf8Ptr(arena)); + }); + } + + void setAppConfigPlatformVersion(RealmAppConfigHandle handle, String platformVersion) { + using((arena) { + _realmLib.realm_app_config_set_platform_version(handle._pointer, platformVersion.toUtf8Ptr(arena)); + }); + } + + void setAppConfigSdkVersion(RealmAppConfigHandle handle, Version sdkVersion) { + using((arena) { + final versionString = sdkVersion.toString(); + _realmLib.realm_app_config_set_sdk_version(handle._pointer, versionString.toUtf8Ptr(arena)); + }); + } } class LastError { @@ -805,6 +856,10 @@ class RealmHttpTransportHandle extends Handle { RealmHttpTransportHandle._(Pointer pointer) : super(pointer, 256); // TODO; What should hint be? } +class RealmAppConfigHandle extends Handle { + RealmAppConfigHandle._(Pointer pointer) : super(pointer, 256); // TODO: What should hint be? +} + extension on List { Pointer toInt8Ptr(Allocator allocator) { final nativeStringSize = length + 1; diff --git a/src/realm_dart.cpp b/src/realm_dart.cpp index e6f12263ff..089a22d9b4 100644 --- a/src/realm_dart.cpp +++ b/src/realm_dart.cpp @@ -70,6 +70,7 @@ void dummy(void) { realm_results_add_notification_callback(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr); realm_results_snapshot(nullptr); realm_http_transport_new(nullptr, nullptr, nullptr); + realm_app_config_new(nullptr, nullptr); #if (ANDROID) realm_android_dummy(); #endif diff --git a/test/application_configuration_test.dart b/test/application_configuration_test.dart new file mode 100644 index 0000000000..7ad98ab12e --- /dev/null +++ b/test/application_configuration_test.dart @@ -0,0 +1,34 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright 2022 Realm Inc. +// +// 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. +// +//////////////////////////////////////////////////////////////////////////////// + +// ignore_for_file: unused_local_variable +import 'dart:io'; +import 'package:realm_dart/src/application_configuration.dart'; +import 'package:test/test.dart' hide test, throws; +import '../lib/realm.dart'; +import 'test.dart'; + +Future main([List? args]) async { + print("Current PID $pid"); + + setupTests(args); + + test('ApplicatonConfiguration can be created', () { + ApplicationConfiguration('foo'); + }); +} \ No newline at end of file