Use a custom networking stack? Pull headers from the client and handle nonce challenges manually.
var request = URLRequest(url: configuration.baseURL.appendingPathComponent("anthropic/v1/messages"))
request.httpMethod = "POST"
request.httpBody = anthropicPayload
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("2023-06-01", forHTTPHeaderField: "anthropic-version")
do {
let headers = try await gateAIClient.authorizationHeaders(
for: request.url!,
method: .post
)
headers.forEach { key, value in
request.setValue(value, forHTTPHeaderField: key)
}
let (data, response) = try await URLSession.shared.data(for: request)
// Handle response...
} catch {
if let nonce = gateAIClient.extractDPoPNonce(from: error) {
let retryHeaders = try await gateAIClient.authorizationHeaders(
for: request.url!,
method: .post,
nonce: nonce
)
retryHeaders.forEach { key, value in
request.setValue(value, forHTTPHeaderField: key)
}
_ = try await URLSession.shared.data(for: request)
} else {
throw error
}
}