From 364dcd61aca2eefdcf85afaa9e35f42402a21331 Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Mon, 25 Mar 2024 18:00:14 +0100 Subject: [PATCH] Revert "Revert "clean: Replace scalaj with sttp (#496)" (#497)" This reverts commit 399cb3fd43643948fa9f8838ba004bbdfa18039f. --- .../com/codacy/api/client/CodacyClient.scala | 34 ++++++++++--------- build.sbt | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/api-scala/src/main/scala/com/codacy/api/client/CodacyClient.scala b/api-scala/src/main/scala/com/codacy/api/client/CodacyClient.scala index f971657b..bb89336a 100644 --- a/api-scala/src/main/scala/com/codacy/api/client/CodacyClient.scala +++ b/api-scala/src/main/scala/com/codacy/api/client/CodacyClient.scala @@ -1,12 +1,14 @@ package com.codacy.api.client -import play.api.libs.json._ import com.codacy.api.util.JsonOps -import scalaj.http.Http +import play.api.libs.json._ +import sttp.client3.{HttpURLConnectionBackend, SttpBackendOptions} +import sttp.client3.quick._ import java.net.URL -import scala.util.{Failure, Success, Try} +import scala.concurrent.duration._ import scala.util.control.NonFatal +import scala.util.{Failure, Success, Try} class CodacyClient( apiUrl: Option[String] = None, @@ -38,24 +40,24 @@ class CodacyClient( sleepTime: Option[Int], numRetries: Option[Int] )(implicit reads: Reads[T]): RequestResponse[T] = { - val url = s"$remoteUrl/${request.endpoint}" try { - val headers = tokens ++ Map("Content-Type" -> "application/json") + var req = quickRequest + .post(uri"$remoteUrl/${request.endpoint}".withParams(request.queryParameters)) + .headers(tokens ++ Map("Content-Type" -> "application/json")) + .body(value) + + var options = SttpBackendOptions.Default - val httpRequest = timeoutOpt match { - case Some(timeout) => - Http(url).timeout(connTimeoutMs = timeout.connTimeoutMs, readTimeoutMs = timeout.readTimeoutMs) - case None => Http(url) + timeoutOpt.foreach { timeout => + options = options.connectionTimeout(timeout.connTimeoutMs.millis) + req = req.readTimeout(timeout.readTimeoutMs.millis) } - val body = httpRequest - .params(request.queryParameters) - .headers(headers) - .postData(value) - .asString - .body + val client = simpleHttpClient.withBackend(HttpURLConnectionBackend(options = options)) + + val response = client.send(req) - parseJsonAs[T](body) match { + parseJsonAs[T](response.body) match { case failure: FailedResponse => retryPost(request, value, timeoutOpt, sleepTime, numRetries.map(x => x - 1), failure.message) case success => success diff --git a/build.sbt b/build.sbt index f2286194..dd3756df 100644 --- a/build.sbt +++ b/build.sbt @@ -77,7 +77,7 @@ lazy val apiScala = project .settings( libraryDependencies ++= Seq( "com.typesafe.play" %% "play-json" % "2.8.2", - "org.scalaj" %% "scalaj-http" % "2.4.2", + "com.softwaremill.sttp.client3" %% "core" % "3.9.5", "org.eclipse.jgit" % "org.eclipse.jgit" % "4.11.9.201909030838-r", "org.scalatest" %% "scalatest" % "3.0.8" % Test )