Browse Source

nextcloud: Add user-agent override

pull/25/head
jld3103 2 years ago
parent
commit
e6d6dacad1
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 28
      packages/nextcloud/lib/src/client.dart
  2. 11
      packages/nextcloud/test/client_test.dart
  3. 6
      packages/nextcloud/test/helper.dart

28
packages/nextcloud/lib/src/client.dart

@ -9,7 +9,7 @@ class NextcloudClient {
this.password,
this.language,
this.appType = AppType.unknown,
this.userAgentSuffix,
this.userAgentOverride,
}) {
final authentication = username != null && password != null
? HttpBasicAuth(username: username!, password: password!)
@ -55,17 +55,13 @@ class NextcloudClient {
}
/// Headers that should be used for all requests
late final commonHeaders = () {
// ignore: no_leading_underscores_for_local_identifiers
final _userAgent = userAgent;
return <String, String>{
late final commonHeaders = <String, String>{
'OCS-APIRequest': 'true',
'Accept': 'application/json',
if (_userAgent != null) ...{
'User-Agent': _userAgent,
}
if (userAgent != null) ...{
'User-Agent': userAgent!,
},
};
}();
T _addCommonSettings<T extends BaseApiClient>(final T apiClient) {
var newApiClient = apiClient;
@ -91,17 +87,11 @@ class NextcloudClient {
/// App type the client will register as. Only relevant for notifications. See [AppType] for explanations.
final AppType appType;
/// Will be appended to the user-agent from [appType]
final String? userAgentSuffix;
/// User-agent made up from the user-agent from [appType] and the [userAgentSuffix]
String? get userAgent {
if (appType.userAgent != null || userAgentSuffix != null) {
return [appType.userAgent, userAgentSuffix].where((final a) => a != null).join();
}
/// Overrides the user-agent set by [appType]
final String? userAgentOverride;
return null;
}
/// User-agent made up from the user-agent from [userAgentOverride] and the [appType]
late String? userAgent = userAgentOverride ?? appType.userAgent;
NextcloudWebDAVClient? _webdav;

11
packages/nextcloud/test/client_test.dart

@ -21,18 +21,9 @@ Future main() async {
test('User-Agent with suffix', () async {
client = await TestHelper.getPreparedClient(
dockerImageName,
userAgentSuffix: 'test',
userAgentOverride: 'test',
);
expect(client.userAgent, 'test');
});
test('User-Agent from AppType with suffix', () async {
client = await TestHelper.getPreparedClient(
dockerImageName,
appType: AppType.nextcloud,
userAgentSuffix: ' test',
);
expect(client.userAgent, '${AppType.nextcloud.userAgent} test');
});
});
}

6
packages/nextcloud/test/helper.dart

@ -21,7 +21,7 @@ class TestNextcloudClient extends NextcloudClient {
super.password,
super.language,
super.appType,
super.userAgentSuffix,
super.userAgentOverride,
});
final String containerID;
@ -138,7 +138,7 @@ class TestHelper {
final String? password = defaultPassword,
final bool useAppPassword = false,
final AppType appType = AppType.unknown,
final String? userAgentSuffix,
final String? userAgentOverride,
}) async {
// ignore: prefer_asserts_with_message
assert(!useAppPassword || (username != null && password != null));
@ -198,7 +198,7 @@ class TestHelper {
username: username,
password: clientPassword,
appType: appType,
userAgentSuffix: userAgentSuffix,
userAgentOverride: userAgentOverride,
);
var iteration = 0;

Loading…
Cancel
Save