<commit_before>import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, Await}
import scala.concurrent.duration.Duration
import com.typesafe.config._
import com.ning.http.client.AsyncHttpClient
import com.ning.http.util.Base64
import dispatch._
import net.liftweb.json._

object SphereHello extends App {
  val api = "https://api.sphere.io/"
  val auth_api = "https://auth.sphere.io/oauth/token"

  //see configuration file scala/src/main/resources/application.conf
  val conf = ConfigFactory.load()
  val clientId = conf.getString("sphere-hello.clientId")
  val clientSecret = conf.getString("sphere-hello.clientSecret")
  val projectKey = conf.getString("sphere-hello.projectKey")
  
  val h = new Http(new AsyncHttpClient)
  
  /** @return oauth access token */
  def login(clientId: String, clientSecret: String, projectKey: String): Future[String] = {
    val encoded = Base64.encode(s"${clientId}:${clientSecret}".getBytes)
    h(url(auth_api).POST
      .setHeader("Authorization", s"Basic ${encoded}")
      .setHeader("Content-Type", "application/x-www-form-urlencoded")
      .setBody(s"grant_type=client_credentials&scope=manage_project:${projectKey}")
    ).map(rsp => parse(rsp.getResponseBody) \ "access_token" match {
      case JString(s) => s
      case _ => throw new IllegalStateException("Auth response does not contain 'access_token'.\n" + rsp.getResponseBody)
    })
  }
  
  /** Issues a query to get all products, see [[http://sphere.io/dev/HTTP_API_Projects_Products.html#product-projections-by-query]].
    * @return <a href="HTTP_API.html#paged-query-response">PagedQueryResult</a>
    *         with the <code>results</code> array of <a href="#product-projection">ProductProjection</a> JSON. */
  def getProducts(oauthToken: String): Future[JValue] = {
    h(url(s"${api}${projectKey}/product-projections").setHeader("Authorization", s"Bearer ${oauthToken}"))
      .map(rsp => parse(rsp.getResponseBody))
  }

  val f = for {
    token <- login(clientId, clientSecret, projectKey)
    products <- getProducts(token)
  } yield products \ "total" match {
    case JInt(n) => n
    case _ => throw new IllegalStateException("Product query response does not contain 'total'.")
  }

  val nrOfProducts = Await.result(f, Duration.Inf)
  h.shutdown()
  println("Number of products: " + nrOfProducts)
}<commit_msg>constant names in Scala example<commit_after>import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, Await}
import scala.concurrent.duration.Duration
import com.typesafe.config._
import com.ning.http.client.AsyncHttpClient
import com.ning.http.util.Base64
import dispatch._
import net.liftweb.json._

object SphereHello extends App {
  val ApiUrl = "https://api.sphere.io/"
  val AuthApiUrl = "https://auth.sphere.io/oauth/token"

  //see configuration file scala/src/main/resources/application.conf
  val conf = ConfigFactory.load()
  val clientId = conf.getString("sphere-hello.clientId")
  val clientSecret = conf.getString("sphere-hello.clientSecret")
  val projectKey = conf.getString("sphere-hello.projectKey")
  
  val h = new Http(new AsyncHttpClient)
  
  /** @return oauth access token */
  def login(clientId: String, clientSecret: String, projectKey: String): Future[String] = {
    val encoded = Base64.encode(s"${clientId}:${clientSecret}".getBytes)
    h(url(AuthApiUrl).POST
      .setHeader("Authorization", s"Basic ${encoded}")
      .setHeader("Content-Type", "application/x-www-form-urlencoded")
      .setBody(s"grant_type=client_credentials&scope=manage_project:${projectKey}")
    ).map(rsp => parse(rsp.getResponseBody) \ "access_token" match {
      case JString(s) => s
      case _ => throw new IllegalStateException("Auth response does not contain 'access_token'.\n" + rsp.getResponseBody)
    })
  }
  
  /** Issues a query to get all products, see [[http://sphere.io/dev/HTTP_API_Projects_Products.html#product-projections-by-query]].
    * @return <a href="HTTP_API.html#paged-query-response">PagedQueryResult</a>
    *         with the <code>results</code> array of <a href="#product-projection">ProductProjection</a> JSON. */
  def getProducts(oauthToken: String): Future[JValue] = {
    h(url(s"${ApiUrl}${projectKey}/product-projections").setHeader("Authorization", s"Bearer ${oauthToken}"))
      .map(rsp => parse(rsp.getResponseBody))
  }

  val f = for {
    token <- login(clientId, clientSecret, projectKey)
    products <- getProducts(token)
  } yield products \ "total" match {
    case JInt(n) => n
    case _ => throw new IllegalStateException("Product query response does not contain 'total'.")
  }

  val nrOfProducts = Await.result(f, Duration.Inf)
  h.shutdown()
  println("Number of products: " + nrOfProducts)
}
{'fraction_non_alphanumeric': 0.13973982375157365, 'fraction_numerical': 0.001678556441460344, 'mean_word_length': 4.3986409966024915, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '1870719', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1487, 'n_words': 306}