<commit_before>package hmda.api.http

import akka.http.scaladsl.server._
import akka.http.scaladsl.server.Directives._
import akka.event.LoggingAdapter
import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.model.StatusCodes
import hmda.api.model.ErrorResponse
import hmda.api.protocol.processing.ApiErrorProtocol
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._

trait HmdaCustomDirectives extends ApiErrorProtocol {
  val log: LoggingAdapter

  implicit def authRejectionHandler =
    RejectionHandler.newBuilder()
      .handle {
        case AuthorizationFailedRejection =>
          extractUri { uri =>
            val errorResponse = ErrorResponse(403, "Unauthorized Access", uri.toString)
            complete(ToResponseMarshallable(StatusCodes.Forbidden -> errorResponse))
          }
      }
      .handleNotFound {
        extractUri { uri =>
          val errorResponse = ErrorResponse(404, "Not Found", uri.toString)
          complete(ToResponseMarshallable(StatusCodes.NotFound -> errorResponse))
        }
      }
      .result()

  def timedGet: Directive0 = get & time
  def timedPost: Directive0 = post & time

  def headerAuthorize: Directive0 =
    authorize(ctx =>
      hasHeader("CFPB-HMDA-Username", ctx) &&
        hasHeader("CFPB-HMDA-Institutions", ctx))

  def institutionAuthorize(institutionId: String): Directive0 = {
    authorize { ctx =>
      institutionIdsFromHeader(ctx).map(_.toLowerCase)
        .contains(institutionId.toLowerCase)
    }
  }<commit_msg>Error responses show path, not full URI<commit_after>package hmda.api.http

import akka.http.scaladsl.server._
import akka.http.scaladsl.server.Directives._
import akka.event.LoggingAdapter
import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.model.StatusCodes
import hmda.api.model.ErrorResponse
import hmda.api.protocol.processing.ApiErrorProtocol
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._

trait HmdaCustomDirectives extends ApiErrorProtocol {
  val log: LoggingAdapter

  implicit def authRejectionHandler =
    RejectionHandler.newBuilder()
      .handle {
        case AuthorizationFailedRejection =>
          extractUri { uri =>
            val errorResponse = ErrorResponse(403, "Unauthorized Access", uri.path.toString)
            complete(ToResponseMarshallable(StatusCodes.Forbidden -> errorResponse))
          }
      }
      .handleNotFound {
        extractUri { uri =>
          val errorResponse = ErrorResponse(404, "Not Found", uri.path.toString)
          complete(ToResponseMarshallable(StatusCodes.NotFound -> errorResponse))
        }
      }
      .result()

  def timedGet: Directive0 = get & time
  def timedPost: Directive0 = post & time

  def headerAuthorize: Directive0 =
    authorize(ctx =>
      hasHeader("CFPB-HMDA-Username", ctx) &&
        hasHeader("CFPB-HMDA-Institutions", ctx))

  def institutionAuthorize(institutionId: String): Directive0 = {
    authorize { ctx =>
      institutionIdsFromHeader(ctx).map(_.toLowerCase)
        .contains(institutionId.toLowerCase)
    }
  }
{'fraction_non_alphanumeric': 0.09128763613068545, 'fraction_numerical': 0.0064061499039077515, 'mean_word_length': 3.4935251798561153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '4890381', 'n_tokens_mistral': 922, 'n_tokens_neox': 920, 'n_words': 166}