<commit_before>package com.wix.restaurants

import java.net.URLEncoder
import java.util.{Date, List => JList, Map => JMap}

import akka.actor.ActorSystem
import akka.http.javadsl.model.headers.Authorization
import akka.http.scaladsl.client.RequestBuilding.{Delete, Get, Post, Put}
import com.openrest.v1_1._
import com.wix.rest.rfc7807.api.model.ErrorResponse
import com.wix.rest.rfc7807.client.AkkaRestClient
import com.wix.restaurants.authentication.model.{Namespaces, User => AuthenticationUser}
import com.wix.restaurants.authentication.{DefaultWixRestaurantsAuthenticationClient, WixRestaurantsAuthenticationClient}
import com.wix.restaurants.authorization.{AuthorizationClient, DefaultAuthorizationClient}
import com.wix.restaurants.exceptions._
import com.wix.restaurants.i18n.Locale
import com.wix.restaurants.json.Json
import com.wix.restaurants.orders.{Orders, Statuses => OrderStatuses}
import com.wix.restaurants.reservations.{Reservation, Reservations, Statuses => ReservationStatuses}

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext}

class DefaultWixRestaurantsClient(apiUrl: String = "https://api.wixrestaurants.com/v2",
                                  authApiUrl: String = "https://auth.wixrestaurants.com/v2",
                                  readTimeout: Duration = Duration.Inf) extends WixRestaurantsClient {

  private val authenticationClient: WixRestaurantsAuthenticationClient = new DefaultWixRestaurantsAuthenticationClient(
    authApiUrl, Some(readTimeout))
  private val authorizationClient: AuthorizationClient = new DefaultAuthorizationClient(
    apiUrl = apiUrl)

  private implicit val system: ActorSystem = ActorSystem("akka-wix-restaurants-client-system")
  private implicit val executionContext: ExecutionContext = system.dispatcher
  private val client: AkkaRestClient = new AkkaRestClient(errorResponseAsException = ExceptionTranslator.asException)


  override def getAuthenticationClient: WixRestaurantsAuthenticationClient = authenticationClient

  override def getAuthorizationClient: AuthorizationClient = authorizationClient

  override def retrieveRestaurantInfo(restaurantId: String): RestaurantFullInfo = {
    val request = Get(s"$apiUrl/organizations/$restaurantId/full")
    Await.result(client.execute(request) withResult[RestaurantFullInfo](), readTimeout)
  }

  override def retrieveOrganization(organizationId: String): Organization = {
    val request = Get(s"$apiUrl/organizations/$organizationId")
    Await.result(client.execute(request) withResult[Organization](), readTimeout)
  }

  override def setOrganization(accessToken: String, organization: Organization): Organization = {
    val request = Put(s"$apiUrl/organizations/${organization.id}", Json.stringify(organization))
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Organization](), readTimeout)
  }

  override def changeOrganizationLocale(accessToken: String, organizationId: String, locale: Locale): Unit = {
    val request = Post(s"$apiUrl/organizations/$organizationId/change_locale?locale=$locale")
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withoutResult(), readTimeout)
  }

  override def getNotifications(accessToken: String , organizationId: String): Notifications = {
    val request = Get(s"$apiUrl/organizations/$organizationId/notifications")
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Notifications](), readTimeout)
  }

  override def setNotifications(accessToken: String, organizationId: String, notifications: Notifications): Notifications = {
    val request = Put(s"$apiUrl/organizations/$organizationId/notifications", Json.stringify(notifications))
        .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Notifications](), readTimeout)
  }

  override def getSecrets(accessToken: String , organizationId: String): Secrets = {
    val request = Get(s"$apiUrl/organizations/$organizationId/secrets")
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Secrets](), readTimeout)
  }

  override def setSecrets(accessToken: String, organizationId: String, secrets: Secrets): Secrets = {
    val request = Put(s"$apiUrl/organizations/$organizationId/secrets", Json.stringify(secrets))
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Secrets](), readTimeout)
  }

  override def getMenu(accessToken: String , restaurantId: String): Menu = {
    val request = Get(s"$apiUrl/organizations/$restaurantId/menu")
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Menu](), readTimeout)
  }

  override def setMenu(accessToken: String, restaurantId: String, menu: Menu): Menu = {
    val request = Put(s"$apiUrl/organizations/$restaurantId/menu", Json.stringify(menu))
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Menu](), readTimeout)
  }

  override def submitOrder(accessToken: String, order: Order): Order = {
    val request = Post(s"$apiUrl/organizations/${order.restaurantId}/orders", Json.stringify(order))
    Option(accessToken).foreach { theAccessToken => request.addHeader(Authorization.oauth2(theAccessToken)) }
    Await.result(client.execute(request) withResult[Order](), readTimeout)
  }
<commit_msg>Fixed bug causing submitOrder to ignore given access token.<commit_after>package com.wix.restaurants

import java.net.URLEncoder
import java.util.{Date, List => JList, Map => JMap}

import akka.actor.ActorSystem
import akka.http.javadsl.model.headers.Authorization
import akka.http.scaladsl.client.RequestBuilding.{Delete, Get, Post, Put}
import com.openrest.v1_1._
import com.wix.rest.rfc7807.api.model.ErrorResponse
import com.wix.rest.rfc7807.client.AkkaRestClient
import com.wix.restaurants.authentication.model.{Namespaces, User => AuthenticationUser}
import com.wix.restaurants.authentication.{DefaultWixRestaurantsAuthenticationClient, WixRestaurantsAuthenticationClient}
import com.wix.restaurants.authorization.{AuthorizationClient, DefaultAuthorizationClient}
import com.wix.restaurants.exceptions._
import com.wix.restaurants.i18n.Locale
import com.wix.restaurants.json.Json
import com.wix.restaurants.orders.{Orders, Statuses => OrderStatuses}
import com.wix.restaurants.reservations.{Reservation, Reservations, Statuses => ReservationStatuses}

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext}

class DefaultWixRestaurantsClient(apiUrl: String = "https://api.wixrestaurants.com/v2",
                                  authApiUrl: String = "https://auth.wixrestaurants.com/v2",
                                  readTimeout: Duration = Duration.Inf) extends WixRestaurantsClient {

  private val authenticationClient: WixRestaurantsAuthenticationClient = new DefaultWixRestaurantsAuthenticationClient(
    authApiUrl, Some(readTimeout))
  private val authorizationClient: AuthorizationClient = new DefaultAuthorizationClient(
    apiUrl = apiUrl)

  private implicit val system: ActorSystem = ActorSystem("akka-wix-restaurants-client-system")
  private implicit val executionContext: ExecutionContext = system.dispatcher
  private val client: AkkaRestClient = new AkkaRestClient(errorResponseAsException = ExceptionTranslator.asException)


  override def getAuthenticationClient: WixRestaurantsAuthenticationClient = authenticationClient

  override def getAuthorizationClient: AuthorizationClient = authorizationClient

  override def retrieveRestaurantInfo(restaurantId: String): RestaurantFullInfo = {
    val request = Get(s"$apiUrl/organizations/$restaurantId/full")
    Await.result(client.execute(request) withResult[RestaurantFullInfo](), readTimeout)
  }

  override def retrieveOrganization(organizationId: String): Organization = {
    val request = Get(s"$apiUrl/organizations/$organizationId")
    Await.result(client.execute(request) withResult[Organization](), readTimeout)
  }

  override def setOrganization(accessToken: String, organization: Organization): Organization = {
    val request = Put(s"$apiUrl/organizations/${organization.id}", Json.stringify(organization))
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Organization](), readTimeout)
  }

  override def changeOrganizationLocale(accessToken: String, organizationId: String, locale: Locale): Unit = {
    val request = Post(s"$apiUrl/organizations/$organizationId/change_locale?locale=$locale")
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withoutResult(), readTimeout)
  }

  override def getNotifications(accessToken: String , organizationId: String): Notifications = {
    val request = Get(s"$apiUrl/organizations/$organizationId/notifications")
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Notifications](), readTimeout)
  }

  override def setNotifications(accessToken: String, organizationId: String, notifications: Notifications): Notifications = {
    val request = Put(s"$apiUrl/organizations/$organizationId/notifications", Json.stringify(notifications))
        .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Notifications](), readTimeout)
  }

  override def getSecrets(accessToken: String , organizationId: String): Secrets = {
    val request = Get(s"$apiUrl/organizations/$organizationId/secrets")
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Secrets](), readTimeout)
  }

  override def setSecrets(accessToken: String, organizationId: String, secrets: Secrets): Secrets = {
    val request = Put(s"$apiUrl/organizations/$organizationId/secrets", Json.stringify(secrets))
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Secrets](), readTimeout)
  }

  override def getMenu(accessToken: String , restaurantId: String): Menu = {
    val request = Get(s"$apiUrl/organizations/$restaurantId/menu")
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Menu](), readTimeout)
  }

  override def setMenu(accessToken: String, restaurantId: String, menu: Menu): Menu = {
    val request = Put(s"$apiUrl/organizations/$restaurantId/menu", Json.stringify(menu))
      .addHeader(Authorization.oauth2(accessToken))
    Await.result(client.execute(request) withResult[Menu](), readTimeout)
  }

  override def submitOrder(accessToken: String, order: Order): Order = {
    val anonymousRequest = Post(s"$apiUrl/organizations/${order.restaurantId}/orders", Json.stringify(order))
    val request = Option(accessToken) match {
      case Some(at) => anonymousRequest.addHeader(Authorization.oauth2(at))
      case None => anonymousRequest
    }
    Await.result(client.execute(request) withResult[Order](), readTimeout)
  }

{'fraction_non_alphanumeric': 0.10690922125638498, 'fraction_numerical': 0.004122233175015683, 'mean_word_length': 5.788321167883212, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '424651', 'n_tokens_mistral': 3224, 'n_tokens_neox': 3151, 'n_words': 565}