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

feat(rt): introduce opaque KMP default HTTP client engine #606

Merged
merged 8 commits into from
Mar 21, 2022

Conversation

aajtodd
Copy link
Contributor

@aajtodd aajtodd commented Mar 18, 2022

Issue #

closes #602

Description of changes

  • Introduce a new artifact http-client-engine-default that provides an HttpClientEngine implementation that works for KMP. This allows us to not hard code against a specific engine (which would require that engine be KMP compatible).
  • Fix ktor engine handling of query parameter encoding. We had some signing issues with presign tests because the presigner implementation currently encodes the query parameters. Ktor was encoding these a separate time resulting in query parameters like %252F instead of just %2F when / was present in a query value. The fix for this was to turn off any encoding by ktor and use the encoding used by the SDK.
  • Refactor the KtorEngine into common and make it a utility that wraps any ktor compatible engine rather than being synonymous with a particular engine. See breaking change notes below

Breaking Change

This PR marks the KtorEngine constructor as deprecated since it no longer provides a concrete engine. Instead it now wraps some ktor compatible engine.

val client = FooClient {
    ...
    httpClientEngine = KtorEngine()
}

Users that are currently constructing service clients like the above snippet will no longer compile on update. The default engine is now the Ktor OKHttp engine and so the recommended fix would be to simply not override httpClientEngine.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

aajtodd added 4 commits March 18, 2022 12:02
Deprecate KtorEngine as synonymous with OkHttp. The `http-client-engine-ktor` is
now a utility wrapper for _any_ Ktor compliant engine. The OkHttp engine
has been moved to `http-client-engine-default` as the default engine on
JVM.

BREAKING CHANGE:

Use of explicit `httpClientEngine = KtorEngine()` will no longer
compile. The default engine will be synonymous with OkHttp
making it unnecessary to set this engine. Users should remove
this explicit SDK client configuration.
@aajtodd aajtodd requested a review from ianbotsf March 18, 2022 17:31
Comment on lines 8 to 18
/**
* Factory function to create a new HTTP client engine using the default for the current KMP target
*/
fun SdkHttpEngine(config: HttpClientEngineConfig = HttpClientEngineConfig.Default): HttpClientEngine =
newDefaultHttpEngine(config)

fun SdkHttpEngine(block: HttpClientEngineConfig.Builder.() -> Unit): HttpClientEngine {
val builder = HttpClientEngineConfig.Builder().apply(block)
val config = HttpClientEngineConfig(builder)
return SdkHttpEngine(config)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment: The name SdkHttpEngine feels ambiguous and inaccurate to me. Technically the CRT engine is also an "SDK HTTP Engine". It'd be clearer to me if it were DefaultHttpEngine or something similar.

"use KtorEngine to wrap it. This constructor will be removed in a future release before GA.",
level = DeprecationLevel.ERROR
)
constructor(config: HttpClientEngineConfig = HttpClientEngineConfig.Default) : this(DeprecationEngine)
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment: It seems cleaner to throw an error immediately upon invoking KtorEngine() rather than later:

companion object {
    @Suppress("UNUSED_PARAMETER")
    @Deprecated(message = "KtorEngine was previously...", level = DeprecationLevel.ERROR)
    operator fun invoke(config: HttpClientEngineConfig = HttpClientEngineConfig.Default): KtorEngine =
        error("Invoking KtorEngine without an engine is deprecated")
}

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's irrelevant, code doesn't compile if you target this constructor

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 9 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@aajtodd aajtodd merged commit b3704ff into main Mar 21, 2022
@aajtodd aajtodd deleted the default-engine branch March 21, 2022 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default HTTP engine artifacts
2 participants