Skip to content

Commit 45c11f2

Browse files
committed
Implement missing API methods
1 parent 3596976 commit 45c11f2

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/main/java/org/codegame/client/Api.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
import java.io.IOException;
44
import java.io.InputStreamReader;
5+
import java.lang.reflect.ParameterizedType;
6+
import java.lang.reflect.Type;
57
import java.net.HttpURLConnection;
68
import java.net.URL;
79
import java.util.Arrays;
10+
import java.util.HashMap;
811

912
import javax.net.ssl.HttpsURLConnection;
1013

1114
import com.google.gson.FieldNamingPolicy;
1215
import com.google.gson.Gson;
1316
import com.google.gson.GsonBuilder;
1417
import com.google.gson.annotations.SerializedName;
18+
import com.google.gson.reflect.TypeToken;
1519

1620
public class Api {
1721
private String url;
@@ -47,9 +51,16 @@ public GameInfo fetchInfo() throws IOException {
4751
return fetchJSON("/api/info", GameInfo.class);
4852
}
4953

54+
public <T> T fetchGameConfig(String gameId, Class<T> configClass) throws IOException {
55+
GameConfigResponse<T> response = fetchJSON("/api/games/" + gameId,
56+
TypeToken.getParameterized(GameConfigResponse.class,
57+
configClass).getType());
58+
return response.config;
59+
}
60+
5061
public class GameData {
5162
@SerializedName("game_id")
52-
public String Id;
63+
public String id;
5364
@SerializedName("join_secret")
5465
public String joinSecret;
5566
}
@@ -77,9 +88,9 @@ public GameData createGame(boolean makePublic, boolean protect, Object config) t
7788

7889
public class PlayerData {
7990
@SerializedName("player_id")
80-
public String playerId;
91+
public String id;
8192
@SerializedName("player_secret")
82-
public String playerSecret;
93+
public String secret;
8394
}
8495

8596
public PlayerData createPlayer(String gameId, String username) throws IOException {
@@ -100,6 +111,20 @@ public PlayerData createPlayer(String gameId, String username, String joinSecret
100111
return postJSON("/api/games/" + gameId + "/players", data, PlayerData.class);
101112
}
102113

114+
private class FetchUsernameResponse {
115+
@SerializedName("username")
116+
public String username;
117+
}
118+
119+
public String fetchUsername(String gameId, String playerId) throws IOException {
120+
return fetchJSON("/api/games/" + gameId + "/players/" + playerId, FetchUsernameResponse.class).username;
121+
}
122+
123+
public HashMap<String, String> fetchPlayers(String gameId) throws IOException {
124+
return fetchJSON("/api/games/" + gameId + "/players",
125+
TypeToken.getParameterized(HashMap.class, String.class, String.class).getType());
126+
}
127+
103128
private <T> T postJSON(String endpoint, Object requestData, Class<T> responseType) throws IOException {
104129
URL obj = new URL(this.baseURL + endpoint);
105130
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
@@ -139,6 +164,20 @@ private <T> T fetchJSON(String endpoint, Class<T> responseType) throws IOExcepti
139164
return data;
140165
}
141166

167+
private <T> T fetchJSON(String endpoint, Type responseType) throws IOException {
168+
var obj = new URL(this.baseURL + endpoint);
169+
var con = (HttpURLConnection) obj.openConnection();
170+
con.setRequestMethod("GET");
171+
con.setRequestProperty("Accept", "application/json");
172+
var reader = new InputStreamReader(con.getInputStream());
173+
int responseCode = con.getResponseCode();
174+
if (responseCode != HttpURLConnection.HTTP_OK)
175+
throw new IOException("Failed to read response from " + endpoint + " endpoint: unexpected response code: "
176+
+ responseCode);
177+
T data = json.fromJson(reader, responseType);
178+
return data;
179+
}
180+
142181
static String trimURL(String url) {
143182
if (url.endsWith("/"))
144183
url = url.substring(0, url.length() - 1);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.codegame.client;
2+
3+
public class GameConfigResponse<T> {
4+
public T config;
5+
}

0 commit comments

Comments
 (0)