Skip to content

Commit

Permalink
Merge pull request #135 from rchatham/main
Browse files Browse the repository at this point in the history
Simplify OpenAI.performRequest
  • Loading branch information
ingvarus-bc committed Feb 2, 2024
2 parents 25f93ee + 21e3e58 commit e469133
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 47 deletions.
39 changes: 7 additions & 32 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,16 @@ 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 +176,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 @@ -79,26 +79,17 @@ extension StreamingSession {
onProcessingError?(self, StreamingError.unknownContent)
return
}

var apiError: Error? = nil
let decoder = JSONDecoder()
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) {
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

0 comments on commit e469133

Please sign in to comment.