-
Notifications
You must be signed in to change notification settings - Fork 63
/
HttpClient.scala
81 lines (72 loc) · 2.95 KB
/
HttpClient.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright 2014 http4s.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.http4s.blaze.http
import org.http4s.blaze.http.HttpClientSession.ReleaseableResponse
import org.http4s.blaze.http.http1.client.BasicHttp1ClientSessionManager
import org.http4s.blaze.util.Execution
import scala.concurrent.Future
/** Generic interface for making HTTP client requests
*
* A [[HttpClient]] hides the details of the underlying sockets, connection
* pools etc. For a representation of a concrete session implementation see
* [[HttpClientSession]].
*/
trait HttpClient extends ClientActions {
/** Release underlying resources associated with the [[HttpClient]]
*
* The resultant `Future[Unit]` will resolve once the resources associated
* with the client have been freed.
*/
def close(): Future[Unit]
/** Dispatch a request, resulting in the response
*
* @param request request to dispatch
* @return the response. The cleanup of the resources associated with
* this dispatch are tied to the [[BodyReader]] of the [[ClientResponse]].
* Release of resources is triggered by complete consumption of the `MessageBody`
* or by calling `MessageBody.discard()`, whichever comes first.
*/
def unsafeDispatch(request: HttpRequest): Future[ReleaseableResponse]
/** Safely dispatch a client request
*
* Resources associated with this dispatch are guarenteed to be cleaned up during the
* resolution of the returned `Future[T]`, regardless of if it is successful or not.
*
* @note The resources _may_ be cleaned up before the future resolves, but this is
* dependant on both the implementation and if the resources have been fully
* consumed.
*/
def apply[T](request: HttpRequest)(f: ClientResponse => Future[T]): Future[T] =
unsafeDispatch(request).flatMap { resp =>
val result = f(resp)
result.onComplete(_ => resp.release())(Execution.directec)
result
}(Execution.directec)
}
object HttpClient {
/** Basic implementation of a HTTP/1.1 client.
*
* This client doesn't do any session pooling, so one request = one socket connection.
*/
lazy val basicHttp1Client: HttpClient = {
val pool = new BasicHttp1ClientSessionManager(HttpClientConfig.Default)
new HttpClientImpl(pool)
}
lazy val pooledHttpClient: HttpClient = {
val pool = ClientSessionManagerImpl(HttpClientConfig.Default)
new HttpClientImpl(pool)
}
}