Skip to content

[http-client] Fix Non-Json bodytypes/Add InputStream body method #200

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

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ public HttpClientRequest body(Supplier<? extends InputStream> streamSupplier) {
return this;
}

@Override
public HttpClientRequest body(InputStream stream) {
this.body = HttpRequest.BodyPublishers.ofInputStream(() -> stream);
return this;
}

@Override
public HttpClientRequest body(Path file) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ default HttpClientRequest queryParam(String name, Collection<String> values) {
*/
HttpClientRequest body(Supplier<? extends InputStream> supplier);

/**
* Set the body content with supplied InputStream.
*
* @param supplier The InputStream content to send as body content
* @return The request being built
*/
HttpClientRequest body(InputStream stream);

/**
* Set the body content with supplied InputStream.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ private void methodStart(Append writer) {
if (count++ > 0) {
writer.append(", ");
}
writer.append(param.utype().shortType()).append(" ");
var paramType =
"java.util.function.Supplier<?extendsjava.io.InputStream>".equals(param.utype().full())
? "Supplier<? extends InputStream>"
: param.utype().shortType();
writer.append(paramType).append(" ");
writer.append(param.name());
}
writer.append(") {").eol();
Expand Down Expand Up @@ -250,10 +254,29 @@ private void writeFormParam(MethodParam param, ParamType paramType) {
private void writeBody() {
for (MethodParam param : method.params()) {
ParamType paramType = param.paramType();

if (paramType == ParamType.BODY) {
writer.append(" .body(%s, ", param.name());
writeGeneric(param.utype());
writer.append(")").eol();

var type = param.utype().full();
if ("java.net.http.HttpRequest.BodyPublisher".equals(type)
|| "java.lang.String".equals(type)
|| "byte[]".equals(type)
|| "java.io.InputStream".equals(type)
|| "java.util.function.Supplier<?extendsjava.io.InputStream>".equals(type)
|| "java.util.function.Supplier<java.io.InputStream>".equals(type)
|| "java.nio.file.Path".equals(type)
|| "io.avaje.http.client.BodyContent".equals(type)) {

writer.append(" .body(%s)", param.name()).eol();

} else {

writer.append(" .body(%s, ", param.name());
writeGeneric(param.utype());
writer.append(")").eol();
}

return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,13 @@ public Set<String> importTypes() {
for (String type : allTypes) {
if (!type.startsWith("java.lang.") && type.indexOf('.') > -1) {
if (type.startsWith("java")) {
set.add(type.replace("[]", ""));
set.add(type.replace("[]", "").replace("?extends", ""));
} else {
set.add(innerTypesImport(type).replace("[]", ""));
set.add(innerTypesImport(type).replace("[]", "").replace("?extends", ""));
}
}
}
set.remove("?");
return set;
}

Expand Down