openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[REQ] Config option to allow for multiple base paths in swift client

Open reubits opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe.

We want to use multiple APIs with different base paths, but the base path variable is static with fixed class name.

open class OpenAPIClientAPI {
    public static var basePath = "..."

Describe the solution you'd like

A configuration option which would allow us to customize the class name for each API (e.g. MyOpenAPIClientAPI). Also open to suggestions here 😄 .

Describe alternatives you've considered

Generating separate clients and putting them in separate compile targets. This is highly inconvenient.

Additional context

This seems like it would be a common problem and should not be a hard limitation. Please advise if I have missed an existing way of accomplishing this.

reubits avatar May 10 '22 14:05 reubits

Another option would be to pass the base path to each method call with a default value. We are open to PRs 🙂

4brunu avatar Oct 07 '22 15:10 4brunu

Any news about this issue?

c-villain avatar May 27 '24 09:05 c-villain

I don't think so

4brunu avatar May 27 '24 10:05 4brunu

@4brunu do you have any suggestion how to solve this problem? I mean if I have multiple base urls?

c-villain avatar May 27 '24 10:05 c-villain

With the current implementation this is not possible. My suggestion is that someone can create a PR where it's possible to pass the base path to each method call with a default value. This should be easy to do, but it's hard to find time for everything. I can help to review the PR.

4brunu avatar May 27 '24 10:05 4brunu

...it's possible to pass the base path to each method call with a default value. ...

@4brunu how do you think to make it better? May be to add the parameter with {{projectName}}API to each method call?

e.g. 👇🏻

open class OpenAPIClient {
    public static var basePath = "https://base.url"
    public static var customHeaders: [String: String] = [:]
    public static var credential: URLCredential?
    public static var requestBuilderFactory: RequestBuilderFactory = URLSessionRequestBuilderFactory()
    public static var apiResponseQueue: DispatchQueue = .main
}

and add this as parameter in each calling:

open class MyAPI {
...
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
    open class func SomeFunc(
      settings: OpenAPIClient = OpenAPIClient(), // <== Look HERE
      myRequest: MyRequest
) async throws -> MyResponse {
....
       let localVariablePath = "...localPath..."
        let localVariableURLString = settings.basePath + localVariablePath
...

}

c-villain avatar May 27 '24 11:05 c-villain

That might be a good idea, because different endpoints might require different authentication, diferente headers, etc.

4brunu avatar May 27 '24 11:05 4brunu