Added support for Target session Id setter & getter#115
Added support for Target session Id setter & getter#115swarna04 merged 5 commits intoadobe:dev-v3.2.0from
Conversation
| /// - Parameter id: a string containing the value of the Target session Id to be set in the SDK. | ||
| static func setSessionId(_ id: String?) { | ||
| let eventData = [TargetConstants.EventDataKeys.TARGET_SESSION_ID: id ?? ""] | ||
| let event = Event(name: TargetConstants.EventName.REQUEST_IDENTITY, type: EventType.target, source: EventSource.requestIdentity, data: eventData) |
There was a problem hiding this comment.
it would be nice to have the eventName be more evident, we can call them
"Set Target Session Id" rather than "TargetRequestIdentity" for all the API's
Griffon debugging would be easier
| /// | ||
| /// - Parameter completion: the callback `closure` invoked with the current session Id or `nil` if there was an error retrieving it. | ||
| static func getSessionId(_ completion: @escaping (String?, Error?) -> Void) { | ||
| let event = Event(name: TargetConstants.EventName.REQUEST_IDENTITY, type: EventType.target, source: EventSource.requestIdentity, data: nil) |
There was a problem hiding this comment.
Similar to above - EventName can be "Get Target Session Identifier" rather than "TargetRequestIdentity"
There was a problem hiding this comment.
Yeah, I'd planned to clean up this code later but since you've mentioned I'll take it up here.
| /// persisted for a period defined by `target.sessionTimeout` configuration setting. | ||
| /// | ||
| /// - Parameter completion: the callback `closure` invoked with the current session Id or `nil` if there was an error retrieving it. | ||
| static func getSessionId(_ completion: @escaping (String?, Error?) -> Void) { |
There was a problem hiding this comment.
At some point, we should consider using Swift Result's as opposed to callback with optional String and Error
Docs:
https://www.hackingwithswift.com/articles/161/how-to-use-result-in-swift
https://developer.apple.com/documentation/swift/result
There was a problem hiding this comment.
Sure, it's probably a more elegant way to return multiple parameters in asynchronous APIs. I believe it does have some drawbacks in terms of Objective-C compatibility, however we could definitely explore more here.
| /// This ID is preserved between app upgrades, is saved and restored during the standard application | ||
| /// backup process, and is removed at uninstall, upon privacy status update to opt out or when AEPTarget.resetExperience is called. | ||
| /// | ||
| /// - Parameter id: a string containing the value of the Target session Id to be set in the SDK. |
There was a problem hiding this comment.
We can also mention in the docs that an empty or nil sessionId value will reset the target session
| setThirdPartyId(thirdPartyId: thirdPartyId, event: event) | ||
| } else { | ||
| dispatchRequestIdentityResponse(triggerEvent: event) | ||
| if let eventData = event.data { |
There was a problem hiding this comment.
A handful of functions in this file are missing docs.. May be we can update them in a later PR or have a JIRA created for them
There was a problem hiding this comment.
I'll create a JIRA ticket to update any missing docs for existing methods outside of this work.
AEPTarget/Sources/Target.swift
Outdated
| } | ||
|
|
||
| if sessionId != targetState.storedSessionId { | ||
| Log.trace(label: Target.LOG_TAG, "setSessionId - Updated Target session Id with the provided value \(sessionId).") |
There was a problem hiding this comment.
Can this be Log.debug as well ?
AEPTarget/Sources/TargetState.swift
Outdated
| } | ||
|
|
||
| private(set) var storedSessionId: String | ||
| #if DEBUG |
There was a problem hiding this comment.
just checking:
Wondering if we can remove the if DEBUG flag and keep setting storedSessionId as still private
Aaand for the Functional Test, we can test setSessionId and GetSessionID together.. so we don't have to mock targetState.sessionId
There was a problem hiding this comment.
Yup. I've revisited this code since we need to be able to update the sessionId from Target extension class as well. In summary, exposed another method in state that allows updating (and resetting) the session Id.
Codecov Report
@@ Coverage Diff @@
## dev-v3.2.0 #115 +/- ##
==============================================
- Coverage 90.40% 90.13% -0.27%
==============================================
Files 24 24
Lines 1261 1307 +46
==============================================
+ Hits 1140 1178 +38
- Misses 121 129 +8 |
sbenedicadb
left a comment
There was a problem hiding this comment.
looks good - a couple of small changes requested.
can we also update the objc test app with these new apis?
| guard let responseEvent = responseEvent else { | ||
| let error = "Request to get Target session Id failed with error, \(TargetError.ERROR_TIMEOUT)" | ||
| completion(nil, TargetError(message: error)) | ||
| Log.error(label: Target.LOG_TAG, error) |
There was a problem hiding this comment.
error is too aggressive for these logs - we should scale these back to warnings
| Log.error(label: Target.LOG_TAG, error) | ||
| return | ||
| } | ||
| guard let sessionId = eventData[TargetConstants.EventDataKeys.TARGET_SESSION_ID] as? String else { |
There was a problem hiding this comment.
isn't it valid for there to be no session? i'm thinking this case should be handled differently.
There was a problem hiding this comment.
There will always be a Target session (and a session Id). What scenario do you have in mind?
There was a problem hiding this comment.
prior to using target for the first time the session id is nil right?
also wondered if this getter should take timeout into consideration. i.e. - if there is a session id but it's expired, should calling this getter return nil?
There was a problem hiding this comment.
Yes, the session Id is generated upon the first Target request. Also, if the session has expired, the subsequent get call will return the new session Id generated by the SDK. This session Id is then used in all future Target requests and can also be passed to another channel to have the same session across.
AEPTarget/Sources/Target.swift
Outdated
| /// - Parameters: | ||
| /// - sessionId: new session Id that needs to be set in the SDK | ||
| private func setSessionId(sessionId: String) { | ||
| if targetState.privacyStatusIsOptOut { |
There was a problem hiding this comment.
given this is a block that returns, it should be switched to use the guard/else pattern
| private(set) var storedSessionId: String { | ||
| didSet { | ||
| if storedSessionId.isEmpty { | ||
| dataStore.remove(key: TargetConstants.DataStoreKeys.SESSION_ID) | ||
| } else { | ||
| dataStore.set(key: TargetConstants.DataStoreKeys.SESSION_ID, value: storedSessionId) | ||
| } | ||
| } | ||
| } |
The objC test app is updated for both sessionId and tntId as part of the other pr #116. |
* Added support for Target session Id setter & getter (#115) * Added support for Target session Id setter/ getter * code cleanup * Fixed formatting issue * Incorporated feedback * Incorporated feedback * Added support for TntId setter API (#116) * Added support for Target session Id setter/ getter * code cleanup * Fixed formatting issue * Added support for Target tntId setter * code cleanup * fixed formatting * fixed definition conflict test issue * Incorporated feedback * Incorporated feedback * Using distinct Event name * More testapp updates * Release prep v3.2.0 (#117) * release prep v3.2.0 * Feedback updates * Dependency and doc updates * Incorporated feedback * Fixed issue: Persisted edge host value was not used for Target request issued upon app close & relaunch (#119) * Fixed an issue where persisted edge host value was not used for Target request issued upon app close & relaunch * minor doc update * Target doc updates (#120) * Target doc updates * docs cleanup
Description
Ref: [MOB-16753], [MOB-16754]
Added Target session Id getter and setter APIs in the SDK to support hybrid implementations.
Related Issue
Motivation and Context
How Has This Been Tested?
Screenshots (if appropriate):
Types of changes
Checklist: