Skip to content

Commit

Permalink
Injection changes for API (abex)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyborger1 committed Jan 13, 2022
1 parent 00ecab2 commit 4970a15
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions src/main/java/com/botdetector/http/BotDetectorClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -62,7 +61,6 @@
import lombok.Setter;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import net.runelite.client.RuneLite;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
Expand Down Expand Up @@ -99,22 +97,10 @@ private enum ApiPath
final String path;
}

public static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addNetworkInterceptor(chain ->
{
Request headerRequest = chain.request()
.newBuilder()
.header("User-Agent", RuneLite.USER_AGENT)
.header("Request-Epoch", CURRENT_EPOCH_SUPPLIER.get())
.build();
return chain.proceed(headerRequest);
})
.build();
public OkHttpClient okHttpClient;

@Inject
private GsonBuilder gsonBuilder;
private Gson gson;

@Getter
@Setter
Expand All @@ -136,6 +122,24 @@ private HttpUrl getUrl(ApiPath path)
.build();
}

@Inject
public BotDetectorClient(OkHttpClient rlClient)
{
okHttpClient = rlClient.newBuilder()
.pingInterval(0, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addNetworkInterceptor(chain ->
{
Request headerRequest = chain.request()
.newBuilder()
.header("Request-Epoch", CURRENT_EPOCH_SUPPLIER.get())
.build();
return chain.proceed(headerRequest);
})
.build();
}

/**
* Sends a single {@link PlayerSighting} to the API to be persisted in the Bot Detector database.
* @param sighting The sighting to send.
Expand All @@ -160,7 +164,7 @@ public CompletableFuture<Boolean> sendSightings(Collection<PlayerSighting> sight
List<PlayerSightingWrapper> wrappedList = sightings.stream()
.map(p -> new PlayerSightingWrapper(uploaderName, p)).collect(Collectors.toList());

Gson gson = gsonBuilder
Gson bdGson = gson.newBuilder()
.registerTypeAdapter(PlayerSightingWrapper.class, new PlayerSightingWrapperSerializer())
.registerTypeAdapter(Boolean.class, new BooleanToZeroOneSerializer())
.registerTypeAdapter(Instant.class, new InstantSecondsConverter())
Expand All @@ -170,7 +174,7 @@ public CompletableFuture<Boolean> sendSightings(Collection<PlayerSighting> sight
.url(getUrl(ApiPath.DETECTION).newBuilder()
.addPathSegment(String.valueOf(manual ? 1 : 0))
.build())
.post(RequestBody.create(JSON, gson.toJson(wrappedList)))
.post(RequestBody.create(JSON, bdGson.toJson(wrappedList)))
.build();

CompletableFuture<Boolean> future = new CompletableFuture<>();
Expand Down Expand Up @@ -219,8 +223,6 @@ public void onResponse(Call call, Response response)
*/
public CompletableFuture<Boolean> verifyDiscord(String token, String nameToVerify, String code)
{
Gson gson = gsonBuilder.create();

Request request = new Request.Builder()
.url(getUrl(ApiPath.VERIFY_DISCORD).newBuilder()
.addPathSegment(token)
Expand Down Expand Up @@ -283,8 +285,6 @@ public void onResponse(Call call, Response response)
*/
public CompletableFuture<Boolean> sendFeedback(Prediction pred, String uploaderName, FeedbackPredictionLabel proposedLabel, String feedbackText)
{
Gson gson = gsonBuilder.create();

Request request = new Request.Builder()
.url(getUrl(ApiPath.FEEDBACK))
.post(RequestBody.create(JSON, gson.toJson(new PredictionFeedback(
Expand Down Expand Up @@ -342,8 +342,6 @@ public void onResponse(Call call, Response response)
*/
public CompletableFuture<Prediction> requestPrediction(String playerName)
{
Gson gson = gsonBuilder.create();

Request request = new Request.Builder()
.url(getUrl(ApiPath.PREDICTION).newBuilder()
.addPathSegment(playerName)
Expand Down Expand Up @@ -389,8 +387,6 @@ public void onResponse(Call call, Response response)
*/
public CompletableFuture<Map<PlayerStatsType, PlayerStats>> requestPlayerStats(String playerName)
{
Gson gson = gsonBuilder.create();

Request request = new Request.Builder()
.url(getUrl(ApiPath.PLAYER_STATS).newBuilder()
.addPathSegment(playerName)
Expand Down Expand Up @@ -475,7 +471,7 @@ private IOException getIOException(Response response)
{
try
{
Map<String, String> map = gsonBuilder.create().fromJson(response.body().string(),
Map<String, String> map = gson.fromJson(response.body().string(),
new TypeToken<Map<String, String>>()
{
}.getType());
Expand Down

0 comments on commit 4970a15

Please sign in to comment.