From 71f7ca178a01d2f34f1fb516dce70e5ced3390ef Mon Sep 17 00:00:00 2001 From: JavadFaghih <47854053+JavadFaghih@users.noreply.github.com> Date: Sat, 9 Mar 2024 12:47:24 +0300 Subject: [PATCH 1/2] add scheme, host, and port for buildURL --- Sources/OpenAI/OpenAI.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Sources/OpenAI/OpenAI.swift b/Sources/OpenAI/OpenAI.swift index 199e3eb3..5ff52833 100644 --- a/Sources/OpenAI/OpenAI.swift +++ b/Sources/OpenAI/OpenAI.swift @@ -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 } } @@ -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! } From 3cff989cc7f187f17cb0eb96efc3dd74122f5e5f Mon Sep 17 00:00:00 2001 From: Ihor Makhnyk Date: Fri, 22 Mar 2024 13:43:37 +0200 Subject: [PATCH 2/2] Fix tests - port to URL --- Tests/OpenAITests/OpenAITests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/OpenAITests/OpenAITests.swift b/Tests/OpenAITests/OpenAITests.swift index b98c3caa..bae3e015 100644 --- a/Tests/OpenAITests/OpenAITests.swift +++ b/Tests/OpenAITests/OpenAITests.swift @@ -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")) } }