Skip to content

Commit

Permalink
Automatic generation of request Id if missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Vangaorth committed May 25, 2024
1 parent 47d99bb commit f29a9d8
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions ios/IoReactNativeHttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@ class IoReactNativeHttpClient: NSObject {
let headers = headersFromConfig(config)
let redirector = redirectorFromConfig(config)
let timeoutSeconds = timeoutSecondsFromConfig(config)
let requestIdOpt = requestIdOptFromConfig(config)
let requestId = requestIdFromConfigOrRandom(config)

let request = AF.request(url,
method: method,
parameters: parametersOpt,
headers: headers,
requestModifier: { $0.timeoutInterval = timeoutSeconds })
.redirect(using: redirector)
if let requestId = requestIdOpt {
runningRequests.updateValue(request, forKey: requestId)
}

runningRequests.updateValue(request, forKey: requestId)

request.responseString { response in
if let requestId = requestIdOpt {
self.runningRequests.removeValue(forKey: requestId)
}
self.runningRequests.removeValue(forKey: requestId)
let isCancelled = request.isCancelled
self.handleResponse(response, cancelled: isCancelled, resolve: resolve)
}
Expand Down Expand Up @@ -135,8 +133,8 @@ class IoReactNativeHttpClient: NSObject {
return 60
}

func requestIdOptFromConfig(_ configOpt: [String: Any]?) -> String? {
return configOpt?["requestId"] as? String
func requestIdFromConfigOrRandom(_ configOpt: [String: Any]?) -> String {
return (configOpt?["requestId"] as? String) ?? NSUUID().uuidString
}

func handleResponse(_ response: AFDataResponse<String>, cancelled: Bool, resolve: @escaping RCTPromiseResolveBlock) -> Void {
Expand Down

0 comments on commit f29a9d8

Please sign in to comment.