play.api.libs.ws.WSRequest Scala Examples

The following examples show how to use play.api.libs.ws.WSRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: AuthISpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.endpoints

import com.github.tomakehurst.wiremock.stubbing.StubMapping
import play.api.http.HeaderNames.ACCEPT
import play.api.http.Status._
import play.api.libs.ws.{WSRequest, WSResponse}
import support.IntegrationBaseSpec
import v1.stubs.{AuditStub, AuthStub, DesStub}

class AuthISpec extends IntegrationBaseSpec {

  private trait Test {
    val vrn = "123456789"
    val periodKey = "AB19"
    val correlationId = "X-123"

    def setupStubs(): StubMapping

    def request(): WSRequest = {
      setupStubs()
      buildRequest(s"/$vrn/returns/$periodKey")
        .withHttpHeaders((ACCEPT, "application/vnd.hmrc.1.0+json"))
    }
  }

  "Calling the View VAT Return endpoint" when {
    "the user is authorised" should {
      "return 200" in new Test {

        override def setupStubs(): StubMapping = {
          AuditStub.audit()
          AuthStub.authorised()
          DesStub.serviceSuccess(vrn, periodKey)
        }

        val response: WSResponse = await(request().get())
        response.status shouldBe OK
      }
    }

    "the user is belongs to an unsupported affinity group" should {
      "return 403" in new Test {

        override def setupStubs(): StubMapping = {
          AuditStub.audit()
          AuthStub.unauthorisedUnsupportedAffinity()
          DesStub.serviceSuccess(vrn, periodKey)
        }

        val response: WSResponse = await(request().get())
        response.status shouldBe FORBIDDEN
      }
    }

    "the user is not logged in" should {
      "return 500" in new Test {

        override def setupStubs(): StubMapping = {
          AuditStub.audit()
          AuthStub.unauthorisedNotLoggedIn()
        }

        val response: WSResponse = await(request().get())
        response.status shouldBe INTERNAL_SERVER_ERROR
      }
    }

    "the user is NOT authorised" should {
      "return 500" in new Test {

        override def setupStubs(): StubMapping = {
          AuditStub.audit()
          AuthStub.unauthorisedOther()
        }

        val response: WSResponse = await(request().get())
        response.status shouldBe INTERNAL_SERVER_ERROR
      }
    }
  }
} 
Example 2
Source File: IntegrationBaseSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package support

import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{JsValue, Json}
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
import play.api.{Application, Environment, Mode}

trait IntegrationBaseSpec extends UnitSpec with WireMockHelper with GuiceOneServerPerSuite
  with BeforeAndAfterEach with BeforeAndAfterAll {

  val mockHost: String = WireMockHelper.host
  val mockPort: String = WireMockHelper.wireMockPort.toString

  lazy val client: WSClient = app.injector.instanceOf[WSClient]

  def servicesConfig: Map[String, Any] = Map(
    "microservice.services.des.host" -> mockHost,
    "microservice.services.des.port" -> mockPort,
    "microservice.services.auth.host" -> mockHost,
    "microservice.services.auth.port" -> mockPort,
    "auditing.consumer.baseUri.port" -> mockPort,
    "microservice.services.non-repudiation.host" -> mockHost,
    "microservice.services.non-repudiation.port" -> mockPort,
    "feature-switch.refactor.enabled" -> true,
    "feature-switch.refactor.prod.enabled" -> false,
    "microservice.services.non-repudiation.maxTimeout" -> 5000
  )

  override implicit lazy val app: Application = new GuiceApplicationBuilder()
    .in(Environment.simple(mode = Mode.Dev))
    .configure(servicesConfig)
    .build()

  override def beforeAll(): Unit = {
    super.beforeAll()
    startWireMock()
  }

  override def afterAll(): Unit = {
    stopWireMock()
    super.afterAll()
  }

  def buildRequest(path: String): WSRequest = client.url(s"http://localhost:$port$path").withFollowRedirects(false)

  def document(response: WSResponse): JsValue = Json.parse(response.body)
} 
Example 3
Source File: BaseFunctionalSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.support

import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.{WSClient, WSRequest}
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.vatapi.{TestApplication, VrnGenerator}

import scala.collection.mutable

trait BaseFunctionalSpec extends TestApplication with WireMockHelper with GuiceOneServerPerSuite{
  protected val vrn: Vrn = VrnGenerator().nextVrn()

  implicit val urlPathVariables: mutable.Map[String, String] = mutable.Map()


  lazy val client: WSClient = app.injector.instanceOf[WSClient]

  override lazy val app: Application = new GuiceApplicationBuilder().configure(Map(
    "auditing.consumer.baseUri.host" -> "localhost",
    "auditing.consumer.baseUri.port" -> 22222,
    "microservice.services.des.host" -> mockHost,
    "microservice.services.des.port" -> mockPort,
    "microservice.services.auth.host" -> mockHost,
    "microservice.services.auth.port" -> mockPort,
    "auditing.consumer.baseUri.port" -> mockPort,
    "access-keys.xApiKey" -> "dummy-api-key",
    "microservice.services.non-repudiation.host" -> mockHost,
    "microservice.services.non-repudiation.port" -> mockPort,
    "feature-switch.refactor.enabled" -> false,
    "feature-switch.refactor.prod.enabled" -> false
  )).build()

  def when() = new HttpVerbs()(urlPathVariables, timeout)
  def given() = new Givens(when())

  def buildRequest(path: String): WSRequest = client.url(s"http://localhost:$port$path").withFollowRedirects(false)
} 
Example 4
Source File: Http.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.support

import play.api.libs.json.{JsValue, Json, Writes}
import play.api.libs.ws.{EmptyBody, WSRequest, WSResponse}
import uk.gov.hmrc.http.{HeaderCarrier, HttpResponse}
import uk.gov.hmrc.play.http.ws.WSHttpResponse

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}

object Http extends BaseFunctionalSpec {

  def get(url: String)(implicit hc: HeaderCarrier, timeout: FiniteDuration): HttpResponse = perform(url) { request =>
    request.get()
  }

  def post[A](url: String, body: A, headers: Seq[(String, String)] = Seq.empty)(
      implicit writes: Writes[A],
      hc: HeaderCarrier,
      timeout: FiniteDuration): HttpResponse = perform(url) { request =>
    request.post(Json.toJson(body))
  }

  def postString(url: String, body: String, headers: Seq[(String, String)] = Seq.empty)(
    implicit hc: HeaderCarrier,
    timeout: FiniteDuration): HttpResponse = perform(url) { request =>
    request.withHttpHeaders("Content-Type" -> "application/json").post[String](body)
  }

  def postJson(url: String, body: JsValue, headers: Seq[(String, String)] = Seq.empty)(
      implicit hc: HeaderCarrier,
      timeout: FiniteDuration): HttpResponse = perform(url) { request =>
    request.post(body)
  }

  def putJson(url: String, body: JsValue, headers: Seq[(String, String)] = Seq.empty)(
      implicit hc: HeaderCarrier,
      timeout: FiniteDuration): HttpResponse = perform(url) { request =>
    request.put(body)
  }

  def postEmpty(url: String)(implicit hc: HeaderCarrier, timeout: FiniteDuration): HttpResponse = perform(url) {
    request =>
      request.post(EmptyBody)
  }

  def delete(url: String)(implicit hc: HeaderCarrier, timeout: FiniteDuration): HttpResponse = perform(url) {
    request =>
      request.delete()
  }

  def perform(url: String)(fun: WSRequest => Future[WSResponse])(implicit hc: HeaderCarrier,
                                                                         timeout: FiniteDuration): WSHttpResponse =
    await(fun(client.url(url).addHttpHeaders(hc.headers: _*).withRequestTimeout(timeout)).map(new WSHttpResponse(_)))

  override def await[A](future: Future[A])(implicit timeout: FiniteDuration) = Await.result(future, timeout)

} 
Example 5
Source File: MockWsClient.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.mocks
import org.scalamock.handlers.CallHandler
import org.scalamock.scalatest.MockFactory
import play.api.libs.ws.{BodyWritable, WSClient, WSRequest, WSResponse}

import scala.concurrent.Future
import scala.concurrent.duration.Duration

trait MockWsClient extends MockFactory {

  val mockWsClient: WSClient = mock[WSClient]
  val mockWsRequest: WSRequest = mock[WSRequest]

  object MockWsClient {

    def url(url: String): CallHandler[WSRequest] = {
      (mockWsClient.url(_: String))
        .expects(url)
    }
  }

  object MockWsRequest {

    def withHttpHeaders(headers: Seq[(String, String)]): CallHandler[WSRequest] = {
      (mockWsRequest.withHttpHeaders _ ).expects(*)
    }

    def withRequestTimeout(timeout: Duration): CallHandler[WSRequest] = {
      (mockWsRequest.withRequestTimeout(_: Duration))
        .expects(timeout)
    }

    def post[I: BodyWritable](body: I): CallHandler[Future[WSResponse]] = {
      (mockWsRequest.post(_: I)(_: BodyWritable[I]))
        .expects(body, *)
    }
  }

} 
Example 6
Source File: NRSConnectorSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.vatapi.connectors

import java.util.concurrent.TimeoutException

import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.libs.json.{JsResultException, Json, Writes}
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.vatapi.UnitSpec
import uk.gov.hmrc.vatapi.assets.TestConstants.NRSResponse._
import uk.gov.hmrc.vatapi.httpparsers.NrsSubmissionHttpParser.NrsSubmissionOutcome
import uk.gov.hmrc.vatapi.httpparsers.{EmptyNrsData, NRSData}
import uk.gov.hmrc.vatapi.mocks.MockHttp
import uk.gov.hmrc.vatapi.mocks.config.MockAppContext
import uk.gov.hmrc.vatapi.models.NRSSubmission

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class NRSConnectorSpec extends UnitSpec with GuiceOneAppPerSuite
  with MockHttp
  with MockAppContext {

  class Setup {
    val wsClient = mock[WSClient]
    val testNrsConnector = new NRSConnector(mockHttp, mockAppContext, wsClient)

    MockAppContext.nrsMaxTimeoutMilliseconds returns 5000

    val testUrl: String = testNrsConnector.nrsSubmissionUrl(testVrn.vrn)
    def result(requestBody: NRSSubmission): Future[NrsSubmissionOutcome] = testNrsConnector.submit(testVrn, requestBody)
  }

  implicit val hc: HeaderCarrier = HeaderCarrier(nsStamp = 2L)

  val testVrn = Vrn("123456789")

  "NRSConnector.submit" should {

    "successful responses are returned from the connector" should {
      "return the correctly formatted NRS Data model" in new Setup {

        val request = mock[WSRequest]
        val response = mock[WSResponse]
        val expectedResponse = NRSData("2dd537bc-4244-4ebf-bac9-96321be13cdc","This has been deprecated - DO NOT USE","")


        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]
        val meJson = Json.toJson(nrsSubmission)

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(meJson))(any())).thenReturn(Future.successful(response))
        when(response.json).thenReturn(nrsResponseJson)
        when(response.status).thenReturn(202)

        await(result(nrsSubmission)) shouldBe Right(expectedResponse)

      }

    }

    "return EmptyNrsData" when {
      "the connection times out" in new Setup {

        val request = mock[WSRequest]

        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(Json.toJson(nrsSubmission)))(any())).thenReturn(Future.failed(new TimeoutException("Expected Error")))

        await(result(nrsSubmission)) shouldBe Right(EmptyNrsData)

      }

      "the response JSON cannot be parsed" in new Setup {

        val request = mock[WSRequest]
        val response = mock[WSResponse]

        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(Json.toJson(nrsSubmission)))(any())).thenReturn(Future.successful(response))
        when(response.json).thenThrow(JsResultException(Seq()))
        await(result(nrsSubmission)) shouldBe Right(EmptyNrsData)

      }
    }
  }
} 
Example 7
Source File: HttpRequest.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package support.functional

import play.api.libs.ws.{EmptyBody, WSRequest, WSResponse}
import support.WSClient

import scala.concurrent.Future

trait HttpRequest extends WSClient {

  private val GET = "GET"
  private val POST = "POST"
  private val PUT = "PUT"

  case class HttpRequest(verb: String, wsRequest: WSRequest, body: Option[String] = None) {
    def execute: Future[WSResponse] = body.fold(executeNoBody)(executeWithBody)

    private def executeNoBody: Future[WSResponse] = verb match {
      case GET  => wsRequest.get()
      case POST => wsRequest.post(EmptyBody)
      case PUT => wsRequest.put(EmptyBody)
    }

    private def executeWithBody(body: String): Future[WSResponse] = verb match {
      case GET  => wsRequest.get()
      case POST => wsRequest.post(body)
      case PUT => wsRequest.put(body)
    }
  }

  def httpGet(url: String): HttpRequest = {
    HttpRequest(GET, buildRequest(url))
  }

  def httpPost(url: String): HttpRequest = {
    HttpRequest(POST, buildRequest(url))
  }

  def httpPut(url: String): HttpRequest = {
    HttpRequest(PUT, buildRequest(url))
  }

  def requestWithHeaders(request: HttpRequest, headers: Seq[(String, String)]): HttpRequest = {
    request.copy(wsRequest = request.wsRequest.withHttpHeaders(headers: _*))
  }

  def requestWithBody(request: HttpRequest, body: String): HttpRequest = {
    request.copy(body = Some(body))
  }
} 
Example 8
package commons_test.test_helpers

import commons_test.test_helpers.WsScalaTestClientWithHost.TestWsClient
import org.scalatestplus.play.PortNumber
import play.api.libs.ws.{WSClient, WSRequest}

trait WsScalaTestClientWithHost {

  def wsUrl(url: String)(implicit testWsClient: TestWsClient): WSRequest = {
    testWsClient.url(url)
  }

}

object WsScalaTestClientWithHost {
  case class TestWsClient(host: Host, portNumber: PortNumber, wsClient: WSClient) {
    def url(url: String): WSRequest = {
      wsClient.url(host.value + portNumber.value + url)
    }
  }
} 
Example 9
Source File: AthanorClient.scala    From tap   with Apache License 2.0 5 votes vote down vote up
package controllers.external.athanor

import io.heta.tap.data.results.StringListResult
import javax.inject.Inject
import io.heta.tap.util.AppConfig
import play.api.Logger
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._ // scalastyle:ignore




class AthanorClient @Inject()(wsClient: WSClient, config: AppConfig)(implicit ec: ExecutionContext) {

  val logger: Logger = Logger(this.getClass)

  val athanorURL= config.getAthanorURL

  def process(text:String,parameter:String,start:Long):Future[StringListResult] = {
    //logger.info(s"Analysing with athanor: $text")

    val url = athanorURL + parameter
    logger.info(s"Analysing with athanor at this url: $url")

    val request: WSRequest = wsClient.url(url)

    val athanorRequest: WSRequest = request
      .withHttpHeaders("Accept" -> "application/json")
      .withRequestTimeout(30000.millis)

    val futureResponse: Future[WSResponse] = athanorRequest.post(text)


    val result: Future[StringListResult] = {
      val decoded = futureResponse.map { response =>
        val res = decodeRepsonse(response)
        val queryTime = (System.currentTimeMillis() - start).toInt
        StringListResult(res,"ok",querytime = queryTime)
      }
      val errMsg = "There was a problem connecting to the Athanor server."
      futureResponse.recover {
        case e: Any => {
          val msg = s"$errMsg: $e"
          logger.error(msg)
          StringListResult(Vector(),msg)
        }
        //case _ => logger.error(errMsg)
      }
      decoded
    }
    result
  }

  case class AthanorMsg(message: String, results: Vector[Vector[String]])

  def decodeRepsonse(response:WSResponse): Vector[Vector[String]] = {
    val resBody = response.body
    if(resBody.nonEmpty && resBody.contains(":[[")) {
      logger.debug("Decoding response: "+ resBody)

      import play.api.libs.functional.syntax._ //scalastyle:ignore
      import play.api.libs.json._ //scalastyle:ignore

      implicit val AMWrites: Writes[AthanorMsg] = (
        (JsPath \ "message").write[String] and
          (JsPath \ "results").write[Vector[Vector[String]]]
        ) (unlift(AthanorMsg.unapply))

      implicit val AMReads: Reads[AthanorMsg] = (
        (JsPath \ "message").read[String] and
          (JsPath \ "results").read[Vector[Vector[String]]]
        ) (AthanorMsg.apply _)

      val athanorMsg:AthanorMsg = response.json.as[AthanorMsg]
      logger.debug("Athanor message: " + athanorMsg.message)
      logger.debug("Athanor results: " + athanorMsg.results)
      athanorMsg.results
    } else {
      logger.error("There was a problem: " + resBody) //TODO If we get to here, we need to return the message to the client!!
      Vector()
    }
  }
} 
Example 10
Source File: WSPost.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.http.ws.default

import play.api.libs.json.{Json, Writes}
import play.api.libs.ws.WSRequest
import uk.gov.hmrc.http.{CorePost, HeaderCarrier, HttpResponse, PostHttpTransport}
import uk.gov.hmrc.play.http.ws.{WSExecute, WSHttpResponse, WSRequestBuilder}

import scala.concurrent.{ExecutionContext, Future}

trait WSPost extends CorePost with PostHttpTransport with WSRequestBuilder with WSExecute {

  def withEmptyBody(request: WSRequest): WSRequest

  override def doPost[A](
    url: String,
    body: A,
    headers: Seq[(String, String)]
  )(
    implicit rds: Writes[A],
    hc: HeaderCarrier,
    ec: ExecutionContext
  ): Future[HttpResponse] =
    execute(buildRequest(url, headers).withBody(Json.toJson(body)), "POST")
      .map(WSHttpResponse.apply)

  override def doFormPost(
    url: String,
    body: Map[String, Seq[String]],
    headers: Seq[(String, String)]
  )(
    implicit hc: HeaderCarrier,
    ec: ExecutionContext
  ): Future[HttpResponse] =
    execute(buildRequest(url, headers).withBody(body), "POST")
      .map(WSHttpResponse.apply)

  override def doPostString(
    url: String,
    body: String,
    headers: Seq[(String, String)]
  )(
    implicit hc: HeaderCarrier,
    ec: ExecutionContext
  ): Future[HttpResponse] =
    execute(buildRequest(url, headers).withBody(body), "POST")
      .map(WSHttpResponse.apply)

  override def doEmptyPost[A](
    url: String,
    headers: Seq[(String, String)]
  )(
    implicit hc: HeaderCarrier,
    ec: ExecutionContext
  ): Future[HttpResponse] =
    execute(withEmptyBody(buildRequest(url, headers)), "POST")
      .map(WSHttpResponse.apply)
} 
Example 11
Source File: Connector.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.connectors

import com.github.ghik.silencer.silent
import play.api.libs.ws.{WS, WSRequest}
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.HeaderCarrierConverter

trait RequestBuilder {
  def buildRequest(url: String)(implicit hc: HeaderCarrier): WSRequest
}

object RequestBuilder {
  def headers(hc: HeaderCarrier): Seq[(String,String)] = {
    hc.headers.filter { case (name,value) => name != HeaderCarrierConverter.Path }
  }
}

trait PlayWSRequestBuilder extends RequestBuilder {
  @silent("deprecated")
  def buildRequest(url: String)(implicit hc: HeaderCarrier): WSRequest =
    WS.url(url)(play.api.Play.current)
      .withHeaders(RequestBuilder.headers(hc): _*)
}

trait WSClientRequestBuilder extends RequestBuilder {
  this: WSClientProvider =>
  def buildRequest(url: String)(implicit hc: HeaderCarrier): WSRequest = client.url(url).withHeaders(RequestBuilder.headers(hc): _*)
} 
Example 12
Source File: Connector.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.connectors

import play.api.libs.ws.{WSClient, WSRequest}
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.HeaderCarrierConverter

trait RequestBuilder {
  def buildRequest(url: String)(implicit hc: HeaderCarrier): WSRequest
}

object RequestBuilder {
  def headers(hc: HeaderCarrier): Seq[(String, String)] =
    hc.headers.filter {
      case (name, value) => name != HeaderCarrierConverter.Path
    }
}

trait WSClientRequestBuilder extends RequestBuilder {
  def client: WSClient
  def buildRequest(url: String)(implicit hc: HeaderCarrier): WSRequest =
    client.url(url).withHttpHeaders(RequestBuilder.headers(hc): _*)
} 
Example 13
Source File: DownloadParentPoms.scala    From scaladex   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ch.epfl.scala.index
package data
package maven

import java.nio.file.{Files, Path}

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import ch.epfl.scala.index.data.download.PlayWsDownloader
import org.slf4j.LoggerFactory
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}

import scala.util.Failure

class DownloadParentPoms(repository: LocalPomRepository,
                         paths: DataPaths,
                         tmp: Option[Path] = None)(
    implicit val system: ActorSystem,
    implicit val materializer: ActorMaterializer
) extends PlayWsDownloader {

  private val log = LoggerFactory.getLogger(getClass)

  assert(
    repository == LocalPomRepository.MavenCentral || repository == LocalPomRepository.Bintray
  )

  val parentPomsPath = paths.parentPoms(repository)
  val pomReader =
    tmp match {
      case Some(path) => PomsReader.tmp(paths, path)
      case None       => PomsReader(repository, paths)
    }

  
    val parentPomsToDownload: Set[Dependency] =
      pomReader
        .load()
        .collect {
          case Failure(m: MissingParentPom) => m.dep
        }
        .toSet

    log.debug(s"to download: ${parentPomsToDownload.size}")
    log.debug(s"last failed: $lastFailedToDownload")

    if (parentPomsToDownload.size > lastFailedToDownload) {

      val downloaded =
        download[Dependency, Int]("Download parent POMs",
                                  parentPomsToDownload,
                                  downloadRequest,
                                  processResponse,
                                  parallelism = 32)
      val failedDownloads = downloaded.sum

      log.warn(s"failed downloads: $failedDownloads")

      if (0 < failedDownloads && parentPomsToDownload.size != failedDownloads) {

        run(failedDownloads) // grand-parent poms, etc
      }
    }
  }
} 
Example 14
Source File: BintrayDownloadPoms.scala    From scaladex   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ch.epfl.scala.index.data
package bintray

import download.PlayWsDownloader
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path}

import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
import play.api.libs.ws.ahc.AhcWSClient
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import org.slf4j.LoggerFactory

class BintrayDownloadPoms(paths: DataPaths)(
    implicit val system: ActorSystem,
    implicit val materializer: ActorMaterializer
) extends PlayWsDownloader {

  private val log = LoggerFactory.getLogger(getClass)

  private val bintrayPomBase = paths.poms(LocalPomRepository.Bintray)

  
  def run(): Unit = {

    download[BintraySearch, Unit]("Downloading POMs",
                                  searchesBySha1,
                                  downloadRequest,
                                  processPomDownload,
                                  parallelism = 32)
    ()
  }
} 
Example 15
Source File: MillinerHatSignup.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.resourceManagement

import org.hatdex.hat.resourceManagement.models.HatSignup
import play.api.cache.AsyncCacheApi
import play.api.http.Status._
import play.api.libs.json.{ JsError, JsSuccess }
import play.api.libs.ws.{ WSClient, WSRequest, WSResponse }
import play.api.{ Configuration, Logger }

import scala.concurrent.duration._
import scala.concurrent.{ ExecutionContext, Future }

trait MillinerHatSignup {
  val logger: Logger
  val ws: WSClient
  val configuration: Configuration
  val schema: String = configuration.get[String]("resourceManagement.millinerAddress") match {
    case address if address.startsWith("https") => "https://"
    case address if address.startsWith("http")  => "http://"
    case _                                      => "https://"
  }

  val millinerAddress: String = configuration.get[String]("resourceManagement.millinerAddress")
    .stripPrefix("http://")
    .stripPrefix("https://")
  val hatSharedSecret: String = configuration.get[String]("resourceManagement.hatSharedSecret")

  val cache: AsyncCacheApi

  def getHatSignup(hatAddress: String)(implicit ec: ExecutionContext): Future[HatSignup] = {
    // Cache the signup information for subsequent calls (For private/public key and database details)
    cache.getOrElseUpdate[HatSignup](s"configuration:$hatAddress") {
      val request: WSRequest = ws.url(s"$schema$millinerAddress/api/manage/configuration/$hatAddress")
        .withVirtualHost(millinerAddress)
        .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> hatSharedSecret)

      val futureResponse: Future[WSResponse] = request.get()
      futureResponse.map { response =>
        response.status match {
          case OK =>
            response.json.validate[HatSignup] match {
              case signup: JsSuccess[HatSignup] =>
                logger.debug(s"Got back configuration: ${signup.value}")
                cache.set(s"configuration:$hatAddress", signup.value, 1.minute)
                signup.value
              case e: JsError =>
                logger.error(s"Parsing HAT configuration failed: $e")
                throw new HatServerDiscoveryException("Fetching HAT configuration failed")
            }
          case _ =>
            logger.error(s"Fetching HAT configuration failed: ${response.body}")
            throw new HatServerDiscoveryException("Fetching HAT configuration failed")
        }
      }
    }
  }

} 
Example 16
Source File: PlaySpecs2Spec.scala    From play-grpc   with Apache License 2.0 5 votes vote down vote up
package play.grpc.specs2

import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner

import play.api.inject.bind
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.libs.ws.WSRequest
import play.api.routing.Router
import play.api.test._

import akka.grpc.internal.GrpcProtocolNative

import example.myapp.helloworld.grpc.helloworld._

import io.grpc.Status


@RunWith(classOf[JUnitRunner])
class PlaySpecs2Spec extends ForServer with ServerGrpcClient with PlaySpecification with ApplicationFactories {

  protected def applicationFactory: ApplicationFactory =
    withGuiceApp(GuiceApplicationBuilder().overrides(bind[Router].to[GreeterServiceImpl]))

  // RICH: Still need to work out how to make WSClient work properly with endpoints
  def wsUrl(path: String)(implicit running: RunningServer): WSRequest = {
    val ws  = running.app.injector.instanceOf[WSClient]
    val url = running.endpoints.httpEndpoint.get.pathUrl(path)
    ws.url(url)
  }

  "A Play server bound to a gRPC router" should {
    "give a 404 when routing a non-gRPC request" >> { implicit rs: RunningServer =>
      val result = await(wsUrl("/").get)
      result.status must ===(404) // Maybe should be a 426, see #396
    }
    "give a 415 error when not using a gRPC content-type" >> { implicit rs: RunningServer =>
      val result = await(wsUrl(s"/${GreeterService.name}/FooBar").get)
      result.status must ===(415) // Maybe should be a 426, see #396
    }
    "give a grpc UNIMPLEMENTED when routing a non-existent gRPC method" >> { implicit rs: RunningServer =>
      val result = await(
        wsUrl(s"/${GreeterService.name}/FooBar")
          .addHttpHeaders("Content-Type" -> GrpcProtocolNative.contentType.toString)
          .get,
      )
      result.status must ===(200) // Maybe should be a 426, see #396
      result.header("grpc-status") must beSome(Status.Code.UNIMPLEMENTED.value().toString)
    }
    "give a grpc INVALID_ARGUMENT error when routing an empty request to a gRPC method" >> {
      implicit rs: RunningServer =>
        val result = await(
          wsUrl(s"/${GreeterService.name}/SayHello")
            .addHttpHeaders("Content-Type" -> GrpcProtocolNative.contentType.toString)
            .get,
        )
        result.status must ===(200) // Maybe should be a 426, see #396

        // grpc-status 3 means INVALID_ARGUMENT error. See https://developers.google.com/maps-booking/reference/grpc-api/status_codes
        result.header("grpc-status") must beSome(Status.Code.INVALID_ARGUMENT.value().toString)
    }
    "work with a gRPC client" >> { implicit rs: RunningServer =>
      withGrpcClient[GreeterServiceClient] { client: GreeterServiceClient =>
        val reply = await(client.sayHello(HelloRequest("Alice")))
        reply.message must ===("Hello, Alice!")
      }
    }
  }
}