@@ -22,6 +22,8 @@ class DefaultDatafileHandler : OPTDatafileHandler {
2222 var timers : AtomicProperty < [ String : ( timer: Timer , interval: Int ) ] > = AtomicProperty ( property: [ String: ( Timer, Int) ] ( ) )
2323 let dataStore = DataStoreUserDefaults ( )
2424
25+ let downloadQueue = DispatchQueue ( label: " DefaultDatafileHandlerQueue " , qos: DispatchQoS . default, attributes: DispatchQueue . Attributes. concurrent, autoreleaseFrequency: DispatchQueue . AutoreleaseFrequency. inherit, target: nil )
26+
2527 required init ( ) {
2628
2729 }
@@ -48,23 +50,54 @@ class DefaultDatafileHandler : OPTDatafileHandler {
4850 return datafile
4951 }
5052
51- open func downloadDatafile( sdkKey: String ,
52- resourceTimeoutInterval: Double ? = nil ,
53- completionHandler: @escaping DatafileDownloadCompletionHandler ) {
53+ open func getSession( resourceTimeoutInterval: Double ? ) -> URLSession {
5454 let config = URLSessionConfiguration . ephemeral
5555 if let resourceTimeoutInterval = resourceTimeoutInterval,
5656 resourceTimeoutInterval > 0 {
5757 config. timeoutIntervalForResource = TimeInterval ( resourceTimeoutInterval)
5858 }
59- let session = URLSession ( configuration: config)
59+ return URLSession ( configuration: config)
60+ }
61+
62+ open func getRequest( sdkKey: String ) -> URLRequest ? {
6063 let str = String ( format: DefaultDatafileHandler . endPointStringFormat, sdkKey)
61- if let url = URL ( string: str) {
62- var request = URLRequest ( url: url)
63-
64- if let lastModified = dataStore. getItem ( forKey: " OPTLastModified- " + sdkKey) {
65- request. addValue ( lastModified as! String , forHTTPHeaderField: " If-Modified-Since " )
64+ guard let url = URL ( string: str) else { return nil }
65+
66+ var request = URLRequest ( url: url)
67+
68+ if let lastModified = dataStore. getItem ( forKey: " OPTLastModified- " + sdkKey) {
69+ request. addValue ( lastModified as! String , forHTTPHeaderField: " If-Modified-Since " )
70+ }
71+
72+ return request
73+
74+ }
75+
76+ open func getResponseData( sdkKey: String , response: HTTPURLResponse , url: URL ? ) -> Data ? {
77+ if let url = url, let data = try ? Data ( contentsOf: url) {
78+ if let str = String ( data: data, encoding: . utf8) {
79+ self . logger? . log ( level: . debug, message: str)
80+ }
81+ self . saveDatafile ( sdkKey: sdkKey, dataFile: data)
82+ if let lastModified = response. allHeaderFields [ " Last-Modified " ] {
83+ self . dataStore. saveItem ( forKey: " OPTLastModified- " + sdkKey, value: lastModified)
6684 }
6785
86+ return data
87+ }
88+
89+ return nil
90+ }
91+
92+ open func downloadDatafile( sdkKey: String ,
93+ resourceTimeoutInterval: Double ? = nil ,
94+ completionHandler: @escaping DatafileDownloadCompletionHandler ) {
95+
96+ downloadQueue. async {
97+ let session = self . getSession ( resourceTimeoutInterval: resourceTimeoutInterval)
98+
99+ guard let request = self . getRequest ( sdkKey: sdkKey) else { return }
100+
68101 let task = session. downloadTask ( with: request) { ( url, response, error) in
69102 var result = OptimizelyResult< Data?> . failure( . datafileDownloadFailed( " Failed to parse " ) )
70103
@@ -73,31 +106,22 @@ class DefaultDatafileHandler : OPTDatafileHandler {
73106 result = . failure( . datafileDownloadFailed( error. debugDescription) )
74107 } else if let response = response as? HTTPURLResponse {
75108 if response. statusCode == 200 {
76- if let url = url, let data = try ? Data ( contentsOf: url) {
77- if let str = String ( data: data, encoding: . utf8) {
78- self . logger? . d ( str)
79- }
80- self . saveDatafile ( sdkKey: sdkKey, dataFile: data)
81- if let lastModified = response. allHeaderFields [ " Last-Modified " ] {
82- self . dataStore. saveItem ( forKey: " OPTLastModified- " + sdkKey, value: lastModified)
83- }
84-
85- result = . success( data)
86- }
87- } else if response. statusCode == 304 {
88- self . logger? . d ( " The datafile was not modified and won't be downloaded again " )
109+ let data = self . getResponseData ( sdkKey: sdkKey, response: response, url: url)
110+ result = Result . success ( data)
111+ }
112+ else if response. statusCode == 304 {
113+ self . logger? . log ( level: . debug, message: " The datafile was not modified and won't be downloaded again " )
89114 result = . success( nil )
90115 }
91116 }
92-
117+
93118 completionHandler ( result)
94119
95120 //self.logger?.d(response.debugDescription)
96121 }
97122
98123 task. resume ( )
99124 }
100-
101125 }
102126
103127 func startPeriodicUpdates( sdkKey: String , updateInterval: Int , datafileChangeNotification: ( ( Data ) -> Void ) ? ) {
0 commit comments