forked from playframework/playframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AhcWSRequest.scala
277 lines (221 loc) · 8.16 KB
/
AhcWSRequest.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.api.libs.ws.ahc
import java.io.File
import java.net.URI
import akka.stream.scaladsl.Source
import akka.util.ByteString
import play.api.libs.ws._
import play.api.mvc.MultipartFormData
import scala.concurrent.Future
import scala.concurrent.duration.Duration
/**
* A WS Request backed by AsyncHTTPClient.
*/
case class AhcWSRequest(underlying: StandaloneAhcWSRequest) extends WSRequest with WSBodyWritables {
override type Self = WSRequest
override type Response = WSResponse
/**
* The URI for this request
*/
override def uri: URI = underlying.uri
/**
* The base URL for this request
*/
override def url: String = underlying.url
/**
* The method for this request
*/
override def method: String = underlying.method
/**
* The body of this request
*/
override def body: WSBody = underlying.body
/**
* The headers for this request
*/
override def headers: Map[String, Seq[String]] = underlying.headers
/**
* The cookies for this request
*/
override def cookies: Seq[WSCookie] = underlying.cookies
/**
* The query string for this request
*/
override def queryString: Map[String, Seq[String]] = underlying.queryString
/**
* A calculator of the signature for this request
*/
override def calc: Option[WSSignatureCalculator] = underlying.calc
/**
* The authentication this request should use
*/
override def auth: Option[(String, String, WSAuthScheme)] = underlying.auth
/**
* Whether this request should follow redirects
*/
override def followRedirects: Option[Boolean] = underlying.followRedirects
/**
* The timeout for the request
*/
override def requestTimeout: Option[Duration] = underlying.requestTimeout
/**
* The virtual host this request will use
*/
override def virtualHost: Option[String] = underlying.virtualHost
/**
* The proxy server this request will use
*/
override def proxyServer: Option[WSProxyServer] = underlying.proxyServer
override def contentType: Option[String] = underlying.contentType
override def sign(calc: WSSignatureCalculator): Self = toWSRequest {
underlying.sign(calc)
}
override def withCookies(cookies: WSCookie*): Self = toWSRequest {
underlying.withCookies(cookies: _*)
}
override def addCookies(cookies: WSCookie*): Self = toWSRequest {
underlying.addCookies(cookies: _*)
}
override def withQueryStringParameters(parameters: (String, String)*): Self = toWSRequest {
underlying.withQueryStringParameters(parameters: _*)
}
override def withAuth(username: String, password: String, scheme: WSAuthScheme): Self = toWSRequest {
underlying.withAuth(username, password, scheme)
}
@deprecated("Use addHttpHeaders or withHttpHeaders", "2.6.0")
override def withHeaders(hdrs: (String, String)*): Self = toWSRequest {
underlying.addHttpHeaders(hdrs: _*)
}
override def withHttpHeaders(headers: (String, String)*): Self = toWSRequest {
underlying.withHttpHeaders(headers: _*)
}
@deprecated("Use addQueryStringParameters or withQueryStringParameters", "2.6.0")
override def withQueryString(parameters: (String, String)*): Self = toWSRequest {
underlying.addQueryStringParameters(parameters: _*)
}
override def withFollowRedirects(follow: Boolean): Self = toWSRequest {
underlying.withFollowRedirects(follow)
}
override def withDisableUrlEncoding(disableUrlEncoding: Boolean): Self = toWSRequest {
underlying.withDisableUrlEncoding(disableUrlEncoding)
}
override def withRequestTimeout(timeout: Duration): Self = toWSRequest {
underlying.withRequestTimeout(timeout)
}
override def withRequestFilter(filter: WSRequestFilter): Self = toWSRequest {
underlying.withRequestFilter(filter.asInstanceOf[WSRequestFilter])
}
override def withVirtualHost(vh: String): Self = toWSRequest {
underlying.withVirtualHost(vh)
}
override def withProxyServer(proxyServer: WSProxyServer): Self = toWSRequest {
underlying.withProxyServer(proxyServer)
}
override def withMethod(method: String): Self = toWSRequest {
underlying.withMethod(method)
}
override def withBody[T: BodyWritable](body: T): Self = toWSRequest(underlying.withBody(body))
override def withUrl(url: String): WSRequest = toWSRequest(underlying.withUrl(url))
//-------------------------------------------------
// PATCH
//-------------------------------------------------
/**
* Perform a PATCH on the request asynchronously.
*/
override def patch[T: BodyWritable](body: T): Future[Response] = withBody(body).execute("PATCH")
/**
* Perform a PATCH on the request asynchronously.
* Request body won't be chunked
*
*
* @deprecated Import WSBodyWritables and use the typeclass in preference to this method, since 2.6.0
*/
@deprecated("Use patch(bodyWritable)", "2.6.0")
override def patch(body: File): Future[WSResponse] = {
patch[File](body)
}
/**
* Perform a PATCH on the request asynchronously.
*
* @deprecated Import WSBodyWritables and use the typeclass in preference to this method, since 2.6.0
*/
@deprecated("Use patch(bodyWritable)", "2.6.0")
override def patch(body: Source[MultipartFormData.Part[Source[ByteString, _]], _]): Future[WSResponse] = {
patch[Source[MultipartFormData.Part[Source[ByteString, _]], _]](body)
}
//-------------------------------------------------
// POST
//-------------------------------------------------
/**
* Perform a POST on the request asynchronously.
*/
override def post[T: BodyWritable](body: T): Future[Response] = withBody(body).execute("POST")
/**
* Perform a POST on the request asynchronously.
*
* @deprecated Import WSBodyWritables and use the typeclass in preference to this method, since 2.6.0
*/
@deprecated("Use post(BodyWritable)", "2.6.0")
override def post(body: File): Future[WSResponse] = {
post[File](body)
}
/**
* Performs a POST on the request asynchronously.
*
* @deprecated Import WSBodyWritables and use the typeclass in preference to this method, since 2.6.0
*/
@deprecated("Use post(BodyWritable)", "2.6.0")
override def post(body: Source[MultipartFormData.Part[Source[ByteString, _]], _]): Future[WSResponse] = {
post[Source[MultipartFormData.Part[Source[ByteString, _]], _]](body)
}
//-------------------------------------------------
// PUT
//-------------------------------------------------
/**
* Perform a PUT on the request asynchronously.
*/
override def put[T: BodyWritable](body: T): Future[Response] = withBody(body).execute("PUT")
/**
* Perform a PUT on the request asynchronously.
* Request body won't be chunked
*
* @deprecated Import WSBodyWritables and use the typeclass in preference to this method, since 2.6.0
*/
@deprecated("Use put(BodyWritable)", "2.6.0")
override def put(body: File): Future[WSResponse] = {
put[File](body)
}
/**
* Perform a PUT on the request asynchronously.
*
* @deprecated Import WSBodyWritables and use the typeclass in preference to this method, since 2.6.0
*/
@deprecated("Use put(BodyWritable)", "2.6.0")
override def put(body: Source[MultipartFormData.Part[Source[ByteString, _]], _]): Future[WSResponse] = {
put[Source[MultipartFormData.Part[Source[ByteString, _]], _]](body)
}
override def delete(): Future[Response] = execute("DELETE")
override def get(): Future[Response] = execute("GET")
override def head(): Future[Response] = execute("HEAD")
override def options(): Future[Response] = execute("OPTIONS")
override def stream(): Future[Response] = {
val futureResponse: Future[StandaloneWSResponse] = underlying.stream()
futureResponse.map { f =>
AhcWSResponse(f)
}(play.core.Execution.trampoline)
}
def execute(method: String): Future[Response] = {
withMethod(method).execute()
}
override def execute(): Future[Response] = {
val futureResponse: Future[StandaloneWSResponse] = underlying.execute()
futureResponse.map { f =>
AhcWSResponse(f)
}(play.core.Execution.trampoline)
}
private def toWSRequest(request: StandaloneWSRequest): Self = {
AhcWSRequest(request.asInstanceOf[StandaloneAhcWSRequest])
}
}