Skip to content

Commit

Permalink
Refactor xp updater requests to use generic request sender method
Browse files Browse the repository at this point in the history
  • Loading branch information
psikoi authored and Adam- committed Apr 30, 2020
1 parent f60d818 commit 1778d5d
Showing 1 changed file with 23 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2018, Adam <[email protected]>
* Copyright (c) 2020, Alexsuperfly <[email protected]>
* Copyright (c) 2020, Psikoi <https://github.com/psikoi>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -43,7 +44,6 @@
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

Expand Down Expand Up @@ -108,7 +108,7 @@ else if (state == GameState.LOGIN_SCREEN)
if (Math.abs(totalXp - lastXp) > XP_THRESHOLD)
{
log.debug("Submitting update for {}", local.getName());
sendUpdateRequest(local.getName());
update(local.getName());
lastXp = totalXp;
}
}
Expand All @@ -124,10 +124,9 @@ public void onGameTick(GameTick gameTick)
}
}

private void sendUpdateRequest(String username)
private void update(String username)
{
String reformedUsername = username.replace(" ", "_");
OkHttpClient httpClient = RuneLiteAPI.CLIENT;

if (config.cml())
{
Expand All @@ -145,20 +144,7 @@ private void sendUpdateRequest(String username)
.url(url)
.build();

httpClient.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(Call call, IOException e)
{
log.warn("Error submitting CML update, caused by {}.", e.getMessage());
}

@Override
public void onResponse(Call call, Response response)
{
response.close();
}
});
sendRequest("CrystalMathLabs", request);
}

if (config.templeosrs())
Expand All @@ -176,20 +162,25 @@ public void onResponse(Call call, Response response)
.url(url)
.build();

httpClient.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(Call call, IOException e)
{
log.warn("Error submitting TempleOSRS update, caused by {}.", e.getMessage());
}

@Override
public void onResponse(Call call, Response response)
{
response.close();
}
});
sendRequest("TempleOSRS", request);
}
}

private static void sendRequest(String platform, Request request)
{
RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(Call call, IOException e)
{
log.warn("Error submitting {} update, caused by {}.", platform, e.getMessage());
}

@Override
public void onResponse(Call call, Response response)
{
response.close();
}
});
}
}

0 comments on commit 1778d5d

Please sign in to comment.