Skip to content
Snippets Groups Projects
Commit cac3e219 authored by Felipe D'Abrantes's avatar Felipe D'Abrantes
Browse files

Add an object to run HTTP calls

parent 0a583c68
No related branches found
No related tags found
1 merge request!14Add endpoints to manage Dailies
package models
import akka.actor.ActorSystem
import akka.stream.{Materializer, SystemMaterializer}
import play.api.libs.ws.ahc.{StandaloneAhcWSClient}
import play.api.libs.json.JsValue
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
object HttpCall {
/**
* Fetches the response of a GET request to the given URL.
*
* @param url The URL of the request.
*
* @return a Future containing the response body as a JSON.
*/
def get(url: String): Future[JsValue] = {
// Create ActorSystem for thread and streaming management
implicit val system: ActorSystem = ActorSystem()
// Materializer ensures streams are executed correctly and resources are managed efficiently
implicit val materializer: Materializer = SystemMaterializer(system).materializer
// Create the standalone WS client
val wsClient: StandaloneAhcWSClient = StandaloneAhcWSClient()
// Call API and fetch response
val response: Future[JsValue] = wsClient.url(url).get().map { response =>
val statusText: String = response.statusText
println(s"Got a response: $statusText")
response.body[JsValue]
}
// Close WSClient and terminate ActorSystem
response
.andThen { case _ => wsClient.close() }
.andThen { case _ => system.terminate() }
}
}
...@@ -18,3 +18,5 @@ libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0 ...@@ -18,3 +18,5 @@ libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0
// Adds additional packages into conf/routes // Adds additional packages into conf/routes
// play.sbt.routes.RoutesKeys.routesImport += "com.daily.binders._" // play.sbt.routes.RoutesKeys.routesImport += "com.daily.binders._"
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "4.3.0" libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "4.3.0"
libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.8.10"
libraryDependencies += "com.typesafe.play" %% "play-ahc-ws-standalone" % "2.1.10"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment