sdinesh94
sdinesh94

Reputation: 1176

What is the meaning of the term 'context' in Akka/Spray

I am new to Akka/Spray and Scala. I see from akka docs that there is HttpRequest which is the Http Request class and there is also the RequestContext which according to akka docs

encapsulates the context of an HttpRequest as it flows through a akka-http Route structure.

May I know what is the meaning of the term 'context' here?

Thanks

Upvotes: 0

Views: 196

Answers (1)

Michał
Michał

Reputation: 2494

Let's take a look at how akka-http Route type is defined:

type Route = scala.Function1[akka.http.scaladsl.server.RequestContext, scala.concurrent.Future[akka.http.scaladsl.server.RouteResult]]

As we can see Route essentially is a function transforming RequestContext into Future[RouteResult], so you can think of RequestContext as a portion of data representing HttpRequest and some additional information.

In fact, taking a deeper look into akka.http.scaladsl.server.RequestContext we will find a trait:

trait RequestContext {
  val request: HttpRequest
  val unmatchedPath: Uri.Path
  implicit def executionContext: ExecutionContextExecutor
  implicit def materializer: Materializer
  def log: LoggingAdapter
  def settings: RoutingSettings
  def parserSettings: ParserSettings
  def reconfigure(
    executionContext: ExecutionContextExecutor = executionContext,
    materializer:     Materializer             = materializer,
    log:              LoggingAdapter           = log,
    settings:         RoutingSettings          = settings): RequestContext
  // ...
}

(comments removed from code)

So essentially context gives you some metadata about HttpRequest.

Upvotes: 1

Related Questions