|
1 | 1 | package com.codacy.api.client
|
2 | 2 |
|
3 |
| -import com.codacy.api.util.JsonOps |
4 | 3 | import play.api.libs.json._
|
5 |
| -import sttp.client3.{HttpURLConnectionBackend, SttpBackendOptions} |
6 |
| -import sttp.client3.quick._ |
| 4 | +import com.codacy.api.util.JsonOps |
| 5 | +import scalaj.http.Http |
7 | 6 |
|
8 | 7 | import java.net.URL
|
9 |
| -import scala.concurrent.duration._ |
10 |
| -import scala.util.control.NonFatal |
11 | 8 | import scala.util.{Failure, Success, Try}
|
| 9 | +import scala.util.control.NonFatal |
12 | 10 |
|
13 | 11 | class CodacyClient(
|
14 | 12 | apiUrl: Option[String] = None,
|
@@ -40,24 +38,24 @@ class CodacyClient(
|
40 | 38 | sleepTime: Option[Int],
|
41 | 39 | numRetries: Option[Int]
|
42 | 40 | )(implicit reads: Reads[T]): RequestResponse[T] = {
|
| 41 | + val url = s"$remoteUrl/${request.endpoint}" |
43 | 42 | try {
|
44 |
| - var req = quickRequest |
45 |
| - .post(uri"$remoteUrl/${request.endpoint}".withParams(request.queryParameters)) |
46 |
| - .headers(tokens ++ Map("Content-Type" -> "application/json")) |
47 |
| - .body(value) |
48 |
| - |
49 |
| - var options = SttpBackendOptions.Default |
| 43 | + val headers = tokens ++ Map("Content-Type" -> "application/json") |
50 | 44 |
|
51 |
| - timeoutOpt.foreach { timeout => |
52 |
| - options = options.connectionTimeout(timeout.connTimeoutMs.millis) |
53 |
| - req = req.readTimeout(timeout.readTimeoutMs.millis) |
| 45 | + val httpRequest = timeoutOpt match { |
| 46 | + case Some(timeout) => |
| 47 | + Http(url).timeout(connTimeoutMs = timeout.connTimeoutMs, readTimeoutMs = timeout.readTimeoutMs) |
| 48 | + case None => Http(url) |
54 | 49 | }
|
55 | 50 |
|
56 |
| - val client = simpleHttpClient.withBackend(HttpURLConnectionBackend(options = options)) |
57 |
| - |
58 |
| - val response = client.send(req) |
| 51 | + val body = httpRequest |
| 52 | + .params(request.queryParameters) |
| 53 | + .headers(headers) |
| 54 | + .postData(value) |
| 55 | + .asString |
| 56 | + .body |
59 | 57 |
|
60 |
| - parseJsonAs[T](response.body) match { |
| 58 | + parseJsonAs[T](body) match { |
61 | 59 | case failure: FailedResponse =>
|
62 | 60 | retryPost(request, value, timeoutOpt, sleepTime, numRetries.map(x => x - 1), failure.message)
|
63 | 61 | case success => success
|
|
0 commit comments