Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add scheme, host, and port for buildURL #180

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ final public class OpenAI: OpenAIProtocol {

/// API host. Set this property if you use some kind of proxy or your own server. Default is api.openai.com
public let host: String

public let port: Int
public let scheme: String
/// Default request timeout
public let timeoutInterval: TimeInterval

public init(token: String, organizationIdentifier: String? = nil, host: String = "api.openai.com", timeoutInterval: TimeInterval = 60.0) {
public init(token: String, organizationIdentifier: String? = nil, host: String = "api.openai.com", port: Int = 443, scheme: String = "https", timeoutInterval: TimeInterval = 60.0) {
self.token = token
self.organizationIdentifier = organizationIdentifier
self.host = host
self.port = port
self.scheme = scheme
self.timeoutInterval = timeoutInterval
}
}
Expand Down Expand Up @@ -196,8 +199,9 @@ extension OpenAI {

func buildURL(path: String) -> URL {
var components = URLComponents()
components.scheme = "https"
components.scheme = configuration.scheme
components.host = configuration.host
components.port = configuration.port
components.path = path
return components.url!
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/OpenAITests/OpenAITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,14 @@ class OpenAITests: XCTestCase {
let configuration = OpenAI.Configuration(token: "foo", organizationIdentifier: "bar", timeoutInterval: 14)
let openAI = OpenAI(configuration: configuration, session: self.urlSession)
let chatsURL = openAI.buildURL(path: .chats)
XCTAssertEqual(chatsURL, URL(string: "https://api.openai.com/v1/chat/completions"))
XCTAssertEqual(chatsURL, URL(string: "https://api.openai.com:443/v1/chat/completions"))
}

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

Expand Down
Loading