Skip to content

Commit

Permalink
refactor to use RequestStatus enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ab1470 committed Aug 20, 2024
1 parent c23fc23 commit e9a4c3e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
22 changes: 9 additions & 13 deletions Sources/KlaviyoSwift/KlaviyoAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,27 @@ 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<Data, KlaviyoAPIError> = { request, attemptNumber in
let start = Date()

var urlRequest: URLRequest
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))
}

Expand All @@ -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)
}
Expand Down
40 changes: 20 additions & 20 deletions Sources/KlaviyoSwift/SDKRequestIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,27 @@ public enum RequestStatus {
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.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))))
}
}
}
}
}

0 comments on commit e9a4c3e

Please sign in to comment.