Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify OpenAI.performRequest #135

Merged
merged 10 commits into from
Feb 2, 2024
40 changes: 8 additions & 32 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,17 @@ extension OpenAI {
timeoutInterval: configuration.timeoutInterval)
let task = session.dataTask(with: request) { data, _, error in
if let error = error {
completion(.failure(error))
return
return completion(.failure(error))
}
guard let data = data else {
completion(.failure(OpenAIError.emptyData))
return
return completion(.failure(OpenAIError.emptyData))
}

var apiError: Error? = nil
let decoder = JSONDecoder()
do {
let decoded = try JSONDecoder().decode(ResultType.self, from: data)
completion(.success(decoded))
completion(.success(try decoder.decode(ResultType.self, from: data)))
} catch {
apiError = error
}

if let apiError = apiError {
do {
let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: data)
completion(.failure(decoded))
} catch {
completion(.failure(apiError))
}
completion(.failure((try? decoder.decode(APIErrorResponse.self, from: data)) ?? error))
}
}
task.resume()
Expand Down Expand Up @@ -189,25 +177,13 @@ extension OpenAI {

let task = session.dataTask(with: request) { data, _, error in
if let error = error {
completion(.failure(error))
return
return completion(.failure(error))
}
guard let data = data else {
completion(.failure(OpenAIError.emptyData))
return
return completion(.failure(OpenAIError.emptyData))
}

completion(.success(AudioSpeechResult(audioData: data)))
let apiError: Error? = nil

if let apiError = apiError {
do {
let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: data)
completion(.failure(decoded))
} catch {
completion(.failure(apiError))
}
}
}
task.resume()
} catch {
Expand Down
21 changes: 6 additions & 15 deletions Sources/OpenAI/Private/StreamingSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,20 @@ extension StreamingSession {
return
}
guard let jsonData = jsonContent.data(using: .utf8) else {
onProcessingError?(self, StreamingError.unknownContent)
return
return onProcessingError?(self, StreamingError.unknownContent)
rchatham marked this conversation as resolved.
Show resolved Hide resolved
}

var apiError: Error? = nil
do {
let decoder = JSONDecoder()
let object = try decoder.decode(ResultType.self, from: jsonData)
onReceiveContent?(self, object)
} catch {
apiError = error
}

if let apiError = apiError {
do {
let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: jsonData)
if let decoded = try decoder.decode(APIErrorResponse.self, from: jsonData) {
rchatham marked this conversation as resolved.
Show resolved Hide resolved
onProcessingError?(self, decoded)
} catch {
if index == jsonObjects.count - 1 {
previousChunkBuffer = "data: \(jsonContent)" // Chunk ends in a partial JSON
} else {
onProcessingError?(self, apiError)
}
} else if index == jsonObjects.count - 1 {
previousChunkBuffer = "data: \(jsonContent)" // Chunk ends in a partial JSON
} else {
onProcessingError?(self, error)
}
}
}
Expand Down