Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting the user agent for sync requests #6007

Merged
merged 3 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
x.y.z Release notes (yyyy-MM-dd)
=============================================================
### Enhancements
* None.
* Add a User-Agent header to HTTP requests made to the Realm Object Server. By
default, this contains information about the Realm library version and your
app's bundle ID. The application identifier can be customized by setting
`RLMSyncManager.sharedManager.userAgent`/`SyncManager.shared.userAgent` prior
to opening a synchronized Realm.
(PR: https://github.com/realm/realm-cocoa/pull/6007).

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
Expand Down
1 change: 1 addition & 0 deletions Realm/ObjectServerTests/RLMSyncTestCase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ - (void)setUp {
withIntermediateDirectories:YES attributes:nil error:&error];
s_managerForTest = [[RLMSyncManager alloc] initWithCustomRootDirectory:clientDataRoot];
[RLMSyncManager sharedManager].logLevel = RLMSyncLogLevelOff;
[RLMSyncManager sharedManager].userAgent = self.name;
}

- (void)tearDown {
Expand Down
14 changes: 12 additions & 2 deletions Realm/RLMSyncManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,21 @@ typedef void(^RLMSyncErrorReportingBlock)(NSError *, RLMSyncSession * _Nullable)
@property (nullable, nonatomic, copy) RLMSyncErrorReportingBlock errorHandler;

/**
A reverse-DNS string uniquely identifying this application. In most cases this is automatically set by the SDK, and
does not have to be explicitly configured.
A reverse-DNS string uniquely identifying this application. In most cases this
is automatically set by the SDK, and does not have to be explicitly configured.
*/
@property (nonatomic, copy) NSString *appID;

/**
A string identifying this application which is included in the User-Agent
header of sync connections. By default, this will be the application's bundle
identifier.

This property must be set prior to opening a synchronized Realm for the first
time. Any modifications made after opening a Realm will be ignored.
*/
@property (nonatomic, copy) NSString *userAgent;

/**
The logging threshold which newly opened synced Realms will use. Defaults to
`RLMSyncLogLevelInfo`.
Expand Down
16 changes: 16 additions & 0 deletions Realm/RLMSyncManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#import "sync/sync_manager.hpp"
#import "sync/sync_session.hpp"

#if !defined(REALM_COCOA_VERSION)
#import "RLMVersion.h"
#endif

using namespace realm;
using Level = realm::util::Logger::Level;

Expand Down Expand Up @@ -111,6 +115,7 @@ - (instancetype)initWithCustomRootDirectory:(NSURL *)rootDirectory {
auto mode = should_encrypt ? SyncManager::MetadataMode::Encryption : SyncManager::MetadataMode::NoEncryption;
rootDirectory = rootDirectory ?: [NSURL fileURLWithPath:RLMDefaultDirectoryForBundleIdentifier(nil)];
SyncManager::shared().configure(rootDirectory.path.UTF8String, mode, "", none, true);
tgoyne marked this conversation as resolved.
Show resolved Hide resolved
self.userAgent = self.appID;
return self;
}
return nil;
Expand All @@ -123,6 +128,17 @@ - (NSString *)appID {
return _appID;
}

- (void)setUserAgent:(NSString *)userAgent {
bool isSwift = !!NSClassFromString(@"RealmSwiftObjectUtil");
auto fullUserAgent = [[NSMutableString alloc] initWithFormat:@"Realm %@/%@",
isSwift ? @"Swift" : @"Objective C", REALM_COCOA_VERSION];
if (userAgent.length > 0) {
[fullUserAgent appendFormat:@" %@", userAgent];
}
SyncManager::shared().set_user_agent(RLMStringDataWithNSString(fullUserAgent));
_userAgent = userAgent;
tgoyne marked this conversation as resolved.
Show resolved Hide resolved
}

#pragma mark - Passthrough properties

- (RLMSyncLogLevel)logLevel {
Expand Down
2 changes: 1 addition & 1 deletion dependencies.list
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=3.11.2
REALM_CORE_VERSION=5.12.1
REALM_SYNC_VERSION=3.13.3
REALM_OBJECT_SERVER_VERSION=3.11.5
REALM_OBJECT_SERVER_VERSION=3.13.1