Skip to content

Commit 5861c59

Browse files
authored
Merge pull request #180 from JavadFaghih/main
Add scheme, host, and port for buildURL
2 parents f1c05f5 + 3cff989 commit 5861c59

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

Sources/OpenAI/OpenAI.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ final public class OpenAI: OpenAIProtocol {
2222

2323
/// API host. Set this property if you use some kind of proxy or your own server. Default is api.openai.com
2424
public let host: String
25-
25+
public let port: Int
26+
public let scheme: String
2627
/// Default request timeout
2728
public let timeoutInterval: TimeInterval
2829

29-
public init(token: String, organizationIdentifier: String? = nil, host: String = "api.openai.com", timeoutInterval: TimeInterval = 60.0) {
30+
public init(token: String, organizationIdentifier: String? = nil, host: String = "api.openai.com", port: Int = 443, scheme: String = "https", timeoutInterval: TimeInterval = 60.0) {
3031
self.token = token
3132
self.organizationIdentifier = organizationIdentifier
3233
self.host = host
34+
self.port = port
35+
self.scheme = scheme
3336
self.timeoutInterval = timeoutInterval
3437
}
3538
}
@@ -196,8 +199,9 @@ extension OpenAI {
196199

197200
func buildURL(path: String) -> URL {
198201
var components = URLComponents()
199-
components.scheme = "https"
202+
components.scheme = configuration.scheme
200203
components.host = configuration.host
204+
components.port = configuration.port
201205
components.path = path
202206
return components.url!
203207
}

Tests/OpenAITests/OpenAITests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,14 @@ class OpenAITests: XCTestCase {
392392
let configuration = OpenAI.Configuration(token: "foo", organizationIdentifier: "bar", timeoutInterval: 14)
393393
let openAI = OpenAI(configuration: configuration, session: self.urlSession)
394394
let chatsURL = openAI.buildURL(path: .chats)
395-
XCTAssertEqual(chatsURL, URL(string: "https://api.openai.com/v1/chat/completions"))
395+
XCTAssertEqual(chatsURL, URL(string: "https://api.openai.com:443/v1/chat/completions"))
396396
}
397397

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

0 commit comments

Comments
 (0)