Packages

  • package root

    Provided herein is API documentation for Scamper, the HTTP library for Scala.

    Provided herein is API documentation for Scamper, the HTTP library for Scala.

    Definition Classes
    root
  • package scamper

    Defines core types.

    Defines core types.

    HTTP Messages

    At the core of Scamper is HttpMessage, which is a trait that defines the fundamental characteristics of an HTTP message. HttpRequest and HttpResponse extend the specification to define characteristics specific to their respective message types.

    An HttpRequest is created using a factory method defined in its companion object. Or you can start with a RequestMethod and use builder methods to further define the request.

    import scamper.Header
    import scamper.Implicits.stringToUri
    import scamper.RequestMethod.Registry.Get
    
    val request = Get("/motd").setHeaders(
      Header("Host: localhost:8080"),
      Header("Accept: text/plain")
    )
    
    printf("Request Method: %s%n", request.method)
    printf("Target URI: %s%n", request.target)
    
    request.headers.foreach(println)
    
    val host: Option[String] = request.getHeaderValue("Host")

    An HttpResponse is created using a factory method defined in its companion object. Or you can start with a ResponseStatus and use builder methods to further define the response.

    import scamper.{ BodyParser, Header }
    import scamper.Implicits.stringToEntity
    import scamper.ResponseStatus.Registry.Ok
    
    val response = Ok("There is an answer.").setHeaders(
      Header("Content-Type: text/plain"),
      Header("Connection: close")
    )
    
    printf("Status Code: %s%n", response.statusCode)
    printf("Reason Phrase: %s%n", response.reasonPhrase)
    
    response.headers.foreach(println)
    
    val contentType: Option[String] = response.getHeaderValue("Content-Type")
    
    implicit val parser = BodyParser.text()
    
    printf("Body: %s%n", response.as[String])
    Definition Classes
    root
  • package client

    Provides HTTP client implementation.

    Provides HTTP client implementation.

    Using HTTP Client

    The HttpClient object can be used to send a request and handle the response.

    import scamper.BodyParser
    import scamper.Implicits.stringToUri
    import scamper.RequestMethod.Registry.Get
    import scamper.client.HttpClient
    
    implicit val parser = BodyParser.text()
    
    def getMessageOfTheDay(): Either[Int, String] = {
      val req = Get("localhost:8080/motd")
    
      // Send request and handle response
      HttpClient.send(req) { res =>
        res.isSuccessful match {
          case true  => Right(res.as[String])
          case false => Left(res.statusCode)
        }
      }
    }

    Note the request must be created with an absolute URI to make effective use of the client.

    Creating HTTP Client

    When using the HttpClient object as the client, it creates an HttpClient instance for one-time usage. If you plan to send multiple requests, you can create and maintain a reference to a client. With it, you also get access to methods corresponding to standard HTTP request methods.

    import scamper.BodyParser
    import scamper.Implicits.stringToUri
    import scamper.client.HttpClient
    
    implicit val parser = BodyParser.text()
    
    // Create HttpClient instance
    val client = HttpClient()
    
    def getMessageOfTheDay(): Either[Int, String] = {
      // Use client instance
      client.get("http://localhost:8080/motd") { res =>
        res.isSuccessful match {
          case true  => Right(res.as[String])
          case false => Left(res.statusCode)
        }
      }
    }

    And, if an implicit client is in scope, you can make use of send() on the request itself.

    import scamper.BodyParser
    import scamper.Implicits.stringToUri
    import scamper.RequestMethod.Registry.Get
    import scamper.client.HttpClient
    import scamper.client.Implicits.ClientHttpRequestType // Adds send method to request
    import scamper.headers.{ Accept, AcceptLanguage }
    import scamper.types.Implicits.{ stringToMediaRange, stringToLanguageRange }
    
    implicit val client = HttpClient()
    implicit val parser = BodyParser.text(4096)
    
    Get("http://localhost:8080/motd")
      .setAccept("text/plain")
      .setAcceptLanguage("en-US; q=0.6", "fr-CA; q=0.4")
      .send(res => println(res.as[String])) // Send request and print response

    See also ClientSettings for information about configuring the HTTP client before it is created.

    Definition Classes
    scamper
  • ClientSettings
  • HttpClient
  • Implicits
  • RequestAborted
  • RequestFilter
  • ResponseFilter
  • ResponseHandler

trait HttpClient extends AnyRef

Provides utility for sending request and handling response.

A client is created from either HttpClient.apply() or ClientSettings.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. HttpClient
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def accept: Seq[MediaRange]

    Gets accepted content types.

  2. abstract def acceptEncoding: Seq[ContentCodingRange]

    Gets accepted encodings.

  3. abstract def bufferSize: Int

    Gets buffer size.

  4. abstract def continueTimeout: Int

    Gets continue timeout.

  5. abstract def cookies: CookieStore

    Gets cookie store.

  6. abstract def delete[T](target: Uri, headers: Seq[Header] = Nil, cookies: Seq[PlainCookie] = Nil)(handler: ResponseHandler[T]): T

    Sends DELETE request and passes response to handler.

    Sends DELETE request and passes response to handler.

    target

    request target

    headers

    request headers

    cookies

    request cookies

    handler

    response handler

    returns

    value from response handler

    Exceptions thrown

    IllegalArgumentException if target is not absolute

  7. abstract def get[T](target: Uri, headers: Seq[Header] = Nil, cookies: Seq[PlainCookie] = Nil)(handler: ResponseHandler[T]): T

    Sends GET request and passes response to handler.

    Sends GET request and passes response to handler.

    target

    request target

    headers

    request headers

    cookies

    request cookies

    handler

    response handler

    returns

    value from response handler

    Exceptions thrown

    IllegalArgumentException if target is not absolute

  8. abstract def post[T](target: Uri, headers: Seq[Header] = Nil, cookies: Seq[PlainCookie] = Nil, body: Entity = Entity.empty)(handler: ResponseHandler[T]): T

    Sends POST request and passes response to handler.

    Sends POST request and passes response to handler.

    target

    request target

    headers

    request headers

    cookies

    request cookies

    body

    message body

    handler

    response handler

    returns

    value from response handler

    Exceptions thrown

    IllegalArgumentException if target is not absolute

  9. abstract def put[T](target: Uri, headers: Seq[Header] = Nil, cookies: Seq[PlainCookie] = Nil, body: Entity = Entity.empty)(handler: ResponseHandler[T]): T

    Sends PUT request and passes response to handler.

    Sends PUT request and passes response to handler.

    target

    request target

    headers

    request headers

    cookies

    request cookies

    body

    message body

    handler

    response handler

    returns

    value from response handler

    Exceptions thrown

    IllegalArgumentException if target is not absolute

  10. abstract def readTimeout: Int

    Gets read timeout.

  11. abstract def send[T](request: HttpRequest)(handler: ResponseHandler[T]): T

    Sends request and passes response to supplied handler.

    Sends request and passes response to supplied handler.

    request

    outgoing request

    handler

    response handler

    returns

    value from response handler

    Exceptions thrown

    IllegalArgumentException if request.target is not absolute

  12. abstract def websocket[T](target: Uri, headers: Seq[Header] = Nil, cookies: Seq[PlainCookie] = Nil)(handler: (WebSocketSession) ⇒ T): T

    Connects to WebSocket server at given target and passes established session to supplied handler.

    Connects to WebSocket server at given target and passes established session to supplied handler.

    target

    WebSocket target

    headers

    additional headers to include in WebSocket request

    cookies

    cookies to include in WebSocket request

    handler

    WebSocket session handler

    returns

    value from session handler

    Exceptions thrown

    IllegalArgumentException if target is not WebSocket URI (i.e., it must be an absolute URI having a scheme of either "ws" or "wss")

    WebSocketHandshakeFailure if WebSocket handshake fails

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  14. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  15. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  16. def toString(): String
    Definition Classes
    AnyRef → Any
  17. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  18. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()

Inherited from AnyRef

Inherited from Any

Ungrouped