-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#245 Add the ability to query REST endpoints from Reader module
* created Provider to query the data from server * support for Future, IO, and ZIO based providers * work in progress
- Loading branch information
Showing
11 changed files
with
275 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
reader/src/main/scala/za/co/absa/atum/reader/basic/Reader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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.reader.basic | ||
|
||
import za.co.absa.atum.reader.provider.Provider | ||
|
||
abstract class Reader[F[_]](implicit val provider: Provider[F[_]]) |
19 changes: 19 additions & 0 deletions
19
reader/src/main/scala/za/co/absa/atum/reader/exceptions/ReaderException.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* 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.reader.exceptions | ||
|
||
abstract class ReaderException(message: String) extends Exception(message) |
26 changes: 26 additions & 0 deletions
26
reader/src/main/scala/za/co/absa/atum/reader/exceptions/RequestException.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.reader.exceptions | ||
|
||
import sttp.model.{RequestMetadata, StatusCode} | ||
|
||
case class RequestException ( | ||
message: String, | ||
responseBody: String, | ||
statusCode: StatusCode, | ||
request: RequestMetadata) | ||
extends ReaderException(message) |
61 changes: 61 additions & 0 deletions
61
reader/src/main/scala/za/co/absa/atum/reader/provider/AbstractHttpProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.reader.provider | ||
|
||
import _root_.io.circe.parser.decode | ||
import _root_.io.circe.Decoder | ||
import com.typesafe.config.Config | ||
import sttp.client3.{Response, SttpBackend, UriContext, basicRequest} | ||
import za.co.absa.atum.reader.exceptions.RequestException | ||
|
||
import scala.util.{Failure, Try} | ||
|
||
/** | ||
* A HttpProvider is a component that is responsible for providing teh data to readers using REST API | ||
* @tparam F | ||
*/ | ||
abstract class AbstractHttpProvider[F[_]](val serverUrl: String) extends Provider[F] { | ||
type RequestFunction = SttpBackend[F, Any] => F[Response[Either[String, String]]] | ||
type ResponseMapperFunction[R] = Response[Either[String, String]] => Try[R] | ||
|
||
protected def executeRequest(requestFnc: RequestFunction): F[Response[Either[String, String]]] | ||
protected def mapResponse[R](response: F[Response[Either[String, String]]], mapperFnc: ResponseMapperFunction[R]): F[Try[R]] | ||
|
||
protected def query[R: Decoder](endpointUri: String): F[Try[R]] = { | ||
val endpointToQuery = serverUrl + endpointUri | ||
val request = basicRequest | ||
.get(uri"$endpointToQuery") | ||
val response = executeRequest(request.send(_)) | ||
mapResponse(response, responseMapperFunction[R]) | ||
} | ||
|
||
private def responseMapperFunction[R: Decoder](response: Response[Either[String, String]]): Try[R] = { | ||
response.body match { | ||
case Left(error) => Failure(RequestException(response.statusText, error, response.code, response.request)) | ||
case Right(body) => decode[R](body).toTry | ||
} | ||
} | ||
|
||
} | ||
|
||
object AbstractHttpProvider { | ||
final val UrlKey = "atum.server.url" | ||
|
||
def atumServerUrl(config: Config): String = { | ||
config.getString(UrlKey) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
reader/src/main/scala/za/co/absa/atum/reader/provider/Provider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* 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.reader.provider | ||
|
||
/** | ||
* A basic class for defining methods that will be providing data to readers. | ||
*/ | ||
abstract class Provider[F[_]] { | ||
// here will come abstract methods that are to return data to readers | ||
} |
46 changes: 46 additions & 0 deletions
46
reader/src/main/scala/za/co/absa/atum/reader/provider/future/HttpProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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.reader.provider.future | ||
|
||
import cats.implicits.catsStdInstancesForFuture | ||
import com.typesafe.config.{Config, ConfigFactory} | ||
import sttp.client3.Response | ||
import sttp.client3.asynchttpclient.future.AsyncHttpClientFutureBackend | ||
import za.co.absa.atum.reader.provider.AbstractHttpProvider | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.util.Try | ||
|
||
|
||
class HttpProvider(serverUrl: String)(implicit executor: ExecutionContext) extends AbstractHttpProvider[Future](serverUrl) { | ||
|
||
def this(config: Config = ConfigFactory.load())(implicit executor: ExecutionContext) = { | ||
this(AbstractHttpProvider.atumServerUrl(config ))(executor) | ||
} | ||
|
||
private val asyncHttpClientFutureBackend = AsyncHttpClientFutureBackend() | ||
|
||
override protected def executeRequest(requestFnc: RequestFunction): Future[Response[Either[String, String]]] = { | ||
requestFnc(asyncHttpClientFutureBackend) | ||
} | ||
|
||
override protected def mapResponse[R]( | ||
response: Future[Response[Either[String, String]]], | ||
mapperFnc: ResponseMapperFunction[R]): Future[Try[R]] = { | ||
response.map(mapperFnc) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
reader/src/main/scala/za/co/absa/atum/reader/provider/io/HttpProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package za.co.absa.atum.reader.provider.io | ||
|
||
import cats.effect.IO | ||
import com.typesafe.config.{Config, ConfigFactory} | ||
import sttp.client3.Response | ||
import sttp.client3.armeria.cats.ArmeriaCatsBackend | ||
import za.co.absa.atum.reader.provider.AbstractHttpProvider | ||
|
||
import scala.util.Try | ||
|
||
class HttpProvider(serverUrl: String) extends AbstractHttpProvider[IO](serverUrl) { | ||
|
||
def this(config: Config = ConfigFactory.load()) = { | ||
this(AbstractHttpProvider.atumServerUrl(config )) | ||
} | ||
|
||
override protected def executeRequest(requestFnc: RequestFunction): IO[Response[Either[String, String]]] = { | ||
ArmeriaCatsBackend | ||
.resource[IO]() | ||
.use(requestFnc) | ||
} | ||
|
||
override protected def mapResponse[R]( | ||
response: IO[Response[Either[String, String]]], | ||
mapperFnc: ResponseMapperFunction[R]): IO[Try[R]] = { | ||
response.map(mapperFnc) | ||
} | ||
} | ||
|
||
object HttpProvider { | ||
lazy implicit val httpProvider: HttpProvider = new HttpProvider() | ||
} |
24 changes: 24 additions & 0 deletions
24
reader/src/main/scala/za/co/absa/atum/reader/provider/zio/HttpProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package za.co.absa.atum.reader.provider.zio | ||
|
||
import com.typesafe.config.{Config, ConfigFactory} | ||
import sttp.client3.Response | ||
import sttp.client3.armeria.zio.ArmeriaZioBackend | ||
import za.co.absa.atum.reader.provider.AbstractHttpProvider | ||
import zio.ZIO | ||
|
||
import scala.util.Try | ||
|
||
class HttpProvider(serverUrl: String) extends AbstractHttpProvider[ZIO](serverUrl) { | ||
|
||
def this(config: Config = ConfigFactory.load()) = { | ||
this(AbstractHttpProvider.atumServerUrl(config )) | ||
} | ||
|
||
|
||
|
||
override protected def executeRequest(requestFnc: RequestFunction): ZIO[Response[Either[String, String]]] = { | ||
ArmeriaZioBackend.usingDefaultClient().map(requestFnc) | ||
} | ||
|
||
override protected def mapResponse[R](response: ZIO[Response[Either[String, String]]], mapperFnc: ResponseMapperFunction[R]): ZIO[Try[R]] = ??? | ||
} //TODO |