22
33import java .io .IOException ;
44import java .io .InputStreamReader ;
5+ import java .lang .reflect .ParameterizedType ;
6+ import java .lang .reflect .Type ;
57import java .net .HttpURLConnection ;
68import java .net .URL ;
79import java .util .Arrays ;
10+ import java .util .HashMap ;
811
912import javax .net .ssl .HttpsURLConnection ;
1013
1114import com .google .gson .FieldNamingPolicy ;
1215import com .google .gson .Gson ;
1316import com .google .gson .GsonBuilder ;
1417import com .google .gson .annotations .SerializedName ;
18+ import com .google .gson .reflect .TypeToken ;
1519
1620public 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 );
0 commit comments