Skip to content

Commit

Permalink
feat: add one additional response body util method (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboxer23 committed Mar 12, 2024
1 parent 32d6b02 commit 92bda1a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
21 changes: 18 additions & 3 deletions 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.21</version>
<version>2.0.22</version>

<name>utils</name>
<!-- FIXME change it to the project's website -->
Expand Down Expand Up @@ -39,6 +39,11 @@
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.15.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -99,11 +104,13 @@
<configuration>
<java>
<googleJavaFormat>
<version>1.15.0</version>
<version>1.17.0</version>
<style>AOSP</style>
<reflowLongStrings>true</reflowLongStrings>
</googleJavaFormat>
<palantirJavaFormat/>
<palantirJavaFormat>
<version>2.35.0</version>
</palantirJavaFormat>
<indent>
<tabs>true</tabs>
<spacesPerTab>4</spacesPerTab>
Expand Down Expand Up @@ -135,6 +142,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
26 changes: 24 additions & 2 deletions src/main/java/com/bigboxer23/utils/http/OkHttpUtil.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.bigboxer23.utils.http;

import com.squareup.moshi.Moshi;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import okhttp3.*;

/** */
public class OkHttpUtil {
private static OkHttpClient defaultClient = getBuilder().build();
private static final OkHttpClient defaultClient = getBuilder().build();

private static Moshi moshi;

public static OkHttpClient.Builder getBuilder() {
return new OkHttpClient.Builder()
Expand Down Expand Up @@ -135,7 +138,7 @@ private static Request.Builder runBuilderCallback(Request.Builder builder, Reque
return Optional.ofNullable(builderCallback).orElse(builder1 -> builder1).modifyBuilder(builder);
}

public Optional<String> getBody(Response response) {
public static Optional<String> getBody(Response response) {
ResponseBody body = response.body();
if (body == null) {
return Optional.empty();
Expand All @@ -146,4 +149,23 @@ public Optional<String> getBody(Response response) {
return Optional.empty();
}
}

public static <T> Optional<T> getBody(Response response, Class<T> clazz) {
Optional<String> body = getBody(response);
if (!body.isPresent()) {
return Optional.empty();
}
try {
return Optional.ofNullable(getMoshi().adapter(clazz).fromJson(body.get()));
} catch (IOException theE) {
return Optional.empty();
}
}

private static Moshi getMoshi() {
if (moshi == null) {
moshi = new Moshi.Builder().build();
}
return moshi;
}
}

0 comments on commit 92bda1a

Please sign in to comment.