<commit_before>package lib

import com.gilt.apidoc.api.v0.models.User
import com.gilt.apidoc.api.v0.{Authorization, Client}
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}
import java.util.UUID

object ApiClient {

  private val unauthenticatedClient = ApiClient(None).client

  def callWith404[T](
    f: Future[T]
  )(implicit ec: scala.concurrent.ExecutionContext): Future[Option[T]] = {

    f.map {
      value => Some(value)
    }.recover {
        case com.gilt.apidoc.api.v0.errors.UnitResponse(404) => None
        case ex: Throwable => throw ex
    }
  }

  def awaitCallWith404[T](
    future: Future[T]
  ): Option[T] = {
    Await.ready(future, 1000.millis).value.get match {
      case Success(value) => Some(value)
      case Failure(ex) => ex match {
        case com.gilt.apidoc.api.v0.errors.UnitResponse(404) => None
        case _ => throw ex
      }
    }
  }

  /**
    * Blocking call to fetch a user. If the provided guid is not a
    * valid UUID, returns none.
    */
  def getUser(guid: String)(implicit ec: ExecutionContext): Option[User] = {
    Try(UUID.fromString(guid)) match {
      case Success(userGuid) => awaitCallWith404( unauthenticatedClient.users.getByGuid(userGuid) )
      case Failure(ex) => None
    }
  }

}

case class ApiClient(user: Option[User]) {

  private val apiHost = Config.requiredString("apidoc.api.host")
  private val apiAuth = Authorization.Basic(Config.requiredString("apidoc.api.token"))
  private val defaultHeaders = Seq(
    user.map { u =><commit_msg>DRY up handling of 404<commit_after>package lib

import com.gilt.apidoc.api.v0.models.User
import com.gilt.apidoc.api.v0.{Authorization, Client}
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}
import java.util.UUID

object ApiClient {

  private val unauthenticatedClient = ApiClient(None).client

  def callWith404[T](
    f: Future[T]
  )(implicit ec: ExecutionContext): Future[Option[T]] = {

    f.map {
      value => Some(value)
    }.recover {
        case com.gilt.apidoc.api.v0.errors.UnitResponse(404) => None
        case ex: Throwable => throw ex
    }
  }

  def awaitCallWith404[T](
    future: Future[T]
  )(implicit ec: ExecutionContext): Option[T] = {
    Await.result(
      callWith404(future),
      1000.millis
    )
  }

  /**
    * Blocking call to fetch a user. If the provided guid is not a
    * valid UUID, returns none.
    */
  def getUser(guid: String)(implicit ec: ExecutionContext): Option[User] = {
    Try(UUID.fromString(guid)) match {
      case Success(userGuid) => awaitCallWith404( unauthenticatedClient.users.getByGuid(userGuid) )
      case Failure(ex) => None
    }
  }

}

case class ApiClient(user: Option[User]) {

  private val apiHost = Config.requiredString("apidoc.api.host")
  private val apiAuth = Authorization.Basic(Config.requiredString("apidoc.api.token"))
  private val defaultHeaders = Seq(
    user.map { u =>
{'fraction_non_alphanumeric': 0.12455050670153645, 'fraction_numerical': 0.015691402419091206, 'mean_word_length': 3.409221902017291, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '5496838', 'n_tokens_mistral': 1060, 'n_tokens_neox': 1007, 'n_words': 226}