-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add onBodyReceived callback, use in logs & metrics (#2427)
- Loading branch information
Showing
56 changed files
with
813 additions
and
399 deletions.
There are no files selected for viewing
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
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
2 changes: 1 addition & 1 deletion
2
core/src/main/scala/sttp/client4/internal/LimitedInputStream.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
35 changes: 35 additions & 0 deletions
35
core/src/main/scala/sttp/client4/internal/OnEndInputStream.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,35 @@ | ||
package sttp.client4.internal | ||
|
||
import java.io.InputStream | ||
|
||
class OnEndInputStream(delegate: InputStream, callback: () => Unit) extends InputStream { | ||
private var callbackCalled = false | ||
|
||
override def read(): Int = { | ||
val result = delegate.read() | ||
if (result == -1) onEnd() | ||
result | ||
} | ||
|
||
override def read(b: Array[Byte]): Int = { | ||
val result = delegate.read(b) | ||
if (result == -1) onEnd() | ||
result | ||
} | ||
|
||
override def read(b: Array[Byte], off: Int, len: Int): Int = { | ||
val result = delegate.read(b, off, len) | ||
if (result == -1) onEnd() | ||
result | ||
} | ||
|
||
override def close(): Unit = { | ||
onEnd() | ||
delegate.close() | ||
} | ||
|
||
private def onEnd(): Unit = if (!callbackCalled) { | ||
callbackCalled = true | ||
callback() | ||
} | ||
} |
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
83 changes: 71 additions & 12 deletions
83
core/src/main/scala/sttp/client4/listener/RequestListener.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 |
---|---|---|
@@ -1,29 +1,88 @@ | ||
package sttp.client4.listener | ||
|
||
import sttp.monad.MonadError | ||
import sttp.client4.{GenericRequest, Response} | ||
import sttp.client4.GenericRequest | ||
import sttp.shared.Identity | ||
import sttp.model.ResponseMetadata | ||
import sttp.client4.ResponseException | ||
|
||
/** A listener to be used by the [[ListenerBackend]] to get notified on request lifecycle events. | ||
* | ||
* @tparam L | ||
* Type of a value ("tag") that is associated with a request, and passed the response (or exception) is available. | ||
* Use `Unit` if no special value should be associated with a request. | ||
* Type of a value ("tag") that is associated with a request, and passed to response/exception callbacks. Use `Unit` | ||
* if no special value should be associated with a request. | ||
*/ | ||
trait RequestListener[F[_], L] { | ||
def beforeRequest(request: GenericRequest[_, _]): F[L] | ||
def requestException(request: GenericRequest[_, _], tag: L, e: Exception): F[Unit] | ||
def requestSuccessful(request: GenericRequest[_, _], response: Response[_], tag: L): F[Unit] | ||
|
||
/** Called before a request is sent. */ | ||
def before(request: GenericRequest[_, _]): F[L] | ||
|
||
/** Called when the response body has been fully received (see [[sttp.client4.Request#onBodyReceived]]), but not yet | ||
* fully handled (e.g. parsed). | ||
* | ||
* This method is not called when there's an exception while reading the response body, decompressing, or for | ||
* WebSocket requests. | ||
* | ||
* Note that this method must run any effects immediately, as it returns a `Unit`, without the `F` wrapper. | ||
*/ | ||
def responseBodyReceived(request: GenericRequest[_, _], response: ResponseMetadata, tag: L): Unit | ||
|
||
/** Called when the request has been handled, as specified by the response description. | ||
* | ||
* The [[responseBodyReceived]] might be called before this method (for safe, non-WebSocket requests), after (for | ||
* requests with `...Unsafe` response descriptions), or not at all (for WebSocket requests). | ||
* | ||
* @param exception | ||
* A [[ResponseException]] that might occur when handling the response: when the raw response is successfully | ||
* received via the network, but e.g. a parsing or decompression exception occurs. | ||
*/ | ||
def responseHandled( | ||
request: GenericRequest[_, _], | ||
response: ResponseMetadata, | ||
tag: L, | ||
exception: Option[ResponseException[_]] | ||
): F[Unit] | ||
|
||
/** Called when there's an exception, when receiving the response body or handling the response (decompression, | ||
* parsing). The exception is other than [[ResponseException]] - in that case, response metadata is available and | ||
* [[responseHandled]] is called. | ||
* | ||
* The [[responseBodyReceived]] might have been called before this method, but will not be called after. | ||
* | ||
* @param responseBodyReceivedCalled | ||
* Indicates if [[responseBodyReceivedCalled]] has been called before this method. | ||
*/ | ||
def exception( | ||
request: GenericRequest[_, _], | ||
tag: L, | ||
exception: Throwable, | ||
responseBodyReceivedCalled: Boolean | ||
): F[Unit] | ||
} | ||
|
||
object RequestListener { | ||
def lift[F[_], L](delegate: RequestListener[Identity, L], monadError: MonadError[F]): RequestListener[F, L] = | ||
new RequestListener[F, L] { | ||
override def beforeRequest(request: GenericRequest[_, _]): F[L] = | ||
monadError.eval(delegate.beforeRequest(request)) | ||
override def requestException(request: GenericRequest[_, _], tag: L, e: Exception): F[Unit] = | ||
monadError.eval(delegate.requestException(request, tag, e)) | ||
override def requestSuccessful(request: GenericRequest[_, _], response: Response[_], tag: L): F[Unit] = | ||
monadError.eval(delegate.requestSuccessful(request, response, tag)) | ||
override def before(request: GenericRequest[_, _]): F[L] = | ||
monadError.eval(delegate.before(request)) | ||
|
||
override def responseBodyReceived(request: GenericRequest[_, _], response: ResponseMetadata, tag: L): Unit = | ||
delegate.responseBodyReceived(request, response, tag) | ||
|
||
override def responseHandled( | ||
request: GenericRequest[_, _], | ||
response: ResponseMetadata, | ||
tag: L, | ||
e: Option[ResponseException[_]] | ||
): F[Unit] = | ||
monadError.eval(delegate.responseHandled(request, response, tag, e)) | ||
|
||
override def exception( | ||
request: GenericRequest[_, _], | ||
tag: L, | ||
e: Throwable, | ||
responseBodyReceivedCalled: Boolean | ||
): F[Unit] = | ||
monadError.eval(delegate.exception(request, tag, e, responseBodyReceivedCalled)) | ||
} | ||
} |
Oops, something went wrong.