<commit_before>package services.ordnance_survey

import services.AddressLookupService
import models.domain.disposal_of_vehicle.AddressViewModel
import utils.helpers.Config
import play.api.Logger
import com.ning.http.client.Realm.AuthScheme
import services.ordnance_survey.domain.{OSAddressbaseResult, OSAddressbaseSearchResponse}
import scala.concurrent.{Future, ExecutionContext}
import ExecutionContext.Implicits.global
import javax.inject.Inject
import play.api.libs.ws.Response

class AddressLookupServiceImpl @Inject()(ws: services.WebService) extends AddressLookupService {
  val username = s"${Config.ordnanceSurveyUsername}"
  val password = s"${Config.ordnanceSurveyPassword}"
  val baseUrl = s"${Config.ordnanceSurveyBaseUrl}"

  // Call the real web service.
  override protected def callPostcodeWebService(postcode: String): Future[Response] = {
    val endPoint = s"$baseUrl/postcode?postcode=${postcodeWithNoSpaces(postcode)}&dataset=dpa" // TODO add lpi to URL, but need to set organisation as Option on the type.
    Logger.debug(s"Calling Ordnance Survey postcode lookup service on $endPoint...")
    ws.url(endPoint).
      withAuth(username = username, password = password, scheme = AuthScheme.BASIC).
      withRequestTimeout(30000). // Timeout is in milliseconds // TODO Timeout value should be read from config file
      get()
  }

  // Call the real web service.
  override protected def callUprnWebService(uprn: String): Future[Response] = {
    val endPoint = s"$baseUrl/uprn?uprn=$uprn&dataset=dpa" // TODO add lpi to URL, but need to set orgnaisation as Option on the type.
    Logger.debug(s"Calling Ordnance Survey uprn lookup service on $endPoint...")
    ws.url(endPoint).
      withAuth(username = username, password = password, scheme = AuthScheme.BASIC).
      withRequestTimeout(30000). // Timeout is in milliseconds // TODO Timeout value should be read from config file
      get()
  }

  override def extractFromJson(resp: Response): Option[Seq[OSAddressbaseResult]] = {
    try {
      val oSAddressbaseSearchResponse = resp.json.as[OSAddressbaseSearchResponse]
      oSAddressbaseSearchResponse.results
    }
    catch {
      case e: Throwable => None
    }
  }

  override def fetchAddressesForPostcode(postcode: String): Future[Seq[(String, String)]] = {
    def toUprnsAndAddresses(resp: Response): Seq[(String, String)] = {
      val results = extractFromJson(resp)

      if (results.isDefined){
        val extractedAddressKeyValues = results.get.map { address =>
          address.DPA match {
            case Some(dpa) => Some((dpa.UPRN, dpa.address))
            case _ => None // TODO check if an LPI entry is present
          }
        }
        extractedAddressKeyValues.flatten // Remove any "None" entries.
      }
      else {
        // Handle no results
        Logger.debug(s"No results returned for postcode: $postcode")
        Seq.empty
      }
    }

    callPostcodeWebService(postcode).map { resp =>
      Logger.debug(s"Http response code from Ordnance Survey postcode lookup service was: ${resp.status}")
      if (resp.status == play.api.http.Status.OK) toUprnsAndAddresses(resp).sortBy(x => x._1) // Sort by UPRN. TODO check with BAs how they would want to sort the list
      else Seq.empty // The service returned http code other than 200
    }.recoverWith {
      case e: Throwable => Future {
        Logger.error(s"Ordnance Survey postcode lookup service error: $e")
        Seq.empty
      }
    }
  }

  def postcodeWithNoSpaces(postcode: String): String = {
    val space = ' '
    postcode.filter(_ != space)
  }


  override def fetchAddressForUprn(uprn: String): Future[Option[AddressViewModel]] = {
    // Extract result from response and return as a view model.
    def toAddressViewModel(resp: Response) = {
      val results = extractFromJson(resp)

      if (results.isDefined) {
        val addressViewModels = results.get.map { address =>
          address.DPA match {
            case Some(dpa) => Some(AddressViewModel(uprn = Some(dpa.UPRN.toLong), address = dpa.address.split(",")))
            case _ => None // TODO check if an LPI entry is present
          }
        }.flatten
        require(addressViewModels.length >= 1, s"Should be at least one address for the UPRN: $uprn")
        Some(addressViewModels.head)
      }
      else {
        Logger.error(s"No results returned by web service for submitted UPRN: $uprn")
        None
      }
    }

    callUprnWebService(uprn).map { resp =>
      Logger.debug(s"Http response code from Ordnance Survey uprn lookup service was: ${resp.status}")
      if (resp.status == play.api.http.Status.OK) toAddressViewModel(resp)
      else None
    }.recoverWith {
      case e: Throwable => Future {
        Logger.error(s"Ordnance Survey uprn lookup service error: $e")
        None
      }
    }
  }
}<commit_msg>Minor cleanups to make the address service calls clearer<commit_after>package services.ordnance_survey

import services.AddressLookupService
import models.domain.disposal_of_vehicle.AddressViewModel
import utils.helpers.Config
import play.api.Logger
import com.ning.http.client.Realm.AuthScheme
import services.ordnance_survey.domain.{OSAddressbaseResult, OSAddressbaseSearchResponse}
import scala.concurrent.{Future, ExecutionContext}
import ExecutionContext.Implicits.global
import javax.inject.Inject
import play.api.libs.ws.Response

class AddressLookupServiceImpl @Inject()(ws: services.WebService) extends AddressLookupService {
  val username = s"${Config.ordnanceSurveyUsername}"
  val password = s"${<PASSWORD>}"
  val baseUrl = s"${Config.ordnanceSurveyBaseUrl}"

  override protected def callPostcodeWebService(postcode: String): Future[Response] = {
    val endPoint = s"$baseUrl/postcode?postcode=${postcodeWithNoSpaces(postcode)}&dataset=dpa" // TODO add lpi to URL, but need to set organisation as Option on the type.
    Logger.debug(s"Calling Ordnance Survey postcode lookup service on $endPoint...")
    ws.url(endPoint).
      withAuth(username = username, password = password, scheme = AuthScheme.BASIC).
      withRequestTimeout(30000). // Timeout is in milliseconds // TODO Timeout value should be read from config file
      get()
  }

  override protected def callUprnWebService(uprn: String): Future[Response] = {
    val endPoint = s"$baseUrl/uprn?uprn=$uprn&dataset=dpa" // TODO add lpi to URL, but need to set orgnaisation as Option on the type.
    Logger.debug(s"Calling Ordnance Survey uprn lookup service on $endPoint...")
    ws.url(endPoint).
      withAuth(username = username, password = password, scheme = AuthScheme.BASIC).
      withRequestTimeout(30000). // Timeout is in milliseconds // TODO Timeout value should be read from config file
      get()
  }

  override def extractFromJson(resp: Response): Option[Seq[OSAddressbaseResult]] = {
    val response = resp.json.asOpt[OSAddressbaseSearchResponse]
    response.flatMap(_.results)
  }

  override def fetchAddressesForPostcode(postcode: String): Future[Seq[(String, String)]] = {
    def toUprnsAndAddresses(resp: Response): Seq[(String, String)] = {
      extractFromJson(resp) match {
        case Some(r) =>
          r.flatMap { address =>
            address.DPA match {
              case Some(dpa) => Some((dpa.UPRN, dpa.address))
              case _ => None // TODO check if an LPI entry is present
            }
          }
        case None =>
          // Handle no results
          Logger.debug(s"No results returned for postcode: $postcode")
          Seq.empty
      }
    }

    callPostcodeWebService(postcode).map { resp =>
      Logger.debug(s"Http response code from Ordnance Survey postcode lookup service was: ${resp.status}")
      if (resp.status == play.api.http.Status.OK) toUprnsAndAddresses(resp).sortBy(x => x._1) // Sort by UPRN. TODO check with BAs how they would want to sort the list
      else Seq.empty // The service returned http code other than 200
    }.recover {
      case e: Throwable =>
        Logger.error(s"Ordnance Survey postcode lookup service error: $e")
        Seq.empty
    }
  }

  def postcodeWithNoSpaces(postcode: String): String = postcode.filter(_ != ' ')

  override def fetchAddressForUprn(uprn: String): Future[Option[AddressViewModel]] = {
    // Extract result from response and return as a view model.
    def toAddressViewModel(resp: Response) = {

      extractFromJson(resp) match {
        case Some(results) =>
          val addressViewModels = results.flatMap { address =>
            address.DPA match {
              case Some(dpa) => Some(AddressViewModel(uprn = Some(dpa.UPRN.toLong), address = dpa.address.split(",")))
              case _ => None // TODO check if an LPI entry is present
            }
          }
          require(addressViewModels.length >= 1, s"Should be at least one address for the UPRN: $uprn")
          Some(addressViewModels.head)
        case None =>
          Logger.error(s"No results returned by web service for submitted UPRN: $uprn")
          None
      }
    }

    callUprnWebService(uprn).map { resp =>
      Logger.debug(s"Http response code from Ordnance Survey uprn lookup service was: ${resp.status}")
      if (resp.status == play.api.http.Status.OK) toAddressViewModel(resp)
      else None
    }.recover {
      case e: Throwable =>
        Logger.error(s"Ordnance Survey uprn lookup service error: $e")
        None
    }
  }
}
{'fraction_non_alphanumeric': 0.10064900521332057, 'fraction_numerical': 0.0031918289179699967, 'mean_word_length': 3.1796353935082258, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4041683', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2718, 'n_words': 761}