Skip to content

Commit

Permalink
Improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlauer committed Dec 15, 2024
1 parent 21d845b commit 0c62153
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
58 changes: 54 additions & 4 deletions blaze-http/src/main/java/com/fizzed/blaze/http/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.atomic.AtomicBoolean;

public class Http extends Action<Http.Result,Void> implements VerbosityMixin<Http> {

Expand All @@ -26,6 +32,7 @@ static public class Result extends com.fizzed.blaze.core.Result<Http,Void,Result
private final Request.Builder requestBuilder;
private String method;
private FormBody.Builder formBuilder;
private Path target;

public Http(Context context) {
super(context);
Expand Down Expand Up @@ -58,6 +65,19 @@ public Http form(String param, String value) {
return this;
}

public Http target(Path target) {
this.target = target;
return this;
}

public Http target(File target) {
return this.target(target.toPath());
}

public Http target(String target) {
return this.target(Paths.get(target));
}

@Override
protected Result doRun() throws BlazeException {
if (this.method == null) {
Expand Down Expand Up @@ -86,21 +106,51 @@ protected Result doRun() throws BlazeException {

final Request request = this.requestBuilder.build();

final long startTime = System.currentTimeMillis();
final AtomicBoolean isFirstRequest = new AtomicBoolean(true);
final OkHttpClient client = this.clientBuilder
.addInterceptor(new Interceptor() {
.addNetworkInterceptor(new Interceptor() {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request r = chain.request();
log.verbose("Http {} {}", r.method(), r.url());
return chain.proceed(r);
final Request _request = chain.request();
if (isFirstRequest.get()) {
log.info("Http request method={} url={}", request.method(), request.url());
if (log.isDebug()) {
_request.headers().forEach(h -> {
log.debug("{}: {}", h.getFirst(), h.getSecond());
});
}
}
isFirstRequest.set(false);
final Response response = chain.proceed(_request);
if (!response.isRedirect()) {
log.info("Http response method={}, url={}, code={}, protocol={} (in {} ms)",
request.method(), request.url(), response.code(), response.protocol(), (System.currentTimeMillis() - startTime));
if (log.isDebug()) {
response.headers().forEach(h -> {
log.debug("{}: {}", h.getFirst(), h.getSecond());
});
}
}
return response;
}
})
.build();

try (Response response = client.newCall(request).execute()) {
Result result = new Result(this, null);

// what to do with the response?
if (this.target != null) {
final ResponseBody responseBody = response.body();
if (responseBody != null) {
try (InputStream is = responseBody.byteStream()) {
Files.copy(is, target, StandardCopyOption.REPLACE_EXISTING);
}
}
}

return result;
} catch (IOException e) {
throw new BlazeException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void get() {
@Test
public void postForm() {
new Http(context).post("http://example.com")
.verbose()
//.verbose()
.form("a", "b")
.run();
}
Expand Down

0 comments on commit 0c62153

Please sign in to comment.