From e6adf68631c878f2fe33afecdae675d9f57540b7 Mon Sep 17 00:00:00 2001 From: Andrew Balmer Date: Mon, 19 Aug 2024 18:27:57 -0400 Subject: [PATCH] pass `URLRequest` into `requestIterator` closures --- Sources/KlaviyoSwift/KlaviyoAPI.swift | 22 ++++++------- Sources/KlaviyoSwift/SDKRequestIterator.swift | 33 +++++++++---------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/Sources/KlaviyoSwift/KlaviyoAPI.swift b/Sources/KlaviyoSwift/KlaviyoAPI.swift index 46fa3a3f..ff688838 100644 --- a/Sources/KlaviyoSwift/KlaviyoAPI.swift +++ b/Sources/KlaviyoSwift/KlaviyoAPI.swift @@ -33,11 +33,11 @@ struct KlaviyoAPI { } // For internal testing use only - static var requestStarted: (KlaviyoRequest) -> Void = { _ in } - static var requestCompleted: (KlaviyoRequest, Data, Double) -> Void = { _, _, _ in } - static var requestFailed: (KlaviyoRequest, Error, Double) -> Void = { _, _, _ in } - static var requestRateLimited: (KlaviyoRequest, Int?) -> Void = { _, _ in } - static var requestHttpError: (KlaviyoRequest, Int, Double) -> Void = { _, _, _ in } + 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 } var send: (KlaviyoRequest, Int) async -> Result = { request, attemptNumber in let start = Date() @@ -46,18 +46,18 @@ struct KlaviyoAPI { do { urlRequest = try request.urlRequest(attemptNumber) } catch { - requestFailed(request, error, 0.0) + requestFailed(request, nil, error, 0.0) return .failure(.internalRequestError(error)) } - requestStarted(request) + requestStarted(request, urlRequest) var response: URLResponse var data: Data do { (data, response) = try await environment.analytics.networkSession().data(urlRequest) } catch { - requestFailed(request, error, 0.0) + requestFailed(request, urlRequest, error, 0.0) return .failure(KlaviyoAPIError.networkError(error)) } @@ -70,16 +70,16 @@ struct KlaviyoAPI { if httpResponse.statusCode == 429 { let retryAfter = Int(httpResponse.value(forHTTPHeaderField: "Retry-After") ?? "0") - requestRateLimited(request, retryAfter) + requestRateLimited(request, urlRequest, retryAfter) return .failure(KlaviyoAPIError.rateLimitError(retryAfter)) } guard 200..<300 ~= httpResponse.statusCode else { - requestHttpError(request, httpResponse.statusCode, duration) + requestHttpError(request, urlRequest, httpResponse.statusCode, duration) return .failure(KlaviyoAPIError.httpError(httpResponse.statusCode, data)) } - requestCompleted(request, data, duration) + requestCompleted(request, urlRequest, data, duration) return .success(data) } diff --git a/Sources/KlaviyoSwift/SDKRequestIterator.swift b/Sources/KlaviyoSwift/SDKRequestIterator.swift index 40043ad2..2e430e84 100644 --- a/Sources/KlaviyoSwift/SDKRequestIterator.swift +++ b/Sources/KlaviyoSwift/SDKRequestIterator.swift @@ -86,9 +86,8 @@ public struct SDKRequest: Identifiable, Equatable { case reqeustError(String, Double) } - static func fromAPIRequest(request: KlaviyoAPI.KlaviyoRequest, response: SDKRequest.Response) -> SDKRequest { + static func fromAPIRequest(request: KlaviyoAPI.KlaviyoRequest, urlRequest: URLRequest?, response: SDKRequest.Response) -> SDKRequest { let type = RequestType.fromEndpoint(request: request) - let urlRequest = try? request.urlRequest() let method = urlRequest?.httpMethod ?? "Unknown" let url = urlRequest?.url?.description ?? "Unknown" return SDKRequest(id: request.uuid, @@ -133,27 +132,27 @@ public struct SDKRequest: Identifiable, Equatable { 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.requestStarted = { _, _ in } + KlaviyoAPI.requestFailed = { _, _, _, _ in } + KlaviyoAPI.requestCompleted = { _, _, _, _ in } + KlaviyoAPI.requestHttpError = { _, _, _, _ in } + KlaviyoAPI.requestRateLimited = { _, _, _ in } } - KlaviyoAPI.requestStarted = { request in - continuation.yield(SDKRequest.fromAPIRequest(request: request, response: .inProgress)) + KlaviyoAPI.requestStarted = { request, urlRequest in + continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .inProgress)) } - KlaviyoAPI.requestCompleted = { request, data, duration in + KlaviyoAPI.requestCompleted = { request, urlRequest, data, duration in let dataDescription = String(data: data, encoding: .utf8) ?? "Invalid Data" - continuation.yield(SDKRequest.fromAPIRequest(request: request, response: .success(dataDescription, duration))) + continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .success(dataDescription, duration))) } - KlaviyoAPI.requestFailed = { request, error, duration in - continuation.yield(SDKRequest.fromAPIRequest(request: request, response: .reqeustError(error.localizedDescription, duration))) + KlaviyoAPI.requestFailed = { request, urlRequest, error, duration in + continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .reqeustError(error.localizedDescription, duration))) } - KlaviyoAPI.requestHttpError = { request, statusCode, duration in - continuation.yield(SDKRequest.fromAPIRequest(request: request, response: .httpError(statusCode, duration))) + KlaviyoAPI.requestHttpError = { request, urlRequest, statusCode, duration in + continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .httpError(statusCode, duration))) } - KlaviyoAPI.requestRateLimited = { request, retryAfter in - continuation.yield(SDKRequest.fromAPIRequest(request: request, response: .reqeustError("Rate Limited", Double(retryAfter ?? 0)))) + KlaviyoAPI.requestRateLimited = { request, urlRequest, retryAfter in + continuation.yield(SDKRequest.fromAPIRequest(request: request, urlRequest: urlRequest, response: .reqeustError("Rate Limited", Double(retryAfter ?? 0)))) } } }