Skip to content

Commit

Permalink
Use JDK 11 HttpClient (#158)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nafg authored Jun 16, 2024
1 parent 96e8a04 commit 1a911bb
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 262 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: ['8', '17']
java: ['11', '17']
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
Expand All @@ -34,7 +34,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 8
java-version: 11
- name: Check Binary Compatibility
run: ./mill -i __.mimaReportBinaryIssues

Expand All @@ -55,7 +55,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 8
java-version: 11
- name: Publish to Maven Central
run: |
if [[ $(git tag --points-at HEAD) != '' ]]; then
Expand Down
8 changes: 3 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -660,11 +660,9 @@ know.
As it turns out, Kenneth Reitz's Requests is
[not a lot of code](https://github.com/requests/requests/tree/main/requests).
Most of the heavy lifting is done in other libraries, and his library is a just
thin-shim that makes the API 10x better. It turns out on the JVM most of the
heavy lifting is also done for you, by `java.net.HttpUrlConnection` in the
simplest case, and other libraries like
[AsyncHttpClient](https://github.com/AsyncHttpClient/async-http-client) for more
advanced use cases.
thin-shim that makes the API 10x better. Similarly, it turns out on the JVM most of the
heavy lifting is also done for you. There have always been options, but
since JDK 11 a decent HTTP client is provided in the standard library.

Given that's the case, how hard can it be to port over a dozen Python files to
Scala? This library attempts to do that: class by class, method by method,
Expand Down
31 changes: 18 additions & 13 deletions requests/src/requests/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ case class Request(url: String,

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

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


/**
* Represents a HTTP response
*
* @param url the URL that the original request was made to
* @param statusCode the status code of the response
* @param statusMessage the status message of the response
* @param headers the raw headers the server sent back with the response
* @param data the response body; may contain HTML, JSON, or binary or textual data
* @param history the response of any redirects that were performed before
* arriving at the current response
*/
* Represents a HTTP response
*
* @param url the URL that the original request was made to
* @param statusCode the status code of the response
* @param statusMessage a string that describes the status code.
* This is not the reason phrase sent by the server,
* but a string describing [[statusCode]], as hardcoded in this library
* @param headers the raw headers the server sent back with the response
* @param data the response body; may contain HTML, JSON, or binary or textual data
* @param history the response of any redirects that were performed before
* arriving at the current response
*/
case class Response(url: String,
statusCode: Int,
@deprecated("Value is inferred from `statusCode`", "0.9.0")
statusMessage: String,
data: geny.Bytes,
headers: Map[String, Seq[String]],
Expand Down Expand Up @@ -222,11 +226,12 @@ case class Response(url: String,
f(new java.io.ByteArrayInputStream(data.array))
}
override def httpContentType: Option[String] = contentType
override def contentLength: Option[Long] = Some(data.array.size)
override def contentLength: Option[Long] = Some(data.array.length)
}

case class StreamHeaders(url: String,
statusCode: Int,
@deprecated("Value is inferred from `statusCode`", "0.9.0")
statusMessage: String,
headers: Map[String, Seq[String]],
history: Option[Response]){
Expand Down
Loading

0 comments on commit 1a911bb

Please sign in to comment.