Skip to content

Commit 1a911bb

Browse files
authored
Use JDK 11 HttpClient (#158)
Fix #150 - Switches from `HttpURLConnection` to JDK 11's `HttpClient` - `statusMessage` no longer provides access to the "reason phrase" sent by the server. Instead a hardcoded `Map` is used.
1 parent 96e8a04 commit 1a911bb

File tree

8 files changed

+402
-262
lines changed

8 files changed

+402
-262
lines changed

.github/workflows/actions.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
java: ['8', '17']
18+
java: ['11', '17']
1919
steps:
2020
- uses: actions/checkout@v3
2121
- uses: actions/setup-java@v3
@@ -34,7 +34,7 @@ jobs:
3434
- uses: actions/setup-java@v3
3535
with:
3636
distribution: 'temurin'
37-
java-version: 8
37+
java-version: 11
3838
- name: Check Binary Compatibility
3939
run: ./mill -i __.mimaReportBinaryIssues
4040

@@ -55,7 +55,7 @@ jobs:
5555
- uses: actions/setup-java@v3
5656
with:
5757
distribution: 'temurin'
58-
java-version: 8
58+
java-version: 11
5959
- name: Publish to Maven Central
6060
run: |
6161
if [[ $(git tag --points-at HEAD) != '' ]]; then

readme.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,9 @@ know.
660660
As it turns out, Kenneth Reitz's Requests is
661661
[not a lot of code](https://github.com/requests/requests/tree/main/requests).
662662
Most of the heavy lifting is done in other libraries, and his library is a just
663-
thin-shim that makes the API 10x better. It turns out on the JVM most of the
664-
heavy lifting is also done for you, by `java.net.HttpUrlConnection` in the
665-
simplest case, and other libraries like
666-
[AsyncHttpClient](https://github.com/AsyncHttpClient/async-http-client) for more
667-
advanced use cases.
663+
thin-shim that makes the API 10x better. Similarly, it turns out on the JVM most of the
664+
heavy lifting is also done for you. There have always been options, but
665+
since JDK 11 a decent HTTP client is provided in the standard library.
668666

669667
Given that's the case, how hard can it be to port over a dozen Python files to
670668
Scala? This library attempts to do that: class by class, method by method,

requests/src/requests/Model.scala

+18-13
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ case class Request(url: String,
6464

6565
/**
6666
* Represents the different things you can upload in the body of a HTTP
67-
* request. By default allows form-encoded key-value pairs, arrays of bytes,
68-
* strings, files, and inputstreams. These types can be passed directly to
67+
* request. By default, allows form-encoded key-value pairs, arrays of bytes,
68+
* strings, files, and InputStreams. These types can be passed directly to
6969
* the `data` parameter of [[Requester.apply]] and will be wrapped automatically
7070
* by the implicit constructors.
7171
*/
@@ -76,6 +76,7 @@ trait RequestBlob{
7676
object RequestBlob{
7777
object EmptyRequestBlob extends RequestBlob{
7878
def write(out: java.io.OutputStream): Unit = ()
79+
override def headers = Seq("Content-Length" -> "0")
7980
}
8081

8182
implicit class ByteSourceRequestBlob[T](x: T)(implicit f: T => geny.Writable) extends RequestBlob{
@@ -176,18 +177,21 @@ class ResponseBlob(val bytes: Array[Byte]){
176177

177178

178179
/**
179-
* Represents a HTTP response
180-
*
181-
* @param url the URL that the original request was made to
182-
* @param statusCode the status code of the response
183-
* @param statusMessage the status message of the response
184-
* @param headers the raw headers the server sent back with the response
185-
* @param data the response body; may contain HTML, JSON, or binary or textual data
186-
* @param history the response of any redirects that were performed before
187-
* arriving at the current response
188-
*/
180+
* Represents a HTTP response
181+
*
182+
* @param url the URL that the original request was made to
183+
* @param statusCode the status code of the response
184+
* @param statusMessage a string that describes the status code.
185+
* This is not the reason phrase sent by the server,
186+
* but a string describing [[statusCode]], as hardcoded in this library
187+
* @param headers the raw headers the server sent back with the response
188+
* @param data the response body; may contain HTML, JSON, or binary or textual data
189+
* @param history the response of any redirects that were performed before
190+
* arriving at the current response
191+
*/
189192
case class Response(url: String,
190193
statusCode: Int,
194+
@deprecated("Value is inferred from `statusCode`", "0.9.0")
191195
statusMessage: String,
192196
data: geny.Bytes,
193197
headers: Map[String, Seq[String]],
@@ -222,11 +226,12 @@ case class Response(url: String,
222226
f(new java.io.ByteArrayInputStream(data.array))
223227
}
224228
override def httpContentType: Option[String] = contentType
225-
override def contentLength: Option[Long] = Some(data.array.size)
229+
override def contentLength: Option[Long] = Some(data.array.length)
226230
}
227231

228232
case class StreamHeaders(url: String,
229233
statusCode: Int,
234+
@deprecated("Value is inferred from `statusCode`", "0.9.0")
230235
statusMessage: String,
231236
headers: Map[String, Seq[String]],
232237
history: Option[Response]){

0 commit comments

Comments
 (0)