Skip to content

Commit

Permalink
* introduced MonadError into the GenericServerConnection
Browse files Browse the repository at this point in the history
* fixed license headers
  • Loading branch information
benedeki committed Nov 1, 2024
1 parent e6dcb52 commit 6968b02
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test_filenames_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
excludes: |
server/src/test/scala/za/co/absa/atum/server/api/TestData.scala,
server/src/test/scala/za/co/absa/atum/server/api/TestTransactorProvider.scala,
server/src/test/scala/za/co/absa/atum/server/ConfigProviderTest.scala
server/src/test/scala/za/co/absa/atum/server/ConfigProviderTest.scala,
model/src/test/scala/za/co/absa/atum/model/testing/*
verbose-logging: 'false'
fail-on-violation: 'true'
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package za.co.absa.atum.model.testing.implicits

object StringImplicits {
implicit class StringLinearization(val str: String) extends AnyVal {
def linearize: String = {
str.stripMargin.replace("\r", "").replace("\n", "")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import za.co.absa.atum.model.ResultValueType
import za.co.absa.atum.model.dto.MeasureResultDTO.TypedValue
import za.co.absa.atum.model.dto._
import za.co.absa.atum.model.utils.JsonSyntaxExtensions._
import za.co.absa.atum.model.utils.SerializationUtilsTest.StringLinearization
import za.co.absa.atum.model.testing.implicits.StringImplicits.StringLinearization

import java.time.{ZoneId, ZoneOffset, ZonedDateTime}
import java.util.UUID
Expand Down Expand Up @@ -436,11 +436,3 @@ class SerializationUtilsUnitTests extends AnyFlatSpecLike {
}

}

object SerializationUtilsTest {
implicit class StringLinearization(val str: String) extends AnyVal {
def linearize: String = {
str.stripMargin.replace("\r", "").replace("\n", "")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,24 +19,26 @@ package za.co.absa.atum.reader.server
import _root_.io.circe.Decoder
import _root_.io.circe.{Error => circeError}
import com.typesafe.config.Config
import sttp.client3.{Identity, RequestT, ResponseException, basicRequest}
import sttp.client3.{Identity, RequestT, Response, ResponseException, basicRequest}
import sttp.model.Uri
import sttp.client3.circe._

import sttp.monad.MonadError
import sttp.monad.syntax._
import za.co.absa.atum.model.envelopes.ErrorResponse
import za.co.absa.atum.reader.server.GenericServerConnection.RequestResult

abstract class GenericServerConnection[F[_]](val serverUrl: String) {
abstract class GenericServerConnection[F[_]: MonadError](val serverUrl: String) {

protected def executeRequest[R](request: RequestT[Identity, RequestResult[R], Any]): F[RequestResult[R]]
protected def executeRequest[R](request: RequestT[Identity, RequestResult[R], Any]): F[Response[RequestResult[R]]]

def getQuery[R: Decoder](endpointUri: String, params: Map[String, String] = Map.empty): F[RequestResult[R]] = {
val endpointToQuery = serverUrl + endpointUri
val uri = Uri.unsafeParse(endpointToQuery).addParams(params)
val request: RequestT[Identity, RequestResult[R], Any] = basicRequest
.get(uri)
.response(asJsonEither[ErrorResponse, R])
executeRequest(request)
val response = executeRequest(request)
response.map(_.body)
}

def close(): F[Unit]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,20 +16,20 @@

package za.co.absa.atum.reader.server.future

import scala.concurrent.{ExecutionContext, Future}
import sttp.client3.{Identity, RequestT, SttpBackend}

import scala.concurrent.{ExecutionContext, Future}
import sttp.client3.{Identity, RequestT, Response, SttpBackend}
import sttp.monad.FutureMonad
import za.co.absa.atum.reader.server.GenericServerConnection
import za.co.absa.atum.reader.server.GenericServerConnection.RequestResult


abstract class FutureServerConnection(serverUrl: String, closeable: Boolean)(implicit executor: ExecutionContext)
extends GenericServerConnection[Future](serverUrl) {
extends GenericServerConnection[Future](serverUrl)(new FutureMonad) {

protected val backend: SttpBackend[Future, Any]

override protected def executeRequest[R](request: RequestT[Identity, RequestResult[R], Any]): Future[RequestResult[R]] = {
request.send(backend).map(_.body)
override protected def executeRequest[R](request: RequestT[Identity, RequestResult[R], Any]): Future[Response[RequestResult[R]]] = {
request.send(backend)
}

override def close(): Future[Unit] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,15 +18,15 @@ package za.co.absa.atum.reader.server.io

import cats.effect.IO
import com.typesafe.config.{Config, ConfigFactory}
import sttp.client3.{Identity, RequestT, SttpBackend}
import sttp.client3.{Identity, RequestT, Response, SttpBackend}
import sttp.client3.armeria.cats.ArmeriaCatsBackend

import sttp.client3.impl.cats.CatsMonadAsyncError
import za.co.absa.atum.reader.server.GenericServerConnection
import za.co.absa.atum.reader.server.GenericServerConnection.RequestResult


class ArmeriaServerConnection protected(serverUrl: String, backend: SttpBackend[IO, Any], closeable: Boolean)
extends GenericServerConnection[IO](serverUrl) {
extends GenericServerConnection[IO](serverUrl)(new CatsMonadAsyncError[IO]) {

def this(mserverUrl: String) = {
this(mserverUrl, ArmeriaCatsBackend[IO](), closeable = true)
Expand All @@ -36,8 +36,8 @@ class ArmeriaServerConnection protected(serverUrl: String, backend: SttpBackend[
this(GenericServerConnection.atumServerUrl(config ), ArmeriaCatsBackend[IO](), closeable = true)
}

override protected def executeRequest[R](request: RequestT[Identity, RequestResult[R], Any]): IO[RequestResult[R]] = {
request.send(backend).map(_.body)
override protected def executeRequest[R](request: RequestT[Identity, RequestResult[R], Any]): IO[Response[RequestResult[R]]] = {
request.send(backend)
}

override def close(): IO[Unit] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,10 +17,9 @@
package za.co.absa.atum.reader.server.zio

import com.typesafe.config.{Config, ConfigFactory}
import sttp.client3.{Identity, RequestT}
import sttp.client3.{Identity, RequestT, Response}
import sttp.client3.armeria.zio.ArmeriaZioBackend
import zio.Task

import za.co.absa.atum.reader.server.GenericServerConnection
import za.co.absa.atum.reader.server.GenericServerConnection.RequestResult

Expand All @@ -31,10 +30,9 @@ class ArmeriaServerConnection(serverUrl: String) extends ZioServerConnection(ser
this(GenericServerConnection.atumServerUrl(config ))
}

override protected def executeRequest[R](request: RequestT[Identity, RequestResult[R], Any]): Task[RequestResult[R]] = {
override protected def executeRequest[R](request: RequestT[Identity, RequestResult[R], Any]): Task[Response[RequestResult[R]]] = {
ArmeriaZioBackend.usingDefaultClient().flatMap { backend =>
val response = backend.send(request)
response.map(_.body)
backend.send(request)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
package za.co.absa.atum.reader.server.zio

import com.typesafe.config.{Config, ConfigFactory}
import sttp.client3.{Identity, RequestT}
import sttp.client3.{Identity, RequestT, Response}
import sttp.client3.httpclient.zio.HttpClientZioBackend
import za.co.absa.atum.reader.server.GenericServerConnection
import za.co.absa.atum.reader.server.GenericServerConnection.RequestResult
Expand All @@ -30,10 +30,9 @@ class HttpClientServerConnection(serverUrl: String) extends ZioServerConnection(
this(GenericServerConnection.atumServerUrl(config ))
}

override protected def executeRequest[R](request: RequestT[Identity, RequestResult[R], Any]): Task[RequestResult[R]] = {
override protected def executeRequest[R](request: RequestT[Identity, RequestResult[R], Any]): Task[Response[RequestResult[R]]] = {
HttpClientZioBackend().flatMap { backend =>
val response = backend.send(request)
response.map(_.body)
backend.send(request)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 ABSA Group Limited
* Copyright 2021 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,10 +17,12 @@
package za.co.absa.atum.reader.server.zio

import zio.{Exit, Task}

import sttp.client3
import sttp.client3.impl.zio.RIOMonadAsyncError
import za.co.absa.atum.reader.server.GenericServerConnection

abstract class ZioServerConnection(serverUrl: String) extends GenericServerConnection[Task](serverUrl) {

abstract class ZioServerConnection(serverUrl: String) extends GenericServerConnection[Task](serverUrl)(new RIOMonadAsyncError[Any]) {

override def close(): Task[Unit] = {
Exit.succeed(())
Expand Down

0 comments on commit 6968b02

Please sign in to comment.