|
1 | 1 | package com.advancedtelematic.libats.http |
2 | 2 |
|
3 | 3 | import java.util.UUID |
4 | | - |
5 | 4 | import akka.actor.ActorSystem |
6 | 5 | import akka.http.scaladsl.Http |
7 | | -import akka.http.scaladsl.model._ |
| 6 | +import akka.http.scaladsl.model.* |
8 | 7 | import akka.http.scaladsl.model.headers.RawHeader |
9 | 8 | import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller} |
10 | 9 | import akka.http.scaladsl.util.FastFuture |
11 | 10 | import akka.stream.Materializer |
12 | | -import cats.syntax.either._ |
13 | | -import cats.syntax.option._ |
| 11 | +import akka.util.ByteString |
| 12 | +import cats.syntax.either.* |
| 13 | +import cats.syntax.option.* |
14 | 14 | import com.advancedtelematic.libats.data.DataType.Namespace |
15 | 15 | import com.advancedtelematic.libats.data.ErrorRepresentation |
16 | 16 | import com.advancedtelematic.libats.http.Errors.RemoteServiceError |
17 | 17 | import com.advancedtelematic.libats.http.ServiceHttpClient.{ServiceHttpFullResponse, ServiceHttpFullResponseEither} |
18 | | -import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._ |
| 18 | +import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport.* |
19 | 19 | import io.circe.{Encoder, Json} |
20 | 20 | import org.slf4j.LoggerFactory |
21 | 21 |
|
@@ -71,14 +71,24 @@ abstract class ServiceHttpClient(_httpClient: HttpRequest => Future[HttpResponse |
71 | 71 | execHttpUnmarshalled(req) |
72 | 72 | } |
73 | 73 |
|
| 74 | + protected def execJsonHttpWithNamespace[Res : ClassTag : FromEntityUnmarshaller, Req : Encoder] |
| 75 | + (ns: Namespace, request: HttpRequest, entity: Req): Future[Either[RemoteServiceError, Res]] = { |
| 76 | + val req = request.addHeader(RawHeader("x-ats-namespace", ns.get)) |
| 77 | + execJsonHttp(req, entity) |
| 78 | + } |
| 79 | + |
| 80 | + // The entity is replaced into `response` so that it can be read again by api users, for example through `handleErrors` |
74 | 81 | private def tryErrorParsing(response: HttpResponse)(implicit um: FromEntityUnmarshaller[ErrorRepresentation]): Future[RemoteServiceError] = { |
75 | 82 | um(response.entity).map { rawError => |
76 | | - RemoteServiceError(s"${rawError.description}", response.status, rawError.cause.getOrElse(Json.Null), |
| 83 | + val entity = HttpEntity.Strict(response.entity.contentType, ByteString(rawError.asJson.noSpaces)) |
| 84 | + RemoteServiceError(s"${rawError.description}", response.withEntity(entity), rawError.cause.getOrElse(Json.Null), |
77 | 85 | rawError.code, rawError.some, rawError.errorId.getOrElse(UUID.randomUUID())) |
78 | 86 | }.recoverWith { case _ => |
79 | | - Unmarshaller.stringUnmarshaller(response.entity).map(msg => RemoteServiceError(msg, response.status)) |
| 87 | + Unmarshaller.stringUnmarshaller(response.entity).map { str => |
| 88 | + RemoteServiceError(str, response.withEntity(HttpEntity.Strict(response.entity.contentType, ByteString(str)))) |
| 89 | + } |
80 | 90 | }.recover { case _ => |
81 | | - RemoteServiceError(s"Unknown error: $response", response.status) |
| 91 | + RemoteServiceError(s"Unknown error: $response", response) |
82 | 92 | } |
83 | 93 | } |
84 | 94 |
|
|
0 commit comments