-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[java_http] send request body #995
[java_http] send request body #995
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
httpUrlConnection.setDoOutput(true); | ||
final outputStream = httpUrlConnection.getOutputStream(); | ||
requestBody.forEach(outputStream.write); | ||
|
||
outputStream |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[optional] Is there any value in minimizing the uses of callMethodWithArgs
and avoiding interop calls in a loop?
(untested suggestion)
httpUrlConnection.setDoOutput(true); | |
final outputStream = httpUrlConnection.getOutputStream(); | |
requestBody.forEach(outputStream.write); | |
outputStream | |
httpUrlConnection.setDoOutput(true); | |
final outputStream = httpUrlConnection.getOutputStream(); | |
final bodyArray = JArray(jbyte.type, requestBody.length) | |
..setRange(0, requestBody.length, requestBody); | |
outputStream | |
..write1(bodyArray) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a good idea to me!
I've pushed a commit with the suggestions and the tests were all passing locally.
Thank you 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been learning about extension methods recently, could we add an extension method to Uint8List
?
extension on Uint8List {
JArray<jbyte> toJArray() =>
JArray(jbyte.type, length)..setRange(0, length, this);
}
Then, the code would go from:
final bodyArray = JArray(jbyte.type, requestBody.length)
..setRange(0, requestBody.length, requestBody);
httpUrlConnection.getOutputStream()
..write1(bodyArray)
..flush()
..close();
to:
httpUrlConnection.getOutputStream()
..write1(requestBody.toJArray())
..flush()
..close();
My intuition is that adding an extension method to Uint8List
is unnecessary at the moment. But if it becomes a common occurence to convert Uint8List
to JArray<jbyte>
in java_http
then it would be a nice idea to add an extension method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we add an extension method to
Uint8List
?
This extension method does look like a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Let's go for the extension method toJArray()
on Uint8List
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM as is, or with the discussed extension method toJArray()
.
Hey @natebosch, I've pushed a commit which adds the extension method I'm new to extension methods and I'm not sure where the best place to put the extension method is. But I'm open to ideas about where to put the extension method. Thank you! |
For something very short like this which is used in 1 place, I usually start with a private extension locally within the library. A "utils" or "extensions" directory or library like you chose here is also great. It's a pretty low-impact decision in this case which is why I approved without commenting. Since you're looking for input on best practices my recommendation would be a private local extension for this one. extension on Uint8List { For example: |
Hey @natebosch, Thanks for explaining when to use an unnamed extension and where to store named extensions, I'll definitely remember that for the future 🧠. I've pushed a commit to make the change to a private extension, I think it looks better. The example you linked was very helpful. I appreciate your help! |
This is a PR for the
java_http
GSoC '23 project 🌞.Relevant tracking issue: #957.
Purpose
The purpose of this PR is to make the
package:java_http
client send the request body to the server.We are now passing another two package:http_client_conformance_tests 🎉:
The testRequestBody() tests were initially failing because we can't send a StreamedRequest object between isolates.
Reviewers
@HosseinYousefi
@natebosch
Contribution guidelines:
dart format
.Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.