Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "clean: Replace scalaj with sttp" #497

Merged
merged 1 commit into from
Mar 25, 2024
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
34 changes: 16 additions & 18 deletions api-scala/src/main/scala/com/codacy/api/client/CodacyClient.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.codacy.api.client

import com.codacy.api.util.JsonOps
import play.api.libs.json._
import sttp.client3.{HttpURLConnectionBackend, SttpBackendOptions}
import sttp.client3.quick._
import com.codacy.api.util.JsonOps
import scalaj.http.Http

import java.net.URL
import scala.concurrent.duration._
import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
import scala.util.control.NonFatal

class CodacyClient(
apiUrl: Option[String] = None,
Expand Down Expand Up @@ -40,24 +38,24 @@ class CodacyClient(
sleepTime: Option[Int],
numRetries: Option[Int]
)(implicit reads: Reads[T]): RequestResponse[T] = {
val url = s"$remoteUrl/${request.endpoint}"
try {
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 headers = tokens ++ Map("Content-Type" -> "application/json")

timeoutOpt.foreach { timeout =>
options = options.connectionTimeout(timeout.connTimeoutMs.millis)
req = req.readTimeout(timeout.readTimeoutMs.millis)
val httpRequest = timeoutOpt match {
case Some(timeout) =>
Http(url).timeout(connTimeoutMs = timeout.connTimeoutMs, readTimeoutMs = timeout.readTimeoutMs)
case None => Http(url)
}

val client = simpleHttpClient.withBackend(HttpURLConnectionBackend(options = options))

val response = client.send(req)
val body = httpRequest
.params(request.queryParameters)
.headers(headers)
.postData(value)
.asString
.body

parseJsonAs[T](response.body) match {
parseJsonAs[T](body) match {
case failure: FailedResponse =>
retryPost(request, value, timeoutOpt, sleepTime, numRetries.map(x => x - 1), failure.message)
case success => success
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ lazy val apiScala = project
.settings(
libraryDependencies ++= Seq(
"com.typesafe.play" %% "play-json" % "2.8.2",
"com.softwaremill.sttp.client3" %% "core" % "3.9.5",
"org.scalaj" %% "scalaj-http" % "2.4.2",
"org.eclipse.jgit" % "org.eclipse.jgit" % "4.11.9.201909030838-r",
"org.scalatest" %% "scalatest" % "3.0.8" % Test
)
Expand Down