<commit_before>package com.gu.pandomainauth

import dispatch._

import scala.concurrent.Future


object PublicSettings {
  val bucketName = "pan-domain-auth-settings"
  val cookieName = "gutoolsAuth"
  val assymCookieName = s"$cookieName-assym"

  def getPublicKey(domain: String)(implicit client: Http, ec: scala.concurrent.ExecutionContext): Future[Either[Throwable, String]] = {
    val req = host("s3-eu-west-1.amazonaws.com").secure / bucketName / s"$domain.settings.public"
    client(req OK as.String).either.left.map(new PublicKeyAcquisitionException(_)).map { attemptedKey =>
      attemptedKey.right.flatMap(validateKey)
    }
  }

  private[pandomainauth] def validateKey(pubKey: String): Either[Throwable, String] = {
    if ("[a-zA-Z0-9+/]+={0,3}".r.pattern.matcher(pubKey).matches) Right(pubKey)
    else Left(PublicKeyFormatException)
  }

  class PublicKeyAcquisitionException(cause: Throwable) extends Exception(cause.getMessage, cause)
  object PublicKeyFormatException extends Exception("Invalid public key")
}
<commit_msg>Simplify function that fetches the publicKey<commit_after>package com.gu.pandomainauth

import dispatch._

import scala.concurrent.Future


object PublicSettings {
  val bucketName = "pan-domain-auth-settings"
  val cookieName = "gutoolsAuth"
  val assymCookieName = s"$cookieName-assym"

  def getPublicKey(domain: String)(implicit client: Http, ec: scala.concurrent.ExecutionContext): Future[String] = {
    val req = host("s3-eu-west-1.amazonaws.com").secure / bucketName / s"$domain.settings.public"
    client(req OK as.String).either.left.map(new PublicKeyAcquisitionException(_)).map { attemptedKey =>
      attemptedKey.right.flatMap(validateKey)
    } flatMap {
      case Right(key) => Future.successful(key)
      case Left(err) => Future.failed(err)
    }
  }

  private[pandomainauth] def validateKey(pubKey: String): Either[Throwable, String] = {
    if ("[a-zA-Z0-9+/]+={0,3}".r.pattern.matcher(pubKey).matches) Right(pubKey)
    else Left(PublicKeyFormatException)
  }

  class PublicKeyAcquisitionException(cause: Throwable) extends Exception(cause.getMessage, cause)
  object PublicKeyFormatException extends Exception("Invalid public key")
}

{'fraction_non_alphanumeric': 0.1230140717203813, 'fraction_numerical': 0.005447117566954153, 'mean_word_length': 5.054945054945055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3320314', 'n_tokens_mistral': 698, 'n_tokens_neox': 677, 'n_words': 131}