Skip to content

Commit

Permalink
resolve review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Minghe Huang committed May 30, 2023
1 parent a768269 commit 4cf235c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
12 changes: 5 additions & 7 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ final public class OpenAI: OpenAIProtocol {
public let host: String

/// Optional base path if you set up OpenAI API proxy on a custom path on your own host. Default is ""
public let basePath: String?
public let basePath: String

/// Default request timeout
public let timeoutInterval: TimeInterval

public init(token: String, organizationIdentifier: String? = nil, host: String = "api.openai.com", basePath: String? = nil, timeoutInterval: TimeInterval = 60.0) {
public init(token: String, organizationIdentifier: String? = nil, host: String = "api.openai.com", basePath: String = "", timeoutInterval: TimeInterval = 60.0) {
self.token = token
self.organizationIdentifier = organizationIdentifier
self.host = host
Expand Down Expand Up @@ -172,11 +172,9 @@ extension OpenAI {
extension OpenAI {

func buildURL(path: String) -> URL {
var components = URLComponents()
components.scheme = "https"
components.host = configuration.host
components.path = (configuration.basePath ?? "") + path
return components.url!
return URL(string: "https://\(configuration.host)")!
.appendingPathComponent(configuration.basePath.trimmingCharacters(in: .init(charactersIn: "/")))
.appendingPathComponent(path.trimmingCharacters(in: .init(charactersIn: "/")))
}
}

Expand Down
33 changes: 31 additions & 2 deletions Tests/OpenAITests/OpenAITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,39 @@ class OpenAITests: XCTestCase {
}

func testCustomURLBuilt() {
let configuration = OpenAI.Configuration(token: "foo", organizationIdentifier: "bar", host: "my.host.com", timeoutInterval: 14)
let openAI = OpenAI(configuration: configuration, session: self.urlSession)
var configuration = OpenAI.Configuration(token: "foo", organizationIdentifier: "bar", host: "my.host.com", timeoutInterval: 14)
var openAI = OpenAI(configuration: configuration, session: self.urlSession)
let completionsURL = openAI.buildURL(path: .completions)
XCTAssertEqual(completionsURL, URL(string: "https://my.host.com/v1/completions"))

configuration = OpenAI.Configuration(
token: "foo",
organizationIdentifier: "bar",
host: "bizbaz.com",
timeoutInterval: 14
)
openAI = OpenAI(configuration: configuration, session: URLSessionMock())
XCTAssertEqual(openAI.buildURL(path: "foo"), URL(string: "https://bizbaz.com/foo"))

configuration = OpenAI.Configuration(
token: "foo",
organizationIdentifier: "bar",
host: "bizbaz.com",
basePath: "/openai",
timeoutInterval: 14
)
openAI = OpenAI(configuration: configuration, session: URLSessionMock())
XCTAssertEqual(openAI.buildURL(path: "foo"), URL(string:"https://bizbaz.com/openai/foo"))

configuration = OpenAI.Configuration(
token: "foo",
organizationIdentifier: "bar",
host: "bizbaz.com",
basePath: "/openai/",
timeoutInterval: 14
)
openAI = OpenAI(configuration: configuration, session: URLSessionMock())
XCTAssertEqual(openAI.buildURL(path: "/foo"), URL(string: "https://bizbaz.com/openai/foo"))
}
}

Expand Down

0 comments on commit 4cf235c

Please sign in to comment.