Skip to content

Commit

Permalink
Change private to internal for generateHeaders() visbility modifier t…
Browse files Browse the repository at this point in the history
…o enable future extension and/or testing.
  • Loading branch information
benvolioT committed Jul 30, 2023
1 parent 2334b8c commit 31532cc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
33 changes: 4 additions & 29 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final public class OpenAI: OpenAIProtocol {
}

private let session: URLSessionProtocol
private let dataTaskManager: URLSessionDataTaskManager
private var streamingSessions: [NSObject] = []

public let configuration: Configuration
Expand All @@ -54,6 +55,7 @@ final public class OpenAI: OpenAIProtocol {
init(configuration: Configuration, session: URLSessionProtocol) {
self.configuration = configuration
self.session = session
self.dataTaskManager = URLSessionDataTaskManager(session: session)
}

public convenience init(configuration: Configuration, session: URLSession = URLSession.shared) {
Expand Down Expand Up @@ -110,7 +112,7 @@ final public class OpenAI: OpenAIProtocol {
}

extension OpenAI {
private func generateHeaders() -> [String: String] {
internal func generateHeaders() -> [String: String] {
var headers = configuration.additionalHeaders
headers["Authorization"] = "Bearer \(configuration.token)"
if let organizationIdentifier = configuration.organizationIdentifier {
Expand All @@ -125,34 +127,7 @@ extension OpenAI {
func performRequest<ResultType: Codable>(request: any URLRequestBuildable, completion: @escaping (Result<ResultType, Error>) -> Void) {
do {
let request = try request.build()
let task = session.dataTask(with: request) { data, _, error in
if let error = error {
completion(.failure(error))
return
}
guard let data = data else {
completion(.failure(OpenAIError.emptyData))
return
}

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

if let apiError = apiError {
do {
let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: data)
completion(.failure(decoded))
} catch {
completion(.failure(apiError))
}
}
}
task.resume()
dataTaskManager.performDataTask(with: request, completion: completion)
} catch {
completion(.failure(error))
}
Expand Down
40 changes: 40 additions & 0 deletions Sources/OpenAI/Private/URLSessionDataTaskManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// URLSessionDataTaskManager.swift
//
//
// Created by Benjamin Truitt on 7/29/23.
//

import Foundation

public class URLSessionDataTaskManager {

private var session: URLSessionProtocol

init(session: URLSessionProtocol) {
self.session = session
}

func performDataTask<ResultType: Codable>(with request: URLRequest, completion: @escaping (Result<ResultType, Error>) -> Void) {
let task = session.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if let error = error {
completion(.failure(error))
} else if let data = data {
do {
let decoded = try JSONDecoder().decode(ResultType.self, from: data)
completion(.success(decoded))
} catch {
do {
let decoded = try JSONDecoder().decode(APIErrorResponse.self, from: data)
completion(.failure(decoded))
} catch let decodingError {
completion(.failure(decodingError))
}
}
} else {
completion(.failure(OpenAIError.emptyData))
}
}
task.resume()
}
}

0 comments on commit 31532cc

Please sign in to comment.