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

class ClientSettings extends AnyRef

Configures and creates HttpClient.

ClientSettings is a mutable structure. With each applied change, the settings are modified and returned. After applying the desired settings, a client is created using a factory method.

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

Instance Constructors

  1. new ClientSettings()

    Creates client settings.

    Creates client settings.

    Default Settings

    Key

    Value

    accept

    */*

    acceptEncodings

    Nil

    bufferSize

    8192

    readTimeout

    30000

    continueTimeout

    1000

    coookies

    CookieStore.alwaysEmpty

    trust

    (Not set)

    incoming

    (Not set)

    outgoing

    (Not set)


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. def accept(one: MediaRange, more: MediaRange*): ClientSettings.this.type

    Sets accepted content types.

    Sets accepted content types.

    The Accept header for each outgoing request is set accordingly.

  5. def accept(ranges: Seq[MediaRange]): ClientSettings.this.type

    Sets accepted content types.

    Sets accepted content types.

    The Accept header for each outgoing request is set accordingly.

  6. def acceptEncoding(one: ContentCodingRange, more: ContentCodingRange*): ClientSettings.this.type

    Sets accepted content encodings.

    Sets accepted content encodings.

    The Accept-Encoding header for each outgoing request is set accordingly.

  7. def acceptEncoding(ranges: Seq[ContentCodingRange]): ClientSettings.this.type

    Sets accepted content encodings.

    Sets accepted content encodings.

    The Accept-Encoding header for each outgoing request is set accordingly.

  8. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  9. def bufferSize(size: Int): ClientSettings.this.type

    Sets buffer size.

    Sets buffer size.

    The buffer size specifies the size in bytes of client socket's send and receive buffers.

  10. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  11. def continueTimeout(timeout: Int): ClientSettings.this.type

    Sets continue timeout.

    Sets continue timeout.

    The continue timeout specifies how many milliseconds to wait for a 100 (Continue) response before sending the request body.

    Note

    This applies only to requests that include an Except header set to 100-Continue.

  12. def cookies(cookieStore: CookieStore = CookieStore()): ClientSettings.this.type

    Sets cookie store.

  13. def create(): HttpClient

    Creates client using current settings.

  14. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  15. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  16. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  17. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  18. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  19. def incoming(filter: ResponseFilter): ClientSettings.this.type

    Adds supplied response filter.

  20. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  21. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  22. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  23. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  24. def outgoing(filter: RequestFilter): ClientSettings.this.type

    Adds supplied request filter.

  25. def readTimeout(timeout: Int): ClientSettings.this.type

    Sets read timeout.

    Sets read timeout.

    The read timeout specifies how many milliseconds a read from client socket blocks before it times out, whereafter SocketTimeoutException is thrown.

  26. def reset(): ClientSettings.this.type

    Resets to default settings.

  27. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  28. def toString(): String
    Definition Classes
    AnyRef → Any
  29. def trust(manager: TrustManager): ClientSettings.this.type

    Sets trust manager.

    Sets trust manager.

    manager

    trust manager used for HTTPS connections

  30. def trust(truststore: File, storeType: String = "JKS", password: Option[String] = None): ClientSettings.this.type

    Sets truststore.

    Sets truststore.

    truststore

    truststore used for HTTPS connections

    storeType

    store type (e.g., JKS or PKCS12)

    password

    store password

  31. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  32. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  33. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()

Inherited from AnyRef

Inherited from Any

Ungrouped