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.BodyParsers import scamper.Implicits.stringToUri import scamper.RequestMethods.GET import scamper.client.HttpClient implicit val parser = BodyParsers.text() def getMessageOfTheDay(): Either[Int, String] = { val req = GET("localhost:8080/motd") // Send request and handle response HttpClient.send(req) { res => res.status.isSuccessful match { case true => Right(res.as[String]) case false => Left(res.status.code) } } }
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 instance of
HttpClient
for one-time usage. If you plan to send multiple requests, you
can create and maintain a reference to an instance, and use it as the client.
With that, you also get access to methods corresponding to the standard HTTP
request methods.
import scamper.BodyParsers import scamper.Implicits.stringToUri import scamper.client.HttpClient implicit val parser = BodyParsers.text() // Create HttpClient instance val client = HttpClient(bufferSize = 4096, readTimeout = 3000) def getMessageOfTheDay(): Either[Int, String] = { // Use client instance client.get("http://localhost:8080/motd") { res => res.status.isSuccessful match { case true => Right(res.as[String]) case false => Left(res.status.code) } } }
And if the client is declared as an implicit value, you can make use of send()
on the request itself.
import scamper.BodyParsers import scamper.Implicits.stringToUri import scamper.RequestMethods.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(bufferSize = 8192, readTimeout = 1000) implicit val parser = BodyParsers.text(4096) GET("http://localhost:8080/motd") .withAccept("text/plain") .withAcceptLanguage("en-US; q=0.6", "fr-CA; q=0.4") .send(res => println(res.as[String])) // Send request and print response
- Alphabetic
- By Inheritance
- client
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
- trait HttpClient extends AnyRef
Provides utility for sending request and handling response.
- case class RequestAborted(message: String) extends HttpException with Product with Serializable
Indicates request was aborted.
- trait ResponseFilter extends ResponseHandler[Boolean]
Provides utility for filtering HTTP response.
- trait ResponseHandler[T] extends AnyRef
Provides utility for handling response.
Value Members
- object HttpClient
Provides factory for
HttpClient
. - object Implicits
Includes client-side type classes.
- object ResponseFilter
Includes status-based
ResponseFilter
implementations.