-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
core/src/main/scalajvm/sttp/client4/httpclient/RequestBodyCallback.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package sttp.client4.httpclient | ||
|
||
import sttp.attributes.AttributeKey | ||
|
||
import java.nio.ByteBuffer | ||
|
||
/** Defines a callback to be invoked when subsequent parts of the request body to be sent are created, just before they | ||
* are sent over the network. | ||
* | ||
* When a request is sent, `onInit` is invoked exactly once with the content length (if it is known). This is followed | ||
* by arbitrary number of `onNext` calls. Finally, either `onComplete` or `onError` are called exactly once. | ||
* | ||
* All of the methods should be non-blocking and complete as fast as possible, so as not to obstruct sending data over | ||
* the network. | ||
* | ||
* To register a callback, set the [[RequestBodyCallback.Attribute]] on a request, using the | ||
* [[sttp.client4.Request.attribute]] method. | ||
*/ | ||
trait RequestBodyCallback { | ||
def onInit(contentLength: Option[Long]): Unit | ||
|
||
def onNext(b: ByteBuffer): Unit | ||
|
||
def onComplete(): Unit | ||
def onError(t: Throwable): Unit | ||
} | ||
|
||
object RequestBodyCallback { | ||
|
||
/** The key of the attribute that should be set on a request, to receive callbacks when the request body is sent. */ | ||
val Attribute = AttributeKey[RequestBodyCallback] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Body callbacks | ||
|
||
When using the `HttpClient`-based backends (which includes the `DefaultSyncBackend` and `DefaultFutureBackend` on the | ||
JVM), it is possible to register body-related callbacks. | ||
|
||
This feature is not available in other backends, and setting the attribute described below will have no effect. | ||
|
||
## Request body callbacks | ||
|
||
Defines a callback to be invoked when subsequent parts of the request body to be sent are created, just before they | ||
are sent over the network. The callback is defined through an instance of the `RequestBodyCallback` trait. | ||
|
||
When a request is sent, the `RequestBodyCallback.onInit` method is invoked exactly once with the content length (if it | ||
is known). This is followed by arbitrary number of `onNext` calls. Finally, either `onComplete` or `onError` are called | ||
exactly once. | ||
|
||
All of the methods in the `RequestBodyCallback` implementation should be non-blocking and complete as fast as possible, | ||
so as not to obstruct sending data over the network. | ||
|
||
To register a callback, set the `RequestBodyCallback.Attribute` on a request. For example: | ||
|
||
```scala mdoc:compile-only | ||
import sttp.client4.* | ||
import sttp.client4.httpclient.{HttpClientSyncBackend, RequestBodyCallback} | ||
import java.nio.ByteBuffer | ||
import java.io.File | ||
|
||
val backend = HttpClientSyncBackend() | ||
|
||
val fileToSend: File = ??? | ||
val callback = new RequestBodyCallback { | ||
override def onInit(contentLength: Option[Long]): Unit = println(s"expected content length: $contentLength") | ||
override def onNext(b: ByteBuffer): Unit = println(s"next, bytes: ${b.remaining()}") | ||
override def onComplete(): Unit = println(s"complete") | ||
override def onError(t: Throwable): Unit = println(s"error: ${t.getMessage}") | ||
} | ||
|
||
val response = basicRequest | ||
.get(uri"http://example.com") | ||
.body(fileToSend) | ||
.attribute(RequestBodyCallback.Attribute, callback) | ||
.send(backend) | ||
``` |