Skip to content

Commit

Permalink
Merge pull request #180 from JavadFaghih/main
Browse files Browse the repository at this point in the history
Add scheme, host, and port for buildURL
  • Loading branch information
ingvarus-bc committed Mar 22, 2024
2 parents f1c05f5 + 3cff989 commit 5861c59
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
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

0 comments on commit 5861c59

Please sign in to comment.