Skip to content

Commit

Permalink
remove special handling performSpeechRequest for `AudioSpeechResult…
Browse files Browse the repository at this point in the history
…` receiving raw (non-JSON-wrapped) data.
  • Loading branch information
James J Kalafus committed Feb 14, 2024
1 parent 4b7b666 commit 7cdcc02
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
32 changes: 7 additions & 25 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ final public class OpenAI: OpenAIProtocol {
performRequest(request: MultipartFormDataRequest<AudioTranslationResult>(body: query, url: buildURL(path: .audioTranslations)), completion: completion)
}

public func audioCreateSpeech(query: AudioSpeechQuery, completion: @escaping (Result<AudioSpeechResult, Error>) -> Void) {
performSpeechRequest(request: JSONRequest<AudioSpeechResult>(body: query, url: buildURL(path: .audioSpeech)), completion: completion)
public func audioCreateSpeech(query: AudioSpeechQuery, completion: @escaping (Result<Data, Error>) -> Void) {
performRequest(request: JSONRequest<Data>(body: query, url: buildURL(path: .audioSpeech)), completion: completion)
}

}
Expand All @@ -136,7 +136,11 @@ extension OpenAI {
do {
completion(.success(try decoder.decode(ResultType.self, from: data)))
} catch {
completion(.failure((try? decoder.decode(APIErrorResponse.self, from: data)) ?? error))
if ResultType.self == Data.self {
completion(.success(data as! ResultType))
} else {
completion(.failure((try? decoder.decode(APIErrorResponse.self, from: data)) ?? error))
}
}
}
task.resume()
Expand Down Expand Up @@ -167,28 +171,6 @@ extension OpenAI {
completion?(error)
}
}

func performSpeechRequest(request: any URLRequestBuildable, completion: @escaping (Result<AudioSpeechResult, Error>) -> Void) {
do {
let request = try request.build(token: configuration.token,
organizationIdentifier: configuration.organizationIdentifier,
timeoutInterval: configuration.timeoutInterval)

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

completion(.success(AudioSpeechResult(audioData: data)))
}
task.resume()
} catch {
completion(.failure(error))
}
}
}

extension OpenAI {
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAI/Public/Protocols/OpenAIProtocol+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public extension OpenAIProtocol {
audioCreateSpeech(query: query) { result in
switch result {
case let .success(success):
return continuation.resume(returning: success)
return continuation.resume(returning: AudioSpeechResult(audioData: success))
case let .failure(failure):
return continuation.resume(throwing: failure)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenAI/Public/Protocols/OpenAIProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ public protocol OpenAIProtocol {
- query: An `AudioSpeechQuery` object containing the parameters for the API request. This includes the Text-to-Speech model to be used, input text, voice to be used for generating the audio, the desired audio format, and the speed of the generated audio.
- completion: A closure which receives the result. The closure's parameter, `Result<AudioSpeechResult, Error>`, will either contain the `AudioSpeechResult` object with the audio data or an error if the request failed.
*/
func audioCreateSpeech(query: AudioSpeechQuery, completion: @escaping (Result<AudioSpeechResult, Error>) -> Void)
func audioCreateSpeech(query: AudioSpeechQuery, completion: @escaping (Result<Data, Error>) -> Void)

/**
Transcribes audio data using OpenAI's audio transcription API and completes the operation asynchronously.

Expand Down

0 comments on commit 7cdcc02

Please sign in to comment.