@@ -25,8 +25,8 @@ internal struct ApphudAPIArrayResponse<T: Decodable>: Decodable {
25
25
}
26
26
27
27
typealias ApphudParsedResponse = ( Bool , [ String : Any ] ? , Data ? , Error ? , Int )
28
- typealias ApphudHTTPResponse = ( Bool , [ String : Any ] ? , Data ? , Error ? , Int , Double )
29
- typealias ApphudHTTPResponseCallback = ( Bool , [ String : Any ] ? , Data ? , Error ? , Int , Double ) -> Void
28
+ typealias ApphudHTTPResponse = ( Bool , [ String : Any ] ? , Data ? , Error ? , Int , Double , Int )
29
+ typealias ApphudHTTPResponseCallback = ( Bool , [ String : Any ] ? , Data ? , Error ? , Int , Double , Int ) -> Void
30
30
typealias ApphudStringCallback = ( String ? , Error ? ) -> Void
31
31
/**
32
32
This is Apphud's internal class.
@@ -111,8 +111,8 @@ public class ApphudHttpClient {
111
111
return URLSession . init ( configuration: config)
112
112
} ( )
113
113
114
- private let GET_TIMEOUT : TimeInterval = 10 .0
115
- public var POST_CUSTOMERS_TIMEOUT : TimeInterval = 10 .0
114
+ private let GET_TIMEOUT : TimeInterval = 7 .0
115
+ public var POST_CUSTOMERS_TIMEOUT : TimeInterval = 7 .0
116
116
private let POST_TIMEOUT : TimeInterval = 20.0
117
117
118
118
internal func requestInstance( url: URL ) -> URLRequest ? {
@@ -129,11 +129,11 @@ public class ApphudHttpClient {
129
129
}
130
130
}
131
131
132
- internal func startRequest( path: ApphudEndpoint , apiVersion: ApphudApiVersion = . APIV1, params: [ String : Any ] ? , method: ApphudHttpMethod , useDecoder: Bool = false , retry: Bool = false , callback: ApphudHTTPResponseCallback ? ) {
132
+ internal func startRequest( path: ApphudEndpoint , apiVersion: ApphudApiVersion = . APIV1, params: [ String : Any ] ? , method: ApphudHttpMethod , useDecoder: Bool = false , retry: Bool = false , requestID : String ? = nil , callback: ApphudHTTPResponseCallback ? ) {
133
133
134
134
let timeout = path == . customers ? POST_CUSTOMERS_TIMEOUT : nil
135
135
136
- if let request = makeRequest ( path: path. value, apiVersion: apiVersion, params: params, method: method, defaultTimeout: timeout) , !suspended {
136
+ if let request = makeRequest ( path: path. value, apiVersion: apiVersion, params: params, method: method, defaultTimeout: timeout, requestID : requestID ) , !suspended {
137
137
Task ( priority: . userInitiated) {
138
138
139
139
let retries : Int
@@ -150,7 +150,7 @@ public class ApphudHttpClient {
150
150
let response = await start ( request: request, useDecoder: useDecoder, retries: retries, delay: retryDelay)
151
151
152
152
Task { @MainActor in
153
- callback ? ( response. 0 , response. 1 , response. 2 , response. 3 , response. 4 , response. 5 )
153
+ callback ? ( response. 0 , response. 1 , response. 2 , response. 3 , response. 4 , response. 5 , response . 6 )
154
154
}
155
155
}
156
156
} else {
@@ -226,11 +226,7 @@ public class ApphudHttpClient {
226
226
return nil
227
227
}
228
228
229
- private var requestID : String {
230
- UUID ( ) . uuidString. lowercased ( ) . replacingOccurrences ( of: " - " , with: " " ) + " _ " + String( Date ( ) . timeIntervalSince1970)
231
- }
232
-
233
- private func makeRequest( path: String , apiVersion: ApphudApiVersion , params: [ String : Any ] ? , method: ApphudHttpMethod , defaultTimeout: TimeInterval ? = nil ) -> URLRequest ? {
229
+ private func makeRequest( path: String , apiVersion: ApphudApiVersion , params: [ String : Any ] ? , method: ApphudHttpMethod , defaultTimeout: TimeInterval ? = nil , requestID: String ? = nil ) -> URLRequest ? {
234
230
235
231
var request : URLRequest ?
236
232
@@ -240,7 +236,7 @@ public class ApphudHttpClient {
240
236
241
237
if method == . get {
242
238
var components = URLComponents ( string: urlString)
243
- var items : [ URLQueryItem ] = [ URLQueryItem ( name: " api_key " , value: apiKey) , URLQueryItem ( name : " request_id " , value : requestID ) ]
239
+ var items : [ URLQueryItem ] = [ URLQueryItem ( name: " api_key " , value: apiKey) ]
244
240
if let requestParams = params {
245
241
for key in requestParams. keys {
246
242
items. append ( URLQueryItem ( name: key, value: ( requestParams [ key] as? LosslessStringConvertible ) ? . description) )
@@ -266,13 +262,14 @@ public class ApphudHttpClient {
266
262
request? . setValue ( " application/json; charset=utf-8 " , forHTTPHeaderField: " Accept " )
267
263
request? . setValue ( platform, forHTTPHeaderField: " X-Platform " )
268
264
request? . setValue ( self . sdkType, forHTTPHeaderField: " X-SDK " )
265
+ request? . setValue ( requestID ?? UUID ( ) . uuidString, forHTTPHeaderField: " Idempotency-Key " )
269
266
request? . setValue ( sdkVersion, forHTTPHeaderField: " X-SDK-VERSION " )
270
267
request? . setValue ( " Apphud \( platform) ( \( self . sdkType) \( sdkVersion) ) " , forHTTPHeaderField: " User-Agent " )
271
268
272
269
request? . timeoutInterval = defaultTimeout ?? ( method == . get ? GET_TIMEOUT : POST_TIMEOUT)
273
270
274
271
if method != . get {
275
- var finalParams : [ String : Any ] = [ " api_key " : apiKey, " request_id " : requestID ]
272
+ var finalParams : [ String : Any ] = [ " api_key " : apiKey]
276
273
if params != nil {
277
274
finalParams. merge ( params!, uniquingKeysWith: { $1} )
278
275
}
@@ -313,32 +310,40 @@ public class ApphudHttpClient {
313
310
let method = request. httpMethod ?? " "
314
311
315
312
do {
316
- let ( data, response) = retries > 0 ? try await URLSession . shared. data ( for: request, retries: retries, delay: delay) : try await URLSession . shared. data ( for: request)
313
+ let result : ( Data , URLResponse , Int )
314
+ if retries > 0 {
315
+ result = try await URLSession . shared. data ( for: request, retries: retries, delay: delay)
316
+ } else {
317
+ let resp = try await URLSession . shared. data ( for: request)
318
+ result = ( resp. 0 , resp. 1 , 1 )
319
+ }
317
320
318
- guard let httpResponse = response as? HTTPURLResponse else {
319
- return ( false , nil , nil , nil , NSURLErrorUnknown, 0 )
321
+ guard let httpResponse = result . 1 as? HTTPURLResponse else {
322
+ return ( false , nil , nil , nil , NSURLErrorUnknown, 0 , 1 )
320
323
}
321
324
322
- let apphudResponse : ApphudParsedResponse = parseResponse ( request: request, httpResponse: httpResponse, data: data , parseJson: !useDecoder || apphudIsSandbox ( ) )
325
+ let apphudResponse : ApphudParsedResponse = parseResponse ( request: request, httpResponse: httpResponse, data: result . 0 , parseJson: !useDecoder || apphudIsSandbox ( ) )
323
326
324
327
let requestDuration = Date ( ) . timeIntervalSince ( startDate)
325
328
326
- let finalHttpResponse : ApphudHTTPResponse = ( apphudResponse. 0 , apphudResponse. 1 , apphudResponse. 2 , apphudResponse. 3 , apphudResponse. 4 , requestDuration)
329
+ let finalHttpResponse : ApphudHTTPResponse = ( apphudResponse. 0 , apphudResponse. 1 , apphudResponse. 2 , apphudResponse. 3 , apphudResponse. 4 , requestDuration, result . 2 )
327
330
328
331
return finalHttpResponse
329
332
330
333
} catch {
334
+ let apphudError = error as? ApphudError
335
+ let attempts = apphudError? . attempts ?? retries
331
336
332
337
let code = ( error as NSError ? ) ? . code ?? NSURLErrorUnknown
333
338
334
339
if ApphudUtils . shared. logLevel == . all {
335
- apphudLog ( " Request \( method) \( request. url? . absoluteString ?? " " ) failed with code: \( code) after: \( retries ) retries error: \( error. localizedDescription) " , logLevel: . all)
340
+ apphudLog ( " Request \( method) \( request. url? . absoluteString ?? " " ) failed with code: \( code) after: \( attempts ) attempts error: \( error. localizedDescription) " , logLevel: . all)
336
341
} else {
337
- apphudLog ( " Request \( method) \( request. url? . absoluteString ?? " " ) failed with code: \( code) after: \( retries ) retries error: \( error. localizedDescription) " )
342
+ apphudLog ( " Request \( method) \( request. url? . absoluteString ?? " " ) failed with code: \( code) after: \( attempts ) attempts error: \( error. localizedDescription) " )
338
343
}
339
344
340
345
// Handle any errors that occurred during the request
341
- return ( false , nil , nil , error, code, 0 )
346
+ return ( false , nil , nil , error, code, 0 , attempts )
342
347
}
343
348
}
344
349
0 commit comments