forked from Spireblight/STR-Spire-Mod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ebs client to broadcast messages
- Loading branch information
Showing
4 changed files
with
33 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,61 @@ | ||
package str_exporter.client; | ||
|
||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.stream.JsonReader; | ||
import str_exporter.config.Config; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.lang.reflect.Type; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
public class EBSClient { | ||
public static final AtomicLong lastSuccessAuth = new AtomicLong(0); | ||
public static final AtomicLong lastSuccessRequest = new AtomicLong(0); | ||
private final Config config; | ||
|
||
public EBSClient(Config config) { | ||
this.config = config; | ||
} | ||
|
||
public User verifyCredentials(String code) throws IOException { | ||
Gson gson = new Gson(); | ||
Map<String, String> body = new HashMap<>(); | ||
body.put("code", code); | ||
return doRequest("POST", "/api/v1/auth", config.gson.toJson(body), User.class); | ||
} | ||
|
||
public void broadcastMessage(String message) throws IOException { | ||
doRequest("POST", "/api/v1/message", message, String.class); | ||
} | ||
|
||
URL url = new URL(config.getApiUrl() + "/api/v1/auth"); | ||
private <T> T doRequest(String method, String path, String body, Type outputType) throws IOException { | ||
URL url = new URL(config.getApiUrl() + path); | ||
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | ||
con.setRequestMethod("POST"); | ||
con.setRequestMethod(method); | ||
con.setRequestProperty("Content-Type", "application/json"); | ||
con.setRequestProperty("Accept", "application/json"); | ||
con.setDoOutput(true); | ||
|
||
OutputStream os = con.getOutputStream(); | ||
String msg = "{\"code\":\"" + code + "\"}"; | ||
byte[] input = msg.getBytes(StandardCharsets.UTF_8); | ||
os.write(input, 0, input.length); | ||
if (body != null && !body.isEmpty()) { | ||
byte[] input = body.getBytes(StandardCharsets.UTF_8); | ||
os.write(input, 0, input.length); | ||
} | ||
|
||
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), | ||
StandardCharsets.UTF_8))) { | ||
if (con.getResponseCode() == 200) { | ||
if (con.getResponseCode() >= 200 && con.getResponseCode() < 300) { | ||
JsonReader reader = new JsonReader(br); | ||
EBSClient.lastSuccessAuth.set(System.currentTimeMillis()); | ||
return gson.fromJson(reader, User.class); | ||
EBSClient.lastSuccessRequest.set(System.currentTimeMillis()); | ||
return config.gson.fromJson(reader, outputType); | ||
} | ||
throw new IOException("Failed : HTTP error code : " + con.getResponseCode()); | ||
throw new IOException(method + " " + path + " failed: HTTP error code: " + con.getResponseCode()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters