diff options
-rw-r--r-- | README.md | 17 | ||||
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/RequestT.scala | 5 | ||||
-rw-r--r-- | tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala | 5 |
3 files changed, 21 insertions, 6 deletions
@@ -386,11 +386,19 @@ OkHttp fully supports HTTP/2. It is also entirely possible to write your own backend (if so, please consider contributing!) or wrapping an existing one. You can even write completely -generic wrappers for any delegate backend, as the each backend comes equipped -with a monad for the response wrapper. +generic wrappers for any delegate backend, as each backend comes equipped +with a monad for the response type. This brings the possibility to `map` and +`flatMap` over responses. -This brings the possibility to `map` and `flatMap` over responses. That way you -could implement e.g. a logging or metric-capturing wrapper. +Possible use-cases for wrapper-backend include: + +* logging +* capturing metrics +* request signing (transforming the request before sending it to the delegate) + +To pass some context to wrapper-backends, requests can be *tagged*. Each +`RequestT` instance contains a `tags: Map[String, Any]` field. This is unused +by http, but can be used e.g. to pass a metric name or logging context. ## JSON @@ -423,7 +431,6 @@ val response: Either[io.circe.Error, Response] = .send() ``` - ## Request type All request descriptions have type `RequestT[U, T, S]` (T as in Template). diff --git a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala index 27ed7f3..020e926 100644 --- a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala +++ b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala @@ -225,6 +225,11 @@ case class RequestT[U[_], T, +S]( def followRedirects(fr: Boolean): RequestT[U, T, S] = this.copy(options = options.copy(followRedirects = fr)) + def tag(k: String, v: Any): RequestT[U, T, S] = + this.copy(tags = tags + (k -> v)) + + def tag(k: String): Option[Any] = tags.get(k) + def send[R[_]]()(implicit handler: SttpHandler[R, S], isIdInRequest: IsIdInRequest[U]): R[Response[T]] = { // we could avoid the asInstanceOf by creating an artificial copy diff --git a/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala b/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala index 6292ecd..7e4d5a9 100644 --- a/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala +++ b/tests/src/test/scala/com/softwaremill/sttp/testHelpers.scala @@ -17,7 +17,10 @@ import scala.concurrent.duration._ import scala.language.higherKinds import scalaz._ -trait TestHttpServer extends BeforeAndAfterAll with ScalaFutures with TestingPatience { +trait TestHttpServer + extends BeforeAndAfterAll + with ScalaFutures + with TestingPatience { this: Suite => protected implicit val actorSystem: ActorSystem = ActorSystem("sttp-test") import actorSystem.dispatcher |