Skip to content

Commit

Permalink
feat: add URLSession into StatsigOptions (#278)
Browse files Browse the repository at this point in the history
add Configurable URLSession in iOS, default is `URLSession.shared`
  • Loading branch information
weihao-statsig authored Jun 10, 2024
1 parent 345133b commit c0fef62
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 6 additions & 3 deletions Sources/Statsig/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class NetworkService {
marker?.start(attempt: currentAttempt)


let task = URLSession.shared.dataTask(with: request) {
let task = self?.statsigOptions.urlSession.dataTask(with: request) {
[weak self] responseData, response, error in

marker?.end(currentAttempt, responseData, response, error)
Expand All @@ -324,8 +324,11 @@ class NetworkService {
}
}

taskCapture?(task)
task.resume()
if let taskCapture = taskCapture, let task = task {
taskCapture(task)
}

task?.resume()
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion Sources/Statsig/StatsigOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public class StatsigOptions {
*/
public var customCacheKey: ((String, StatsigUser) -> String)?

/**
The URLSession to be used for network requests. By default, it is set to URLSession.shared.
This property can be customized to utilize URLSession instances with specific configurations, including certificate pinning, for enhanced security when communicating with servers.
*/
public var urlSession: URLSession = .shared

internal var mainApiUrl: URL?
internal var logEventApiUrl: URL?
var environment: [String: String] = [:]
Expand All @@ -119,7 +125,8 @@ public class StatsigOptions {
eventLoggingApi: String? = nil,
evaluationCallback: ((EvaluationCallbackData) -> Void)? = nil,
userValidationCallback: ((StatsigUser) -> StatsigUser)? = nil,
customCacheKey: ((String, StatsigUser) -> String)? = nil
customCacheKey: ((String, StatsigUser) -> String)? = nil,
urlSession: URLSession? = nil
)
{
if let initTimeout = initTimeout, initTimeout >= 0 {
Expand Down Expand Up @@ -176,6 +183,10 @@ public class StatsigOptions {
if let customCacheKey = customCacheKey {
self.customCacheKey = customCacheKey
}

if let urlSession = urlSession {
self.urlSession = urlSession
}

self.overrideStableID = overrideStableID

Expand Down

0 comments on commit c0fef62

Please sign in to comment.