Skip to content
This repository was archived by the owner on Oct 10, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# https://scalameta.org/scalafmt/docs/configuration.html
version = "2.7.5"
version = "3.0.0"
runner.dialect = scala213source3
preset = defaultWithAlign
align.preset = most
align.multiline = false
Expand All @@ -10,10 +11,14 @@ rewrite.rules = [
PreferCurlyFors,
RedundantBraces,
RedundantParens,
SortImports
SortImports,
SortModifiers
]
rewrite.redundantBraces.stringInterpolation = true
rewrite.redundantBraces.parensForOneLineApply = true
align.tokens = [{code = "="}, {code = "=>", owner = "Case"}, {code = "%", owner = "Term.ApplyInfix"}, {code = "%%", owner = "Term.ApplyInfix"}]
rewrite.neverInfix.excludeFilters = [
// Default list from http://scalameta.org/scalafmt/#Other
until
to
by
Expand All @@ -30,6 +35,7 @@ rewrite.neverInfix.excludeFilters = [
synchronized
have
when
// custom entries here
is
ignore
not
Expand All @@ -41,4 +47,5 @@ rewrite.neverInfix.excludeFilters = [
called
wasNever
calledAgain
returns
]
36 changes: 24 additions & 12 deletions src/main/scala/io/moia/scalaHttpClient/AwsRequestSigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ class AwsRequestSigner private (credentialsProvider: AwsCredentialsProvider, reg

/** Signs the given HttpRequest
*
* @param request `HttpRequest` to be signed
* @return `Future[HttpRequest]` the signed `HttpRequest`
* @throws AlreadyAuthorizedException if the given `HttpRequest` already includes an "Authorize" header
* @param request
* `HttpRequest` to be signed
* @return
* `Future[HttpRequest]` the signed `HttpRequest`
* @throws AlreadyAuthorizedException
* if the given `HttpRequest` already includes an "Authorize" header
*/
def signRequest(request: HttpRequest): Future[HttpRequest] =
if (isAlreadyAuthorized(request.headers))
Expand All @@ -53,15 +56,19 @@ class AwsRequestSigner private (credentialsProvider: AwsCredentialsProvider, reg

/** Checks if the given collection of `HttpHeader`s includes one "Authorization" header
*
* @param headers `Seq[HttpHeader]` headers of an Akka `HttpRequest`
* @return true if "Authorization" header exists
* @param headers
* `Seq[HttpHeader]` headers of an Akka `HttpRequest`
* @return
* true if "Authorization" header exists
*/
private def isAlreadyAuthorized(headers: Seq[HttpHeader]): Boolean = headers.exists(_.is("authorization"))

/** Constructs an `SdkHttpFullRequest` from Akka's `HttpRequest` for signing.
*
* @param request HttpRequest to convert
* @return SdkHttpFullRequest
* @param request
* HttpRequest to convert
* @return
* SdkHttpFullRequest
*/
private def toSdkRequest(request: HttpRequest): SdkHttpFullRequest = {
val content: InputStream = request.entity.dataBytes.runWith(StreamConverters.asInputStream())
Expand Down Expand Up @@ -89,8 +96,10 @@ class AwsRequestSigner private (credentialsProvider: AwsCredentialsProvider, reg

/** Extracts the headers from the `SdkHttpFullRequest` as collection of Akka's `HttpHeader`s
*
* @param signedSdkRequest `SdkHttpFullRequest` after signing
* @return `Seq[HttpHeader]` of the signedSdkRequest
* @param signedSdkRequest
* `SdkHttpFullRequest` after signing
* @return
* `Seq[HttpHeader]` of the signedSdkRequest
*/
private def getSdkHeaders(signedSdkRequest: SdkHttpFullRequest): scala.collection.immutable.Seq[HttpHeader] =
signedSdkRequest
Expand All @@ -117,9 +126,12 @@ object AwsRequestSigner extends StrictLogging {

/** Construct an `AwsRequestSigner` from the given configuration.
*
* @param config `AwsRequestSignerConfig` to be used to construct one of the three config providers
* @param mat `Materializer` on which the `Future`s run
* @return `AwsRequestSigner`
* @param config
* `AwsRequestSignerConfig` to be used to construct one of the three config providers
* @param mat
* `Materializer` on which the `Future`s run
* @return
* `AwsRequestSigner`
*/
def fromConfig(config: AwsRequestSignerConfig)(implicit mat: Materializer): AwsRequestSigner =
config match {
Expand Down
49 changes: 29 additions & 20 deletions src/main/scala/io/moia/scalaHttpClient/HttpClientConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import scala.concurrent.duration.{FiniteDuration, _}

/** Configuration of a particular HTTP client.
*
* @param scheme For example "http" or "https"
* @param host Name of the host
* @param port Port of the host
* @param awsRequestSignerConfig Optional config for AWS request signing
* @param scheme
* For example "http" or "https"
* @param host
* Name of the host
* @param port
* Port of the host
* @param awsRequestSignerConfig
* Optional config for AWS request signing
*/
final case class HttpClientConfig(
scheme: String,
Expand All @@ -20,22 +24,27 @@ final case class HttpClientConfig(

/** Retry configuration of HTTP clients.
*
* @param retriesRequestTimeout Number of retries for HTTP 408
* @param retriesTooManyRequests Number of retries for HTTP 429 (usually in combination with retry-after header)
* @param retriesClientError Number of retries for all other 4xx codes
* @param retriesInternalServerError Number of retries for HTTP 500
* @param retriesBadGateway Number of retries for HTTP 502
* @param retriesServiceUnavailable Number of retries for HTTP 503
* @param retriesServerError Number of retries for all other 5xx codes
* @param retriesException Number of retries for exceptions in the underlying akka-http client
* @param initialBackoff Time to wait until the first retry. Is multiplied with 2^(# of retry).
* Example:
* 10ms * 2^0 => 10ms
* 10ms * 2^1 => 20ms
* 10ms * 2^2 => 40ms
* 10ms * 2^3 => 80ms
* 10ms * 2^4 => 160ms
* @param strictifyResponseTimeout Time to wait for streaming data to complete
* @param retriesRequestTimeout
* Number of retries for HTTP 408
* @param retriesTooManyRequests
* Number of retries for HTTP 429 (usually in combination with retry-after header)
* @param retriesClientError
* Number of retries for all other 4xx codes
* @param retriesInternalServerError
* Number of retries for HTTP 500
* @param retriesBadGateway
* Number of retries for HTTP 502
* @param retriesServiceUnavailable
* Number of retries for HTTP 503
* @param retriesServerError
* Number of retries for all other 5xx codes
* @param retriesException
* Number of retries for exceptions in the underlying akka-http client
* @param initialBackoff
* Time to wait until the first retry. Is multiplied with 2^(# of retry). Example: 10ms * 2^0 => 10ms 10ms * 2^1 => 20ms 10ms * 2^2 => 40ms 10ms * 2^3 =>
* 80ms 10ms * 2^4 => 160ms
* @param strictifyResponseTimeout
* Time to wait for streaming data to complete
*/
final case class RetryConfig(
retriesRequestTimeout: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import org.scalatest.wordspec.AnyWordSpecLike
import scala.concurrent.duration.{FiniteDuration, _}

class AwsRequestSignerTest extends AnyWordSpecLike with Matchers with FutureValues {
private implicit val system: ActorSystem = ActorSystem("test")
protected implicit val patienceConfig: FiniteDuration = 2.seconds
implicit private val system: ActorSystem = ActorSystem("test")
implicit protected val patienceConfig: FiniteDuration = 2.seconds

val underTest: AwsRequestSigner =
AwsRequestSigner.fromConfig(AwsRequestSignerConfig.BasicCredentials("AKIDEXAMPLE", "secret-key", "eu-central-1"))
Expand Down
7 changes: 3 additions & 4 deletions src/test/scala/io/moia/scalaHttpClient/FutureValues.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import org.scalatest.Assertions
import scala.concurrent.duration.{Duration, DurationLong, FiniteDuration}
import scala.concurrent.{Await, Future, TimeoutException}

/** This is a helper similar to `OptionValues`.
* It awaits a `Future` value within an accepted duration.
* That is much nicer that `ScalaFutures` with their stupid patience config!
/** This is a helper similar to `OptionValues`. It awaits a `Future` value within an accepted duration. That is much nicer that `ScalaFutures` with their stupid
* patience config!
*
* Example:
*
Expand All @@ -18,7 +17,7 @@ import scala.concurrent.{Await, Future, TimeoutException}
* ```
*/
trait FutureValues extends Assertions {
protected implicit val defaultAwaitDuration: FiniteDuration = 1000.millis
implicit protected val defaultAwaitDuration: FiniteDuration = 1000.millis

implicit class WithFutureValue[T](future: Future[T]) {
def futureValue(implicit awaitDuration: Duration): T =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LoggingHttpClientTest extends TestSetup {

case class LoggingContext(context: String)

private implicit val canLogString: CanLog[LoggingContext] = new CanLog[LoggingContext] {
implicit private val canLogString: CanLog[LoggingContext] = new CanLog[LoggingContext] {
val prevContext: String = MDC.get("context")

override def logMessage(originalMsg: String, ctx: LoggingContext): String = {
Expand All @@ -32,7 +32,7 @@ class LoggingHttpClientTest extends TestSetup {
}

private val theLogger: LoggerTakingImplicit[LoggingContext] = Logger.takingImplicit(LoggerFactory.getLogger(getClass.getName))
private implicit val ctx: LoggingContext = LoggingContext("Logging Context")
implicit private val ctx: LoggingContext = LoggingContext("Logging Context")

classOf[LoggingHttpClient[LoggingContext]].getSimpleName should {
"take a customer logger" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import scala.collection.immutable
import scala.concurrent.duration._

class StatusCodesTest extends TestSetup with MockServer {
override implicit val defaultAwaitDuration: FiniteDuration = 1.second
implicit override val defaultAwaitDuration: FiniteDuration = 1.second

"respect retry configuration" when {
s"the server responds with ${StatusCodes.TooManyRequests}" in {
Expand Down