Skip to content

Commit

Permalink
Added ApplicationConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Apr 3, 2022
1 parent 8b4e5e5 commit 251229d
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 2 deletions.
79 changes: 79 additions & 0 deletions lib/src/application_configuration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////
import 'dart:io';

import 'package:meta/meta.dart';
import 'package:pub_semver/pub_semver.dart';

enum MetadataPersistenceMode {
plainText,
encrypted,
}

@immutable
class ApplicationConfiguration {
/// The [appId] is the unique id that identifies the Realm application.
final String appId;

/// The [baseFilePath] is the [Directory] relative to which all local data for this application will be stored.
/// This data includes metadata for users and synchronized Realms.
final Directory? baseFilePath;

/// The [baseUrl] is the [Uri] used to reach the MongoDB Realm server.
/// [baseUrl] only needs to be set if for some reason your application isn't hosted on realm.mongodb.com.
/// This can be the case if you're testing locally or are using a pre-production environment.
final Uri? baseUrl;

/// The [defaultRequestTimeout] for HTTP requests performed as part of authentication.
final Duration? defaultRequestTimeout;

/// The [localAppName] is the friendly name identifying the current client application.
/// This is typically used to differentiate between client applications that use the same
/// MongoDB Realm app. These can be the same conceptual app developed for different platforms, or
/// significantly different client side applications that operate on the same data - e.g. an event managing
/// service that has different clients apps for organizers and attendees.
final String? localAppName;

/// The [localAppVersion]
final Version? localAppVersion;

final MetadataPersistenceMode metadataPersistenceMode;

/// The encryption key for user metadata on this device.
/// This will not change the encryption key for individual Realms, which is set in the [Configuration]
final List<int>? metadataEncryptionKey;

/// The [HttpClient] that will be used for HTTP requests during authentication.
/// You can use this to override the default http client handler and configure settings like proxies,
/// client certificates, and cookies. While these are not required to connect to MongoDB Realm under
/// normal circumstances, they can be useful if client devices are behind corporate firewall or use
/// a more complex networking setup.
final HttpClient httpClient;

ApplicationConfiguration(
this.appId, {
this.baseUrl,
this.baseFilePath,
this.defaultRequestTimeout,
this.localAppName,
this.localAppVersion,
this.metadataPersistenceMode = MetadataPersistenceMode.plainText,
this.metadataEncryptionKey,
HttpClient? httpClient,
}) : httpClient = httpClient ?? HttpClient();
}
4 changes: 2 additions & 2 deletions lib/src/cli/metrics/metrics_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ Future<FlutterInfo?> 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;
}

// Fallback to simplified info build from the version read from .dart_tool/version,
// 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(' ')),
);
}
33 changes: 33 additions & 0 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ 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 '../application_configuration.dart';
import '../collections.dart';
import '../configuration.dart';
import '../init.dart';
Expand Down Expand Up @@ -809,6 +811,33 @@ class _RealmCore {
RealmAppCredentialsHandle createAppCredentialsAnonymous() {
return RealmAppCredentialsHandle._(_realmLib.realm_app_credentials_new_anonymous());
}

AppConfigHandle createAppConfig(ApplicationConfiguration configuration, RealmHttpTransportHandle httpTransport) {
return using((arena) {
final c = configuration;
final app_id = c.appId.toUtf8Ptr(arena);
final handle = AppConfigHandle._(_realmLib.realm_app_config_new(app_id, httpTransport._pointer));
if (c.baseUrl != null) {
_realmLib.realm_app_config_set_base_url(handle._pointer, c.baseUrl.toString().toUtf8Ptr(arena));
}
if (c.defaultRequestTimeout != null) {
_realmLib.realm_app_config_set_default_request_timeout(handle._pointer, c.defaultRequestTimeout!.inMilliseconds);
}
if (c.localAppName != null) {
_realmLib.realm_app_config_set_local_app_name(handle._pointer, c.localAppName!.toUtf8Ptr(arena));
}
if (c.localAppVersion != null) {
final versionString = c.localAppVersion.toString();
_realmLib.realm_app_config_set_local_app_version(handle._pointer, versionString.toUtf8Ptr(arena));
}
_realmLib.realm_app_config_set_platform(handle._pointer, Platform.operatingSystem.toUtf8Ptr(arena));
_realmLib.realm_app_config_set_platform_version(handle._pointer, Platform.operatingSystemVersion.toUtf8Ptr(arena));
final version = Version.parse(Platform.version);
_realmLib.realm_app_config_set_sdk_version(handle._pointer, version.toString().toUtf8Ptr(arena));

return handle;
});
}
}

class LastError {
Expand Down Expand Up @@ -909,6 +938,10 @@ class RealmHttpTransportHandle extends Handle<realm_http_transport> {
RealmHttpTransportHandle._(Pointer<realm_http_transport> pointer) : super(pointer, 256); // TODO; What should hint be?
}

class AppConfigHandle extends Handle<realm_app_config> {
AppConfigHandle._(Pointer<realm_app_config> pointer) : super(pointer, 256); // TODO: What should hint be?
}

extension on List<int> {
Pointer<Int8> toInt8Ptr(Allocator allocator) {
final nativeSize = length + 1;
Expand Down
1 change: 1 addition & 0 deletions lib/src/realm_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'realm_object.dart';
import 'results.dart';

// always expose with `show` to explicitly control the public API surface
export "application_configuration.dart" show ApplicationConfiguration;
export 'package:realm_common/realm_common.dart'
show Ignored, Indexed, MapTo, PrimaryKey, RealmError, RealmModel, RealmUnsupportedSetError, RealmStateError, RealmCollectionType, RealmPropertyType;
export "configuration.dart" show Configuration, RealmSchema, SchemaObject;
Expand Down
1 change: 1 addition & 0 deletions src/realm_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void dummy(void) {
gc_handle_new(nullptr);
gc_handle_deref(nullptr);
realm_http_transport_new(nullptr, nullptr, nullptr);
realm_app_config_new(nullptr, nullptr);
#if (ANDROID)
realm_android_dummy();
#endif
Expand Down
39 changes: 39 additions & 0 deletions test/application_configuration_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
////////////////////////////////////////////////////////////////////////////////
import 'dart:io';

import 'package:pub_semver/pub_semver.dart';

import '../lib/realm.dart';
import 'test.dart';

Future<void> main([List<String>? args]) async {
print("Current PID $pid");

setupTests(args);

test('ApplicationConfiguration can be created', () {
ApplicationConfiguration(
'foo',
baseUrl: Uri.parse('https://not_re.al'),
defaultRequestTimeout: const Duration(seconds: 2),
localAppName: 'bar',
localAppVersion: Version(1, 0, 0),
);
});
}

0 comments on commit 251229d

Please sign in to comment.