1
0
mirror of https://github.com/square/okhttp.git synced 2025-07-01 15:41:42 +03:00
Files
okhttp/okhttp-coroutines
Jesse Wilson f745695087 Replace SocketPolicy with a new type, SocketEffect (#8870)
* Replace SocketPolicy with a new type, SocketEffect

SocketPolicy encapsulated two things:
 - a trigger (request start, response body, etc)
 - an effect (close the socket, close the stream, stall, etc.)

It also had some special cases for effects like
failing the TLS handshake.

With this PR there's a new class, SocketEffect, that determines
what to do. It's assigned to one of 5 fields for different
triggers.

# Conflicts:
#	mockwebserver/src/main/kotlin/mockwebserver3/MockWebServer.kt

* apiDump

* Track API change

* Track more API changes

* Spotless

---------

Co-authored-by: Jesse Wilson <jwilson@squareup.com>
2025-06-20 07:46:46 -04:00
..
2022-03-06 15:31:31 +00:00

OkHttp Coroutines

Support for Kotlin clients using coroutines.

val call = client.newCall(request)

call.executeAsync().use { response ->
  withContext(Dispatchers.IO) {
    println(response.body?.string())
  }
}

This is implemented using suspendCancellableCoroutine but uses the standard Dispatcher in OkHttp. This means that by default Kotlin's Dispatchers are not used.

Cancellation if implemented sensibly in both directions. Cancelling a coroutine scope, will cancel the call. Cancelling a call, will throw a CancellationException but not cancel the scope if caught.