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

feat: get session_id #385

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 17 additions & 9 deletions Sources/Classes/Headers/Public/RSClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ NS_ASSUME_NONNULL_BEGIN
@class RSContext;
@class RSMessageBuilder;

@interface RSClient : NSObject
@interface RSClient : NSObject {
RSOption *_options;
}

- (instancetype)init NS_UNAVAILABLE NS_SWIFT_UNAVAILABLE("Use `RSClient.getInstance(writeKey:)` to initialise.");

+ (instancetype) getInstance;
+ (instancetype) getInstance:(NSString*) writeKey;
+ (instancetype) getInstance:(NSString*) writeKey config:(RSConfig*) config;
Expand Down Expand Up @@ -64,18 +69,10 @@ NS_ASSUME_NONNULL_BEGIN

- (void) shutdown;

- (NSString* _Nullable)getAnonymousId;

- (RSConfig* _Nullable)configuration;

+ (instancetype _Nullable) sharedInstance;

+ (RSOption*) getDefaultOptions;

- (void)trackLifecycleEvents:(NSDictionary *)launchOptions;

- (RSContext *) getContext;

+ (void) putAnonymousId: (NSString *_Nonnull) anonymousId;
+ (void) putDeviceToken: (NSString *_Nonnull) deviceToken;
+ (void) putAuthToken: (NSString *_Nonnull) authToken;
Expand All @@ -86,6 +83,17 @@ NS_ASSUME_NONNULL_BEGIN
- (void)startSession:(long)sessionId;
- (void)endSession;

- (NSString* _Nullable)getAnonymousId __attribute((deprecated("This method will be deprecated soon. Use instance property(anonymousId) instead.")));
- (RSConfig* _Nullable)configuration __attribute((deprecated("This method will be deprecated soon. Use instance property(config) instead.")));
+ (RSOption*) getDefaultOptions __attribute((deprecated("This method will be deprecated soon. Use instance property(defaultOptions) instead.")));
- (RSContext *) getContext __attribute((deprecated("This method will be deprecated soon. Use instance property(context) instead.")));

@property (strong, nonatomic, readonly) NSNumber* _Nullable sessionId;
@property (strong, nonatomic, readonly) NSString* _Nullable anonymousId;
@property (strong, nonatomic, readonly) RSConfig* _Nullable config;
@property (strong, nonatomic, readonly) RSOption* defaultOptions;
@property (strong, nonatomic, readonly) RSContext* context;

@end

NS_ASSUME_NONNULL_END
4 changes: 3 additions & 1 deletion Sources/Classes/Headers/Public/RSEventRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ NS_ASSUME_NONNULL_BEGIN
dispatch_source_t source;
dispatch_queue_t repositoryQueue;
RSClient *client;
RSOption *defaultOptions;
}

+ (instancetype)initiate:(NSString*)writeKey config:(RSConfig*)config client:(RSClient *)client;
+ (instancetype)initiate:(NSString*)writeKey config:(RSConfig*)config client:(RSClient *)client options:(RSOption * __nullable)options;
+ (instancetype) getInstance;
- (void) setAnonymousIdToken;
- (void) dump:(RSMessage*) message;
Expand All @@ -69,6 +70,7 @@ NS_ASSUME_NONNULL_BEGIN

- (void) startSession:(long) sessionId;
- (void) endSession;
- (NSNumber * _Nullable)getSessionId;

- (RSConfig* _Nullable) getConfig;
- (void)applicationDidFinishLaunchingWithOptions:(NSDictionary *) launchOptions;
Expand Down
42 changes: 31 additions & 11 deletions Sources/Classes/RSClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ + (instancetype)initiate:(NSString *)writeKey config:(RSConfig * __nullable)conf
_instance = [[self alloc] init];
if (options != nil) {
_defaultOptions = options;
_instance->_options = options;
}
RSConfig *_config = (config != nil) ? config : [[RSConfig alloc] init];
_repository = [RSEventRepository initiate:writeKey config:_config client:_instance];
_repository = [RSEventRepository initiate:writeKey config:_config client:_instance options:options];
if(_deviceToken != nil && [_deviceToken length] != 0) {
[[_instance getContext] putDeviceToken:_deviceToken];
[_instance.context putDeviceToken:_deviceToken];
}
});
}
Expand Down Expand Up @@ -322,17 +323,17 @@ - (void)identify:(NSString *)userId traits:(NSDictionary *)traits options:(RSOpt
}

- (void)reset {
[RSElementCache reset];
if (_repository != nil) {
[_repository reset];
}
[self reset:NO];
}

- (void) reset:(BOOL) clearAnonymousId {
if(clearAnonymousId) {
[[RSPreferenceManager getInstance] refreshAnonymousId];
}
[self reset];
[RSElementCache reset];
if (_repository != nil) {
[_repository reset];
}
}

- (void)flush {
Expand Down Expand Up @@ -375,21 +376,28 @@ - (void)shutdown {
}

- (NSString*)getAnonymousId {
return self.anonymousId;
}

- (NSString *)anonymousId {
if ([RSClient getOptStatus]) {
return nil;
}
// returns anonymousId
return [RSElementCache getAnonymousId];
}

- (RSContext*) getContext {
- (RSContext *)context {
if ([RSClient getOptStatus]) {
return nil;
}
return [RSElementCache getContext];
}

- (RSConfig*)configuration {
- (RSContext*) getContext {
return self.context;
}

- (RSConfig *)config {
if (_repository == nil) {
return nil;
}
Expand All @@ -399,6 +407,10 @@ - (RSConfig*)configuration {
return [_repository getConfig];
}

- (RSConfig*)configuration {
return self.config;
}

- (void)trackLifecycleEvents:(NSDictionary *)launchOptions {
if ([RSClient getOptStatus]) {
return;
Expand All @@ -414,6 +426,10 @@ + (RSOption*) getDefaultOptions {
return _defaultOptions;
}

- (RSOption *)defaultOptions {
return self->_options;
}

+ (void)setAnonymousId: (NSString *__nullable) anonymousId {
[self putAnonymousId:anonymousId];
}
Expand Down Expand Up @@ -446,7 +462,7 @@ + (void)putDeviceToken:(NSString *_Nonnull)deviceToken {
_deviceToken = deviceToken;
return;
}
[[_instance getContext] putDeviceToken:deviceToken];
[_instance.context putDeviceToken:deviceToken];
}
}

Expand Down Expand Up @@ -486,4 +502,8 @@ - (void)endSession {
}
}

- (NSNumber * _Nullable)sessionId {
return (_repository != nil) ? [_repository getSessionId] : nil;
}

@end
12 changes: 8 additions & 4 deletions Sources/Classes/RSEventRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
static RSEventRepository* _instance;
@implementation RSEventRepository

+ (instancetype)initiate:(NSString *)writeKey config:(RSConfig *)config client:(RSClient *)client {
+ (instancetype)initiate:(NSString *)writeKey config:(RSConfig *)config client:(RSClient *)client options:(RSOption * __nullable)options {
if (_instance == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init:writeKey config:config client:client];
_instance = [[self alloc] init:writeKey config:config client:client options:options];
});
}
return _instance;
Expand All @@ -42,7 +42,7 @@ + (instancetype) getInstance {
* 11.Initiate RSBackGroundModeManager
* 12.Initiate RSApplicationLifeCycleManager
* */
- (instancetype)init:(NSString*)_writeKey config:(RSConfig*)_config client:(RSClient *)_client {
- (instancetype)init:(NSString*)_writeKey config:(RSConfig*)_config client:(RSClient *)_client options:(RSOption * __nullable)_options {
self = [super init];
if (self) {
[RSLogger logDebug:[[NSString alloc] initWithFormat:@"EventRepository: writeKey: %@", _writeKey]];
Expand All @@ -53,6 +53,7 @@ - (instancetype)init:(NSString*)_writeKey config:(RSConfig*)_config client:(RSCl

self->writeKey = _writeKey;
self->config = _config;
self->defaultOptions = _options;

self->authToken = [RSUtils getBase64EncodedString: [[NSString alloc] initWithFormat:@"%@:", self->writeKey]];

Expand Down Expand Up @@ -225,7 +226,7 @@ - (void) dump:(RSMessage *)message {
return;
}
});
[self applyIntegrations:message withDefaultOption:RSClient.getDefaultOptions];
[self applyIntegrations:message withDefaultOption:self->defaultOptions];
message = [self applyConsents:message];
[self applySession:message withUserSession:userSession andRudderConfig:config];

Expand Down Expand Up @@ -349,5 +350,8 @@ - (void) endSession {
[self->userSession clearSession];
}

- (NSNumber * _Nullable)getSessionId {
return [self->userSession getSessionId];
}

@end
2 changes: 1 addition & 1 deletion Tests/RudderUtilsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class RudderUtilsTest: XCTestCase {
"company" : "RudderStack",
"city" : "Hyderabad"]

let response:Any = RSUtils.deSerializeJSONString(jsonString)
let response = RSUtils.deSerializeJSONString(jsonString)
let parsedDict:[String:String]? = response as? [String:String] ?? nil
XCTAssert(parsedDict == dictObj)

Expand Down
49 changes: 49 additions & 0 deletions Tests/UserSessionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// UserSessionTests.swift
// Rudder
//
// Created by Pallab Maiti on 06/09/23.
//

import XCTest
@testable import Rudder

final class UserSessionTests: XCTestCase {

var userSession: RSUserSession!

override func setUp() {
super.setUp()
let preferenceManager = RSPreferenceManager.getInstance()
userSession = RSUserSession.initiate(10, with: preferenceManager)
}

override func tearDown() {
super.tearDown()
}

func test_startSession() {
userSession.start()

XCTAssertEqual(userSession.getId(), RSPreferenceManager.getInstance().getSessionId())
userSession.clear()
}

func test_startSessionWithId() {
userSession.start(12345678)

XCTAssertEqual(userSession.getId(), 12345678)
XCTAssertEqual(RSPreferenceManager.getInstance().getSessionId(), 12345678)
userSession.clear()
}

func test_clearSession() {
userSession.start()

XCTAssertEqual(userSession.getId(), RSPreferenceManager.getInstance().getSessionId())
userSession.clear()

XCTAssertNil(userSession.getId())
XCTAssertNil(RSPreferenceManager.getInstance().getSessionId())
}
}
Loading