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

Support custom base url #80

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 29 additions & 3 deletions Sources/OpenAISwift/OpenAISwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public enum OpenAIError: Error {
}

public class OpenAISwift {
fileprivate(set) var token: String?
fileprivate(set) var baseURL: String?
fileprivate let config: Config
fileprivate let handler = ServerSentEventsHandler()

Expand Down Expand Up @@ -40,8 +42,10 @@ public class OpenAISwift {
}
}

public init(config: Config) {
self.config = config
public init(authToken: String, baseURL: String? = nil, config: Config = Config()) {
self.token = authToken
self.baseURL = baseURL
self.config = Config()
}
}

Expand Down Expand Up @@ -303,7 +307,7 @@ extension OpenAISwift {
}

private func prepareRequest<BodyType: Encodable>(_ endpoint: OpenAIEndpointProvider.API, body: BodyType) -> URLRequest {
var urlComponents = URLComponents(url: URL(string: config.baseURL)!, resolvingAgainstBaseURL: true)
var urlComponents = URLComponents(url: prepareBaseURL(endpoint), resolvingAgainstBaseURL: true)
urlComponents?.path = config.endpointProvider.getPath(api: endpoint)
var request = URLRequest(url: urlComponents!.url!)
request.httpMethod = config.endpointProvider.getMethod(api: endpoint)
Expand All @@ -319,6 +323,28 @@ extension OpenAISwift {

return request
}

private func prepareBaseURL(_ endpoint: Endpoint) -> URL{

if var baseURL = baseURL, !baseURL.isEmpty {
// needs https:// for request
if baseURL.lowercased().hasPrefix("http://") {
// change http to https
baseURL = baseURL.replacingOccurrences(of: "http://", with: "https://", options: .caseInsensitive)
}
if !baseURL.lowercased().hasPrefix("https://") {
// add https:// prefix
baseURL = "https://" + baseURL
}
while baseURL.hasSuffix("/") {
baseURL.removeLast()
}
return URL(string: baseURL)!
}

return URL(string: endpoint.baseURL())!
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do better with the force unwrapping like throw an assertion to the client?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to look like https://api.openai.com. If not, throws an assertion error to let user know?


}
}

extension OpenAISwift {
Expand Down