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

[REQ][Swift5][Client] Ability to set up an interceptor for Alamofire

Open ondra-marik opened this issue 1 year ago • 0 comments

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

I'm generating client with Alamofire framework and it seems quite hard to be able to use custom interceptor (migrating from manual code). The way the API is currently generated, every request creates its own Alamofire Session and while the method itself has interceptor parameter, it's optional and all execute function in AlamofireRequestBuilder is not using it:

// AlamofireImplementations.swift
/**
     May be overridden by a subclass if you want to control the session
     configuration.
     */
    open func createAlamofireSession(interceptor: RequestInterceptor? = nil) -> Alamofire.Session {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = buildHeaders()
        return Alamofire.Session(configuration: configuration,
                                 interceptor: interceptor)
    }
override open func execute(_ apiResponseQueue: DispatchQueue = MPNetworkingAPI.apiResponseQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, ErrorResponse>) -> Void) -> RequestTask {
        let managerId = UUID().uuidString
        // Create a new manager for each request to customize its request header
        let manager = createAlamofireSession()
        managerStore[managerId] = manager
        ...

While possible to subclass AlamofireRequestBuilder and override the above functions, there are limited options how to add the interceptor, as request builders are initialised in API endpoint functions, so it is challenging to pass the interceptor into it to possibly use in overriden createAlamofireSession.

Describe the solution you'd like

Possibly add the interceptor as an optional variable in main generated API class, similar to apiResponseQueue and then use it in request builders? Not sure about the proper solution myself.

Describe alternatives you've considered

Tried overriding the request builder factory, as it is the only customisation option, but it seems impossible to pass the interceptor to request builders, since I can't modify neither the constructor, neither function signatures without rewriting all of the API endpoints myself.

Additional context

generate command: generate -i ./api.json -g swift5 --additional-properties=projectName=MyProject,library=alamofire,mapFileBinaryToData=true

ondra-marik avatar May 19 '24 22:05 ondra-marik