Skip to content

Commit

Permalink
feat: enhanced logging on OkHttpUtil methods (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboxer23 committed Mar 22, 2024
1 parent b4c571c commit eedf3eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.bigboxer23</groupId>
<artifactId>utils</artifactId>
<version>2.0.23</version>
<version>2.0.24</version>

<name>utils</name>
<!-- FIXME change it to the project's website -->
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/bigboxer23/utils/http/OkHttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** */
public class OkHttpUtil {
private static final Logger logger = LoggerFactory.getLogger(OkHttpUtil.class);

private static final OkHttpClient defaultClient = getBuilder().build();

private static Moshi moshi;
Expand Down Expand Up @@ -140,6 +144,9 @@ private static Request.Builder runBuilderCallback(Request.Builder builder, Reque

public static Optional<String> getBody(Response response) {
ResponseBody body = response.body();
if (!response.isSuccessful()) {
logger.error("request not successful: " + response.code());
}
if (body == null) {
return Optional.empty();
}
Expand All @@ -153,11 +160,16 @@ public static Optional<String> getBody(Response response) {
public static <T> Optional<T> getBody(Response response, Class<T> clazz) {
Optional<String> body = getBody(response);
if (!body.isPresent()) {
logger.error("empty body");
return Optional.empty();
}
if (!response.isSuccessful()) {
logger.error("request not successful body: " + body.get());
}
try {
return Optional.ofNullable(getMoshi().adapter(clazz).fromJson(body.get()));
} catch (IOException theE) {
} catch (IOException e) {
logger.error("getBody: ", e);
return Optional.empty();
}
}
Expand Down

0 comments on commit eedf3eb

Please sign in to comment.