diff --git a/Sources/KlaviyoSwift/KlaviyoAPI.swift b/Sources/KlaviyoSwift/KlaviyoAPI.swift index ff688838..c75c4514 100644 --- a/Sources/KlaviyoSwift/KlaviyoAPI.swift +++ b/Sources/KlaviyoSwift/KlaviyoAPI.swift @@ -33,12 +33,8 @@ struct KlaviyoAPI { } // For internal testing use only - static var requestStarted: (KlaviyoRequest, URLRequest) -> Void = { _, _ in } - static var requestCompleted: (KlaviyoRequest, URLRequest, Data, Double) -> Void = { _, _, _, _ in } - static var requestFailed: (KlaviyoRequest, URLRequest?, Error, Double) -> Void = { _, _, _, _ in } - static var requestRateLimited: (KlaviyoRequest, URLRequest, Int?) -> Void = { _, _, _ in } - static var requestHttpError: (KlaviyoRequest, URLRequest, Int, Double) -> Void = { _, _, _, _ in } - + static var requestCompletion: (KlaviyoRequest, URLRequest?, RequestStatus) -> Void = { _, _, _ in } + var send: (KlaviyoRequest, Int) async -> Result = { request, attemptNumber in let start = Date() @@ -46,18 +42,18 @@ struct KlaviyoAPI { do { urlRequest = try request.urlRequest(attemptNumber) } catch { - requestFailed(request, nil, error, 0.0) + requestCompletion(request, nil, .error(.requestFailed(error))) return .failure(.internalRequestError(error)) } - requestStarted(request, urlRequest) + requestCompletion(request, urlRequest, .started) var response: URLResponse var data: Data do { (data, response) = try await environment.analytics.networkSession().data(urlRequest) } catch { - requestFailed(request, urlRequest, error, 0.0) + requestCompletion(request, urlRequest, .error(.requestFailed(error))) return .failure(KlaviyoAPIError.networkError(error)) } @@ -69,17 +65,17 @@ struct KlaviyoAPI { } if httpResponse.statusCode == 429 { - let retryAfter = Int(httpResponse.value(forHTTPHeaderField: "Retry-After") ?? "0") - requestRateLimited(request, urlRequest, retryAfter) + let retryAfter = Int(httpResponse.value(forHTTPHeaderField: "Retry-After") ?? "0") ?? 0 + requestCompletion(request, urlRequest, .error(.rateLimited(retryAfter: retryAfter))) return .failure(KlaviyoAPIError.rateLimitError(retryAfter)) } guard 200..<300 ~= httpResponse.statusCode else { - requestHttpError(request, urlRequest, httpResponse.statusCode, duration) + requestCompletion(request, urlRequest, .error(.httpError(statusCode: httpResponse.statusCode, duration: duration))) return .failure(KlaviyoAPIError.httpError(httpResponse.statusCode, data)) } - requestCompleted(request, urlRequest, data, duration) + requestCompletion(request, urlRequest, .completed(data: data, duration: duration)) return .success(data) } diff --git a/Sources/KlaviyoSwift/SDKRequestIterator.swift b/Sources/KlaviyoSwift/SDKRequestIterator.swift index 8294202a..9278be2f 100644 --- a/Sources/KlaviyoSwift/SDKRequestIterator.swift +++ b/Sources/KlaviyoSwift/SDKRequestIterator.swift @@ -149,27 +149,27 @@ public enum RequestStatus { public func requestIterator() -> AsyncStream { AsyncStream { continuation in continuation.onTermination = { _ in - KlaviyoAPI.requestStarted = { _, _ in } - KlaviyoAPI.requestFailed = { _, _, _, _ in } - KlaviyoAPI.requestCompleted = { _, _, _, _ in } - KlaviyoAPI.requestHttpError = { _, _, _, _ in } - KlaviyoAPI.requestRateLimited = { _, _, _ in } + KlaviyoAPI.requestCompletion = { _, _, _ in } } - KlaviyoAPI.requestStarted = { request, urlRequest in - continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .inProgress)) - } - KlaviyoAPI.requestCompleted = { request, urlRequest, data, duration in - let dataDescription = String(data: data, encoding: .utf8) ?? "Invalid Data" - continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .success(dataDescription, duration))) - } - KlaviyoAPI.requestFailed = { request, urlRequest, error, duration in - continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .reqeustError(error.localizedDescription, duration))) - } - KlaviyoAPI.requestHttpError = { request, urlRequest, statusCode, duration in - continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .httpError(statusCode, duration))) - } - KlaviyoAPI.requestRateLimited = { request, urlRequest, retryAfter in - continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .reqeustError("Rate Limited", Double(retryAfter ?? 0)))) + + KlaviyoAPI.requestCompletion = { request, urlRequest, result in + switch result { + case .started: + continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .inProgress)) + case .completed(let data, let duration): + let dataDescription = String(data: data, encoding: .utf8) ?? "Invalid Data" + continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .success(dataDescription, duration))) + case .error(let error): + switch error { + case .requestFailed(let requestError): + let duration = 0.0 + continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .reqeustError(requestError.localizedDescription, duration))) + case .httpError(let statusCode, duration: let duration): + continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .httpError(statusCode, duration))) + case .rateLimited(let retryAfter): + continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .reqeustError("Rate Limited", Double(retryAfter)))) + } + } } } }