Skip to content

Commit

Permalink
Merge pull request #6007 from realm/tg/user-agent
Browse files Browse the repository at this point in the history
Add support for setting the user agent for sync requests
  • Loading branch information
tgoyne authored Nov 19, 2018
2 parents 14fb456 + c37fa9c commit 3fe88c2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
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
17 changes: 16 additions & 1 deletion 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 @@ -110,7 +114,13 @@ - (instancetype)initWithCustomRootDirectory:(NSURL *)rootDirectory {
bool should_encrypt = !getenv("REALM_DISABLE_METADATA_ENCRYPTION") && !RLMIsRunningInPlayground();
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);
@autoreleasepool {
bool isSwift = !!NSClassFromString(@"RealmSwiftObjectUtil");
auto userAgent = [[NSMutableString alloc] initWithFormat:@"Realm%@/%@",
isSwift ? @"Swift" : @"ObjectiveC", REALM_COCOA_VERSION];
SyncManager::shared().configure(rootDirectory.path.UTF8String, mode, RLMStringDataWithNSString(userAgent), none, true);
SyncManager::shared().set_user_agent(RLMStringDataWithNSString(self.appID));
}
return self;
}
return nil;
Expand All @@ -123,6 +133,11 @@ - (NSString *)appID {
return _appID;
}

- (void)setUserAgent:(NSString *)userAgent {
SyncManager::shared().set_user_agent(RLMStringDataWithNSString(userAgent));
_userAgent = userAgent;
}

#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

0 comments on commit 3fe88c2

Please sign in to comment.