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

Allow UserAgent feature to have a dynamic source of its value #176

Closed
dalewking opened this issue May 4, 2022 · 1 comment · Fixed by #180
Closed

Allow UserAgent feature to have a dynamic source of its value #176

dalewking opened this issue May 4, 2022 · 1 comment · Fixed by #180
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@dalewking
Copy link

dalewking commented May 4, 2022

I have a case where the UserAgent can change depending on server, so needed it to be dynamic and not a fixed string. Here is the class I came up with in case it is useful to others:

class DynamicUserAgent(val agentProvider: () -> String?) {
    class Config(var agentProvider: () -> String? = { null })

    companion object Feature : HttpClientFeature<Config, DynamicUserAgent> {
        override val key: AttributeKey<DynamicUserAgent> = AttributeKey("DynamicUserAgent")

        override fun prepare(block: Config.() -> Unit): DynamicUserAgent =
            DynamicUserAgent(Config().apply(block).agentProvider)

        override fun install(feature: DynamicUserAgent, scope: HttpClient) {
            scope.requestPipeline.intercept(HttpRequestPipeline.State) {
                feature.agentProvider()
                    ?.let { context.header(HttpHeaders.UserAgent, it) }
            }
        }
    }
}

@Alex009 Alex009 added this to the 0.18.0 milestone May 6, 2022
@Alex009 Alex009 added the enhancement New feature or request label May 6, 2022
Alex009 added a commit that referenced this issue Jul 16, 2022
@Alex009 Alex009 linked a pull request Jul 16, 2022 that will close this issue
Alex009 added a commit that referenced this issue Jul 16, 2022
@Alex009
Copy link
Member

Alex009 commented Jul 16, 2022

will be available in 0.18.0

@Alex009 Alex009 closed this as completed Jul 16, 2022
@Alex009 Alex009 self-assigned this Jul 16, 2022
@Alex009 Alex009 mentioned this issue Jul 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

2 participants