Skip to content

Commit

Permalink
pass URLRequest into requestIterator closures
Browse files Browse the repository at this point in the history
  • Loading branch information
ab1470 committed Aug 19, 2024
1 parent e67b274 commit e6adf68
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
22 changes: 11 additions & 11 deletions Sources/KlaviyoSwift/KlaviyoAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data, KlaviyoAPIError> = { request, attemptNumber in
let start = Date()
Expand All @@ -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))
}

Expand All @@ -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)
}
Expand Down
33 changes: 16 additions & 17 deletions Sources/KlaviyoSwift/SDKRequestIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -133,27 +132,27 @@ public struct SDKRequest: Identifiable, Equatable {
public func requestIterator() -> AsyncStream<SDKRequest> {
AsyncStream<SDKRequest> { 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))))
}
}
}

0 comments on commit e6adf68

Please sign in to comment.