Skip to content

Commit

Permalink
Add QUERY method support
Browse files Browse the repository at this point in the history
  • Loading branch information
desiderantes committed Oct 8, 2024
1 parent f277142 commit 63a5ced
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions okhttp/api/okhttp.api
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ public class okhttp3/Request$Builder {
public fun patch (Lokhttp3/RequestBody;)Lokhttp3/Request$Builder;
public fun post (Lokhttp3/RequestBody;)Lokhttp3/Request$Builder;
public fun put (Lokhttp3/RequestBody;)Lokhttp3/Request$Builder;
public fun query (Lokhttp3/RequestBody;)Lokhttp3/Request$Builder;
public fun removeHeader (Ljava/lang/String;)Lokhttp3/Request$Builder;
public fun tag (Ljava/lang/Class;Ljava/lang/Object;)Lokhttp3/Request$Builder;
public fun tag (Ljava/lang/Object;)Lokhttp3/Request$Builder;
Expand Down
8 changes: 5 additions & 3 deletions okhttp/src/main/kotlin/okhttp3/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,10 @@ class Cache internal constructor(
return null
}

if (requestMethod != "GET") {
// Don't cache non-GET responses. We're technically allowed to cache HEAD requests and some
// POST requests, but the complexity of doing so is high and the benefit is low.
if (requestMethod !in CACHEABLE_METHODS) {
// Don't cache non-GET and non-QUERY responses. We're technically allowed to cache HEAD
// requests and some POST requests, but the complexity of doing so is high and the benefit
// is low.
return null
}

Expand Down Expand Up @@ -738,6 +739,7 @@ class Cache internal constructor(
private const val ENTRY_METADATA = 0
private const val ENTRY_BODY = 1
private const val ENTRY_COUNT = 2
private val CACHEABLE_METHODS = setOf("GET", "QUERY")

@JvmStatic
fun key(url: HttpUrl): String = url.toString().encodeUtf8().md5().hex()
Expand Down
3 changes: 3 additions & 0 deletions okhttp/src/main/kotlin/okhttp3/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import okhttp3.internal.commonMethod
import okhttp3.internal.commonPatch
import okhttp3.internal.commonPost
import okhttp3.internal.commonPut
import okhttp3.internal.commonQuery
import okhttp3.internal.commonRemoveHeader
import okhttp3.internal.commonTag
import okhttp3.internal.commonToString
Expand Down Expand Up @@ -276,6 +277,8 @@ class Request internal constructor(builder: Builder) {

open fun patch(body: RequestBody): Builder = commonPatch(body)

open fun query(body: RequestBody): Builder = commonQuery(body)

open fun method(
method: String,
body: RequestBody?,
Expand Down
2 changes: 2 additions & 0 deletions okhttp/src/main/kotlin/okhttp3/internal/-RequestCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ fun Request.Builder.commonPut(body: RequestBody): Request.Builder = method("PUT"

fun Request.Builder.commonPatch(body: RequestBody): Request.Builder = method("PATCH", body)

fun Request.Builder.commonQuery(body: RequestBody): Request.Builder = method("QUERY", body)

fun Request.Builder.commonMethod(
method: String,
body: RequestBody?,
Expand Down
4 changes: 4 additions & 0 deletions okhttp/src/test/java/okhttp3/RequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ class RequestTest {
val patch = Request.Builder().url("http://localhost/api").patch(body).build()
assertThat(patch.method).isEqualTo("PATCH")
assertThat(patch.body).isEqualTo(body)

val query = Request.Builder().url("http://localhost/api").query(body).build()
assertThat(query.method).isEqualTo("QUERY")
assertThat(query.body).isEqualTo(body)
}

@Test
Expand Down

0 comments on commit 63a5ced

Please sign in to comment.