Skip to content

Add the resource to client requests when using the Resources Plugin. #4747

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.resources.*
import io.ktor.util.AttributeKey
import io.ktor.client.request.delete as deleteBuilder
import io.ktor.client.request.get as getBuilder
import io.ktor.client.request.head as headBuilder
Expand Down Expand Up @@ -37,6 +38,7 @@ public suspend inline fun <reified T : Any> HttpClient.get(
): HttpResponse {
val resources = resources()
return getBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -53,6 +55,7 @@ public suspend inline fun <reified T : Any> HttpClient.post(
): HttpResponse {
val resources = resources()
return postBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -69,6 +72,7 @@ public suspend inline fun <reified T : Any> HttpClient.put(
): HttpResponse {
val resources = resources()
return putBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -85,6 +89,7 @@ public suspend inline fun <reified T : Any> HttpClient.delete(
): HttpResponse {
val resources = resources()
return deleteBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -101,6 +106,7 @@ public suspend inline fun <reified T : Any> HttpClient.patch(
): HttpResponse {
val resources = resources()
return patchBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -117,6 +123,7 @@ public suspend inline fun <reified T : Any> HttpClient.options(
): HttpResponse {
val resources = resources()
return optionsBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -133,6 +140,7 @@ public suspend inline fun <reified T : Any> HttpClient.head(
): HttpResponse {
val resources = resources()
return headBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -149,6 +157,7 @@ public suspend inline fun <reified T : Any> HttpClient.request(
): HttpResponse {
val resources = resources()
return requestBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -165,6 +174,7 @@ public suspend inline fun <reified T : Any> HttpClient.prepareGet(
): HttpStatement {
val resources = resources()
return prepareGetBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -181,6 +191,7 @@ public suspend inline fun <reified T : Any> HttpClient.preparePost(
): HttpStatement {
val resources = resources()
return preparePostBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -197,6 +208,7 @@ public suspend inline fun <reified T : Any> HttpClient.preparePut(
): HttpStatement {
val resources = resources()
return preparePutBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -213,6 +225,7 @@ public suspend inline fun <reified T : Any> HttpClient.prepareDelete(
): HttpStatement {
val resources = resources()
return prepareDeleteBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -229,6 +242,7 @@ public suspend inline fun <reified T : Any> HttpClient.preparePatch(
): HttpStatement {
val resources = resources()
return preparePatchBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -245,6 +259,7 @@ public suspend inline fun <reified T : Any> HttpClient.prepareOptions(
): HttpStatement {
val resources = resources()
return prepareOptionsBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -261,6 +276,7 @@ public suspend inline fun <reified T : Any> HttpClient.prepareHead(
): HttpStatement {
val resources = resources()
return prepareHeadBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
Expand All @@ -277,11 +293,18 @@ public suspend inline fun <reified T : Any> HttpClient.prepareRequest(
): HttpStatement {
val resources = resources()
return prepareRequestBuilder {
attributes.put(RESOURCE, resource)
href(resources.resourcesFormat, resource, url)
builder()
}
}

/**
* The instance of the [Resource] annotated class used to for a request.
* Plugins may want to utilize this for monitoring.
*/
public val RESOURCE: AttributeKey<Any> = AttributeKey<Any>("RESOURCE")

@PublishedApi
internal fun HttpClient.resources(): io.ktor.resources.Resources {
return pluginOrNull(Resources) ?: throw IllegalStateException("Resources plugin is not installed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import io.ktor.client.engine.mock.*
import io.ktor.client.plugins.api.createClientPlugin
import io.ktor.client.plugins.resources.*
import io.ktor.client.plugins.resources.Resources
import io.ktor.client.request.*
Expand All @@ -12,6 +13,7 @@ import io.ktor.http.*
import io.ktor.resources.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertSame

class ResourcesTest {

Expand Down Expand Up @@ -149,4 +151,34 @@ class ResourcesTest {
assertEquals(HttpStatusCode.OK, response.status)
}
}

@Resource("path/{id}")
class PathWithOptionalQueryParameter(val id: Int, val query: String? = null)

@Test
fun `check template is added as attribute`() = testWithEngine(MockEngine) {
config {
install(Resources)
engine {
addHandler { request ->
respondOk()
}
}
}
test { client ->
val resource = PathWithOptionalQueryParameter(10, "clyde")
val clientWithAssertions = client.config {
install(
createClientPlugin("check resource attribute is set") {
onRequest { call, _ ->
assertSame(resource, call.attributes[RESOURCE])
}
}
)
}

val response = clientWithAssertions.get(resource)
assertEquals(response.status, HttpStatusCode.OK)
}
}
}