-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
[Experiment] Ktor Call.Factory #7326
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip | ||
networkTimeout=10000 | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Module okhttp-ktor | ||
|
||
OkHttp Ktor library. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
OkHttp Ktor | ||
=========== | ||
|
||
Support for Kotlin Ktor clients |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import com.vanniktech.maven.publish.JavadocJar | ||
import com.vanniktech.maven.publish.KotlinMultiplatform | ||
|
||
plugins { | ||
kotlin("multiplatform") | ||
id("org.jetbrains.dokka") | ||
id("com.vanniktech.maven.publish.base") | ||
id("binary-compatibility-validator") | ||
} | ||
|
||
kotlin { | ||
if (kmpJsEnabled) { | ||
js { | ||
compilations.all { | ||
kotlinOptions { | ||
moduleKind = "umd" | ||
sourceMap = true | ||
metaInfo = true | ||
} | ||
} | ||
nodejs { | ||
testTask { | ||
useMocha { | ||
timeout = "30s" | ||
} | ||
} | ||
} | ||
browser { | ||
} | ||
} | ||
} | ||
|
||
sourceSets { | ||
val nonJvmMain = create("nonJvmMain") { | ||
dependencies { | ||
api(projects.okhttp) | ||
api(libs.squareup.okio) | ||
api(libs.kotlin.stdlib) | ||
api("io.ktor:ktor-client-core:2.0.2") | ||
} | ||
} | ||
|
||
val jsMain = getByName("jsMain") { | ||
dependencies { | ||
dependsOn(nonJvmMain) | ||
implementation("com.squareup.okio:okio-js:3.2.0") | ||
} | ||
} | ||
|
||
getByName("jsTest") { | ||
dependencies { | ||
dependsOn(nonJvmMain) | ||
dependsOn(jsMain) | ||
implementation(libs.kotlin.test.js) | ||
implementation(libs.kotlinx.coroutines.test) | ||
implementation(libs.assertk) | ||
} | ||
} | ||
} | ||
} | ||
|
||
mavenPublishing { | ||
configure( | ||
KotlinMultiplatform(javadocJar = JavadocJar.Dokka("dokkaGfm")) | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2022 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package okhttp3.ktor | ||
|
||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.http.contentLength | ||
import io.ktor.http.contentType | ||
import okhttp3.MediaType | ||
import okhttp3.ResponseBody | ||
import okio.BufferedSource | ||
|
||
class HttpResponseBody( | ||
private val httpResponse: HttpResponse, | ||
private val source: BufferedSource | ||
) : ResponseBody() { | ||
override fun contentType(): MediaType? = httpResponse.contentType()?.toMediaType() | ||
|
||
override fun contentLength(): Long = httpResponse.contentLength() ?: -1 | ||
|
||
override fun source(): BufferedSource = source | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2022 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package okhttp3.ktor | ||
|
||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.client.statement.bodyAsChannel | ||
import io.ktor.utils.io.core.readBytes | ||
import okhttp3.RequestBody | ||
import okhttp3.ResponseBody | ||
|
||
actual suspend fun HttpResponse.toHttpResponseBody(): ResponseBody { | ||
val bytes = this.bodyAsChannel().readRemaining().readBytes() | ||
val buffer = okio.Buffer() | ||
buffer.write(bytes) | ||
return HttpResponseBody(this, buffer) | ||
} | ||
|
||
actual suspend fun RequestBody.toHttpRequestBody(): Any { | ||
val buffer = okio.Buffer() | ||
this.writeTo(buffer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also blocking. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Ktor doc examples aren't great here, using readBytes() https://ktor.io/docs/request.html#upload_file And "This function accepts different types of payloads, including plain text, arbitrary class instances, form data, byte arrays, and so on" |
||
return buffer.readByteArray() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I need the most help here.
buffer.write(bytes)
below is blocking, so at least that should be sorted out.