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

[Kotlin][Multiplatform] Support custom Ktor HTTP client configuration #10968

Conversation

tkirshboim
Copy link
Contributor

@tkirshboim tkirshboim commented Nov 26, 2021

PR Description

The Problem

It is currently not possible to configure the Ktor HTTP client when using generated Kotlin Multiplatform code.
Configuring the HTTP client can be useful for logging or customising request headers, to name a few use cases.

This issue has been raised in #10130 .

The Solution

This PR makes it possible to configure the Ktor HTTP client by allowing to pass an optional httpClientConfig parameter to the generated API classes.

An example of how the new httpClientConfig parameter can be used can be seen here:

private val petApi = PetApi(httpClientConfig = createHttpClientConfig())

private fun createHttpClientConfig(): ((HttpClientConfig<*>) -> Unit) =
        { clientConfig ->
            clientConfig.install(UserAgent) {
                agent = "my-pet-store-app/${Platform().platform}"
            }
            clientConfig.install(Logging) {
                logger = object : Logger {
                    override fun log(message: String) {
                        println("HttpClient: $message")
                    }
                }
                level = LogLevel.ALL
            }
        }

Fixes #10130

CCs

Kotlin technical committee: @jimschubert @dr4ke616 @karismann @Zomzog @andrewemery @4brunu @yutaka0m

Reporter of #10130 : @krzema12

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (5.3.0), 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@tkirshboim tkirshboim force-pushed the kotlin-multiplatform-configurable-http-client branch from 010d999 to fa1585b Compare November 29, 2021 13:47
@@ -25,16 +26,32 @@ import {{packageName}}.auth.*
{{#nonPublicApi}}internal {{/nonPublicApi}}open class ApiClient(
private val baseUrl: String,
httpClientEngine: HttpClientEngine?,
httpClientConfig: ((HttpClientConfig<*>) -> Unit)?,
Copy link
Contributor

Choose a reason for hiding this comment

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

@tkirshboim can you please make this with null value by default and remove the extra constructor?

httpClientConfig: ((HttpClientConfig<*>) -> Unit)? = null,

Copy link
Contributor Author

@tkirshboim tkirshboim Nov 29, 2021

Choose a reason for hiding this comment

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

@4brunu ✅ Done.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, let's wait for the CI to finish 👍

Copy link
Contributor Author

@tkirshboim tkirshboim Nov 29, 2021

Choose a reason for hiding this comment

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

@4brunu CI passed. ☺️

Copy link
Contributor

@4brunu 4brunu left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

@wing328 wing328 merged commit 6406e5c into OpenAPITools:master Nov 30, 2021
@tkirshboim tkirshboim deleted the kotlin-multiplatform-configurable-http-client branch November 30, 2021 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG][Kotlin][Multiplatform] Content-Type is not customizable when fetching files
3 participants