Skip to content

Commit e469133

Browse files
authored
Merge pull request #135 from rchatham/main
Simplify OpenAI.performRequest
2 parents 25f93ee + 21e3e58 commit e469133

File tree

2 files changed

+13
-47
lines changed

2 files changed

+13
-47
lines changed

Sources/OpenAI/OpenAI.swift

+7-32
Original file line numberDiff line numberDiff line change
@@ -127,29 +127,16 @@ extension OpenAI {
127127
timeoutInterval: configuration.timeoutInterval)
128128
let task = session.dataTask(with: request) { data, _, error in
129129
if let error = error {
130-
completion(.failure(error))
131-
return
130+
return completion(.failure(error))
132131
}
133132
guard let data = data else {
134-
completion(.failure(OpenAIError.emptyData))
135-
return
133+
return completion(.failure(OpenAIError.emptyData))
136134
}
137-
138-
var apiError: Error? = nil
135+
let decoder = JSONDecoder()
139136
do {
140-
let decoded = try JSONDecoder().decode(ResultType.self, from: data)
141-
completion(.success(decoded))
137+
completion(.success(try decoder.decode(ResultType.self, from: data)))
142138
} catch {
143-
apiError = error
144-
}
145-
146-
if let apiError = apiError {
147-
do {
148-
let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: data)
149-
completion(.failure(decoded))
150-
} catch {
151-
completion(.failure(apiError))
152-
}
139+
completion(.failure((try? decoder.decode(APIErrorResponse.self, from: data)) ?? error))
153140
}
154141
}
155142
task.resume()
@@ -189,25 +176,13 @@ extension OpenAI {
189176

190177
let task = session.dataTask(with: request) { data, _, error in
191178
if let error = error {
192-
completion(.failure(error))
193-
return
179+
return completion(.failure(error))
194180
}
195181
guard let data = data else {
196-
completion(.failure(OpenAIError.emptyData))
197-
return
182+
return completion(.failure(OpenAIError.emptyData))
198183
}
199184

200185
completion(.success(AudioSpeechResult(audioData: data)))
201-
let apiError: Error? = nil
202-
203-
if let apiError = apiError {
204-
do {
205-
let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: data)
206-
completion(.failure(decoded))
207-
} catch {
208-
completion(.failure(apiError))
209-
}
210-
}
211186
}
212187
task.resume()
213188
} catch {

Sources/OpenAI/Private/StreamingSession.swift

+6-15
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,17 @@ extension StreamingSession {
7979
onProcessingError?(self, StreamingError.unknownContent)
8080
return
8181
}
82-
83-
var apiError: Error? = nil
82+
let decoder = JSONDecoder()
8483
do {
85-
let decoder = JSONDecoder()
8684
let object = try decoder.decode(ResultType.self, from: jsonData)
8785
onReceiveContent?(self, object)
8886
} catch {
89-
apiError = error
90-
}
91-
92-
if let apiError = apiError {
93-
do {
94-
let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: jsonData)
87+
if let decoded = try? decoder.decode(APIErrorResponse.self, from: jsonData) {
9588
onProcessingError?(self, decoded)
96-
} catch {
97-
if index == jsonObjects.count - 1 {
98-
previousChunkBuffer = "data: \(jsonContent)" // Chunk ends in a partial JSON
99-
} else {
100-
onProcessingError?(self, apiError)
101-
}
89+
} else if index == jsonObjects.count - 1 {
90+
previousChunkBuffer = "data: \(jsonContent)" // Chunk ends in a partial JSON
91+
} else {
92+
onProcessingError?(self, error)
10293
}
10394
}
10495
}

0 commit comments

Comments
 (0)