object Client
Build HTTP clients.
val client = Client("github.com") val fetchGithubHomePage: IO[String] = for { homePage <- client.run(Get("/"))(_.readAs[String]) _ <- client.stop() } yield (homePage) println(fetchGithubHomePage.unsafeRunSync)
Once created an HTTP client maintains several TCP connections to the remote server, and can be reused to run several requests. It is better to create a dedicated client this way if you plan to send many requests to the same server.
However there are some situations where you have a single request to run, or you have a batch of requests to send over an unknown set of servers. In this case you can use the run operation that automatically create a temporary HTTP client to run the request and trash it after the exchange completion.
val homePage = Client.run(Get("http://github.com/"))(_.readAs[String])
Note that in this case, for each request, a new client (including the whole IO infrastructure) will to be created, and a new TCP connection will be opened to the server.
- Source
- Client.scala
- Alphabetic
- By Inheritance
- Client
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
apply(host: String, port: Int = 80, scheme: String = "http", ssl: ClientConfiguration = SSL.ClientConfiguration.default, protocol: String = HTTP, maxConnections: Int = 10)(implicit executor: ExecutionContext): Client
Create a new HTTP Client for the provided host/port.
Create a new HTTP Client for the provided host/port.
- host
the host to use to setup the TCP connections.
- port
the port to use to setup the TCP connections.
- scheme
either http or https.
- ssl
if provided the custom SSL configuration to use for this client.
- protocol
the protocol to use for this client (HTTP or HTTP/2).
- maxConnections
the maximum number of TCP connections to maintain with the remote server.
- executor
the
scala.concurrent.ExecutionContext
to use to run user code.- returns
an HTTP client instance.
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
run[A](request: Request, followRedirects: Boolean = true, timeout: FiniteDuration = FiniteDuration(30, "seconds"), protocol: String = HTTP)(f: (Response) ⇒ IO[A] = (_: Response) => IO.unit)(implicit executor: ExecutionContext, ssl: ClientConfiguration): IO[A]
Run the provided request with a temporary client, and apply the f function to the response.
Run the provided request with a temporary client, and apply the f function to the response.
- request
the request to run. It must include a proper
Host
header.- followRedirects
if true follow the intermediate HTTP redirects. Default to true.
- timeout
maximum amount of time allowed to retrieve the response and extract the return value.
- protocol
the protocol to use for this client (HTTP or HTTP/2).
- f
a function that eventually receive the response and transform it to a value of type
A
.- returns
eventually a value of type
A
.
-
def
runSync[A](request: Request, followRedirects: Boolean = true, timeout: FiniteDuration = FiniteDuration(30, "seconds"), protocol: String = HTTP)(f: (Response) ⇒ IO[A] = (_: Response) => IO.unit)(implicit executor: ExecutionContext, ssl: ClientConfiguration): A
Run the provided request with a temporary client, and apply the f function to the response.
Run the provided request with a temporary client, and apply the f function to the response. This operation is blocking and the calling thread will wait for the request to be completed.
- request
the request to run. It must include a proper
Host
header.- followRedirects
if true follow the intermediate HTTP redirects. Default to true.
- timeout
maximum amount of time allowed to retrieve the response and extract the return value.
- protocol
the protocol to use for this client (HTTP or HTTP/2).
- f
a function that eventually receive the response and transform it to a value of type
A
.- returns
synchronously a value of type
A
.
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )