Skip to content

Commit

Permalink
Added ApplicationConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Mar 24, 2022
1 parent 9ed782c commit a197109
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 3 deletions.
52 changes: 52 additions & 0 deletions lib/src/application_configuration.dart
Original file line number Diff line number Diff line change
@@ -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(' ')));
}
}
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(' ')),
);
}
57 changes: 56 additions & 1 deletion lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -187,7 +188,8 @@ class _RealmCore {
}

static void request_callback(Object userData, realm_http_request request, Pointer<Void> 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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -805,6 +856,10 @@ class RealmHttpTransportHandle extends Handle<realm_http_transport> {
RealmHttpTransportHandle._(Pointer<realm_http_transport> pointer) : super(pointer, 256); // TODO; What should hint be?
}

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

extension on List<int> {
Pointer<Int8> toInt8Ptr(Allocator allocator) {
final nativeStringSize = length + 1;
Expand Down
34 changes: 34 additions & 0 deletions test/application_configuration_test.dart
Original file line number Diff line number Diff line change
@@ -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<void> main([List<String>? args]) async {
print("Current PID $pid");

setupTests(args);

test('ApplicatonConfiguration can be created', () {
ApplicationConfiguration('foo');
});
}

0 comments on commit a197109

Please sign in to comment.