From b0a954c43ef82d904bb5dde3d59a448f8be687ee Mon Sep 17 00:00:00 2001 From: adamw Date: Tue, 10 Dec 2024 10:26:56 +0100 Subject: [PATCH] Move request logging --- .../scala/sttp/client4/RequestOptions.scala | 4 +- .../src/main/scala/sttp/client4/SttpApi.scala | 4 +- .../main/scala/sttp/client4/logging/Log.scala | 35 +++++++---------- .../scala/sttp/client4/requestBuilder.scala | 39 ++++++++++--------- 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/core/src/main/scala/sttp/client4/RequestOptions.scala b/core/src/main/scala/sttp/client4/RequestOptions.scala index 51ec852ed1..bd28b0bdf2 100644 --- a/core/src/main/scala/sttp/client4/RequestOptions.scala +++ b/core/src/main/scala/sttp/client4/RequestOptions.scala @@ -2,6 +2,7 @@ package sttp.client4 import scala.concurrent.duration.Duration import sttp.model.HttpVersion +import sttp.client4.logging.LoggingOptions case class RequestOptions( followRedirects: Boolean, @@ -9,5 +10,6 @@ case class RequestOptions( maxRedirects: Int, redirectToGet: Boolean, disableAutoDecompression: Boolean, - httpVersion: Option[HttpVersion] + httpVersion: Option[HttpVersion], + loggingOptions: LoggingOptions ) diff --git a/core/src/main/scala/sttp/client4/SttpApi.scala b/core/src/main/scala/sttp/client4/SttpApi.scala index 67deeb5e86..5dacd0af52 100644 --- a/core/src/main/scala/sttp/client4/SttpApi.scala +++ b/core/src/main/scala/sttp/client4/SttpApi.scala @@ -10,6 +10,7 @@ import scala.concurrent.duration._ import sttp.capabilities.Streams import sttp.capabilities.Effect import sttp.client4.wrappers.FollowRedirectsBackend +import sttp.client4.logging.LoggingOptions trait SttpApi extends SttpExtensions with UriInterpolator { val DefaultReadTimeout: Duration = 1.minute @@ -30,7 +31,8 @@ trait SttpApi extends SttpExtensions with UriInterpolator { FollowRedirectsBackend.MaxRedirects, redirectToGet = false, disableAutoDecompression = false, - httpVersion = None + httpVersion = None, + loggingOptions = LoggingOptions() ), Map() ) diff --git a/core/src/main/scala/sttp/client4/logging/Log.scala b/core/src/main/scala/sttp/client4/logging/Log.scala index d697cdeb37..afcd7dfd31 100644 --- a/core/src/main/scala/sttp/client4/logging/Log.scala +++ b/core/src/main/scala/sttp/client4/logging/Log.scala @@ -57,15 +57,11 @@ class DefaultLog[F[_]]( ) extends Log[F] { def beforeRequestSend(request: GenericRequest[_, _]): F[Unit] = - request.loggingOptions match { - case Some(options) => - before( - request, - options.logRequestBody.getOrElse(logRequestBody), - options.logRequestHeaders.getOrElse(logRequestHeaders) - ) - case None => before(request, logRequestBody, logRequestHeaders) - } + before( + request, + request.loggingOptions.logRequestBody.getOrElse(logRequestBody), + request.loggingOptions.logRequestHeaders.getOrElse(logRequestHeaders) + ) private def before(request: GenericRequest[_, _], _logRequestBody: Boolean, _logRequestHeaders: Boolean): F[Unit] = logger( @@ -82,19 +78,14 @@ class DefaultLog[F[_]]( response: Response[_], responseBody: Option[String], elapsed: Option[Duration] - ): F[Unit] = request.loggingOptions match { - case Some(options) => - handleResponse( - request.showBasic, - response, - responseBody, - options.logResponseBody.getOrElse(responseBody.isDefined), - options.logResponseHeaders.getOrElse(logResponseHeaders), - elapsed - ) - case None => - handleResponse(request.showBasic, response, responseBody, responseBody.isDefined, logResponseHeaders, elapsed) - } + ): F[Unit] = handleResponse( + request.showBasic, + response, + responseBody, + request.loggingOptions.logResponseBody.getOrElse(responseBody.isDefined), + request.loggingOptions.logResponseHeaders.getOrElse(logResponseHeaders), + elapsed + ) private def handleResponse( showBasic: String, diff --git a/core/src/main/scala/sttp/client4/requestBuilder.scala b/core/src/main/scala/sttp/client4/requestBuilder.scala index aa5a3e9f9d..af3968111e 100644 --- a/core/src/main/scala/sttp/client4/requestBuilder.scala +++ b/core/src/main/scala/sttp/client4/requestBuilder.scala @@ -302,8 +302,6 @@ trait PartialRequestBuilder[+PR <: PartialRequestBuilder[PR, R], +R] */ def autoDecompressionDisabled: Boolean = options.disableAutoDecompression - // TODO: add similar functionality to Response - /** Set the HTTP version with which this request should be sent. Supported only in a few backends. */ def httpVersion(version: HttpVersion): PR = withOptions(options.copy(httpVersion = Some(version))) @@ -315,30 +313,33 @@ trait PartialRequestBuilder[+PR <: PartialRequestBuilder[PR, R], +R] */ def httpVersion: Option[HttpVersion] = options.httpVersion - private val loggingOptionsTagKey = "loggingOptions" - - /** Will only have effect when using the `LoggingBackend` */ - def logSettings( + /** Sets per-request logging options. Will only have effect when using the [[sttp.client4.logging.LoggingBackend]] + * wrapper. + */ + def loggingOptions( logRequestBody: Option[Boolean] = None, logResponseBody: Option[Boolean] = None, logRequestHeaders: Option[Boolean] = None, logResponseHeaders: Option[Boolean] = None - ): PR = { - val loggingOptions = LoggingOptions( - logRequestBody = logRequestBody, - logResponseBody = logResponseBody, - logRequestHeaders = logRequestHeaders, - logResponseHeaders = logResponseHeaders + ): PR = withOptions( + options.copy(loggingOptions = + LoggingOptions( + logRequestBody = logRequestBody, + logResponseBody = logResponseBody, + logRequestHeaders = logRequestHeaders, + logResponseHeaders = logResponseHeaders + ) ) - this.tag(loggingOptionsTagKey, loggingOptions) - } + ) - def logSettings( - loggingOptions: Option[LoggingOptions] - ): PR = - this.tag(loggingOptionsTagKey, loggingOptions) + /** Sets per-request logging options. Will only have effect when using the [[sttp.client4.logging.LoggingBackend]] + * wrapper. + */ + def loggingOptions(loggingOptions: LoggingOptions): PR = withOptions(options.copy(loggingOptions = loggingOptions)) - def loggingOptions: Option[LoggingOptions] = tag(loggingOptionsTagKey).asInstanceOf[Option[LoggingOptions]] + /** The per-request logging options, which have effect when using the [[sttp.client4.logging.LoggingBackend]] wrapper. + */ + def loggingOptions: LoggingOptions = options.loggingOptions def show( includeBody: Boolean = true,