<commit_before>/*
 * Copyright 2016 HM Revenue & Customs
 *
 * 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 connectors

import _root_.config.WSHttp
import config.MicroserviceAppConfig._
import connectors.ExchangeObjects.Implicits._
import connectors.ExchangeObjects.{ Invitation, InviteApplicant, RegisterApplicant, Registration }
import model.Exceptions.ConnectorException
import model.OnlineTestCommands.Implicits._
import model.OnlineTestCommands._
import play.api.http.Status._
import play.api.libs.iteratee.Iteratee
import uk.gov.hmrc.play.http.HeaderCarrier

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

trait CubiksGatewayClient {
  val http: WSHttp
  val url: String

  def registerApplicants(batchSize: Int)(implicit hc: HeaderCarrier): Future[List[Registration]] = {
    http.GET(s"$url/csr-cubiks-gateway/faststream/register/$batchSize").map { response =>
      if (response.status == OK) {
        response.json.as[List[Registration]]
      } else {
        throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
      }
    }
  }

  def registerApplicant(registerApplicant: RegisterApplicant)(implicit hc: HeaderCarrier): Future[Registration] = {
    http.POST(s"$url/csr-cubiks-gateway/faststream/register", registerApplicant).map { response =>
      if (response.status == OK) {
        response.json.as[Registration]
      } else {
        throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
      }
    }
  }

  def inviteApplicant(inviteApplicant: InviteApplicant)(implicit hc: HeaderCarrier): Future[Invitation] = {
    http.POST(s"$url/csr-cubiks-gateway/faststream/invite", inviteApplicant).map { response =>
      if (response.status == OK) {
        response.json.as[Invitation]
      } else {
        throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
      }
    }
  }

  def inviteApplicants(invitations: List[InviteApplicant])(implicit hc: HeaderCarrier): Future[List[Invitation]] =
  http.POST(s"$url/csr-cubiks-gateway/faststream/batchInvite", invitations).map { response =>
    if (response.status == OK) {
      response.json.as[List[Invitation]]
    } else {
      throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
    }
  }

  def getReport(application: OnlineTestApplicationForReportRetrieving)(implicit hc: HeaderCarrier): Future[OnlineTestReportAvailability] = {
    http.POST(s"$url/csr-cubiks-gateway/report", application).map { response =>
      if (response.status == OK) {
        response.json.as[OnlineTestReportAvailability]
      } else {
        throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
      }
    }
  }

  def downloadXmlReport(reportId: Int)(implicit hc: HeaderCarrier): Future[TestResult] = {
    http.GET(s"$url/csr-cubiks-gateway/faststream/report-xml/$reportId").map { response =>
      if (response.status == OK) {
        response.json.as[TestResult]
      } else {
        throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
      }
    }
  }

  def downloadPdfReport(reportId: Int)(implicit hc: HeaderCarrier): Future[Array[Byte]] = {
    http.playWS.url(s"$url/csr-cubiks-gateway/report-pdf/$reportId").get(respHeaders =>
      if (respHeaders.status == OK) {
        Iteratee.consume[Array[Byte]]()
      } else {
        throw new ConnectorException(
          s"There was a general problem connecting to the Cubiks Gateway to download the PDF report '$reportId'. " +
            s"HTTP response headers were $respHeaders"
        )
      }).flatMap { iteratee => iteratee.run }
  }
}<commit_msg>Remove passed-along header carrier, initialise a new header carrier when talking to cubiks gateway<commit_after>/*
 * Copyright 2016 HM Revenue & Customs
 *
 * 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 connectors

import _root_.config.WSHttp
import config.MicroserviceAppConfig._
import connectors.ExchangeObjects.Implicits._
import connectors.ExchangeObjects.{ Invitation, InviteApplicant, RegisterApplicant, Registration }
import model.Exceptions.ConnectorException
import model.OnlineTestCommands.Implicits._
import model.OnlineTestCommands._
import play.api.http.Status._
import play.api.libs.iteratee.Iteratee
import uk.gov.hmrc.play.http.HeaderCarrier

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

trait CubiksGatewayClient {
  val http: WSHttp
  val url: String

  // Blank out header carriers for calls to LPG. Passing on someone's true-client-ip header will cause them to be reassessed
  // for whitelisting in the LPG as well (even though they've gone from front -> back -> LPG), which leads to undesireable behaviour.
  implicit val blankedHeaderCarrier = new HeaderCarrier()

  def registerApplicants(batchSize: Int): Future[List[Registration]] = {
    http.GET(s"$url/csr-cubiks-gateway/faststream/register/$batchSize").map { response =>
      if (response.status == OK) {
        response.json.as[List[Registration]]
      } else {
        throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
      }
    }
  }

  def registerApplicant(registerApplicant: RegisterApplicant): Future[Registration] = {
    http.POST(s"$url/csr-cubiks-gateway/faststream/register", registerApplicant).map { response =>
      if (response.status == OK) {
        response.json.as[Registration]
      } else {
        throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
      }
    }
  }

  def inviteApplicant(inviteApplicant: InviteApplicant): Future[Invitation] = {
    http.POST(s"$url/csr-cubiks-gateway/faststream/invite", inviteApplicant).map { response =>
      if (response.status == OK) {
        response.json.as[Invitation]
      } else {
        throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
      }
    }
  }

  def inviteApplicants(invitations: List[InviteApplicant]): Future[List[Invitation]] =
  http.POST(s"$url/csr-cubiks-gateway/faststream/batchInvite", invitations).map { response =>
    if (response.status == OK) {
      response.json.as[List[Invitation]]
    } else {
      throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
    }
  }

  def getReport(application: OnlineTestApplicationForReportRetrieving): Future[OnlineTestReportAvailability] = {
    http.POST(s"$url/csr-cubiks-gateway/report", application).map { response =>
      if (response.status == OK) {
        response.json.as[OnlineTestReportAvailability]
      } else {
        throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
      }
    }
  }

  def downloadXmlReport(reportId: Int): Future[TestResult] = {
    http.GET(s"$url/csr-cubiks-gateway/faststream/report-xml/$reportId").map { response =>
      if (response.status == OK) {
        response.json.as[TestResult]
      } else {
        throw new ConnectorException(s"There was a general problem connecting to Cubiks Gateway. HTTP response was $response")
      }
    }
  }

  def downloadPdfReport(reportId: Int): Future[Array[Byte]] = {
    http.playWS.url(s"$url/csr-cubiks-gateway/report-pdf/$reportId").get(respHeaders =>
      if (respHeaders.status == OK) {
        Iteratee.consume[Array[Byte]]()
      } else {
        throw new ConnectorException(
          s"There was a general problem connecting to the Cubiks Gateway to download the PDF report '$reportId'. " +
            s"HTTP response headers were $respHeaders"
        )
      }).flatMap { iteratee => iteratee.run }
  }
}
{'fraction_non_alphanumeric': 0.10016528925619834, 'fraction_numerical': 0.0017630853994490359, 'mean_word_length': 3.739425587467363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7378092', 'n_tokens_mistral': 2692, 'n_tokens_neox': 2618, 'n_words': 709}