package client
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.
- Alphabetic
- By Inheritance
- client
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- class ClientSettings extends AnyRef
Configures and creates
HttpClient
.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. - trait HttpClient extends AnyRef
Provides utility for sending request and handling response.
Provides utility for sending request and handling response.
A client is created from either HttpClient.apply() or ClientSettings.
- case class RequestAborted(message: String) extends HttpException with Product with Serializable
Indicates request is aborted.
- trait RequestFilter extends AnyRef
Provides utility for filtering outgoing request.
- trait ResponseFilter extends ResponseHandler[HttpResponse]
Provides utility for filtering incoming response.
- trait ResponseHandler[T] extends AnyRef
Provides utility for handling incoming response.
Value Members
- object ClientSettings
Provides factory method for
ClientSettings
. - object HttpClient
Provides factory for creating
HttpClient
. - object Implicits
Includes client-side type classes.
Provided herein is API documentation for Scamper, the HTTP library for Scala.