All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pagerduty.funhttpclient.HttpDriver.scala Maven / Gradle / Ivy

/*
 * Copyright 2015 PagerDuty, Inc.
 *
 * Author: Jesse Haber-Kucharsky 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pagerduty.funhttpclient

import akka.actor._
import akka.io._
import akka.pattern.ask
import akka.util.Timeout
import spray.http._

import scala.concurrent._

/**
 * HTTP drivers are responsible for actually executing the HTTP request and getting back a response.
 *
 * The driver is used in [[HttpOps.request]] to make a request.
 */
trait HttpDriver {

  /**
   * Make a HTTP request asynchronously.
   *
   * Assumes that HTTP errors will be thrown as exceptions.
   */
  def execute(r: HttpRequest): Future[HttpResponse]
}

object HttpDriver {
  def apply(system: ActorSystem, timeout: Timeout): HttpDriver =
    new Impl(system, timeout)

  /**
   * An HTTP driver that executes requests via [[spray.can]] and an actor system.
   *
   * If a response is not returned within the timeout, a [[TimeoutException]] is thrown.
   *
   * @param system The actor system that will be used to launch new actors internally for fulfilling
   *               requests.
   * @param timeout HTTP time-out for requests.
   */
  class Impl(system: ActorSystem, timeout: Timeout) extends HttpDriver {
    implicit val actorSystem = system
    implicit val actorTimeout = timeout

    override def execute(r: HttpRequest): Future[HttpResponse] =
      (IO(spray.can.Http) ? r).mapTo[HttpResponse]
  }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy