1111import com .google .gson .FieldNamingPolicy ;
1212import com .google .gson .Gson ;
1313import com .google .gson .GsonBuilder ;
14+ import com .google .gson .annotations .SerializedName ;
1415
1516public class Api {
1617 private String url ;
@@ -21,22 +22,121 @@ public class Api {
2122 .create ();
2223
2324 public Api (String url ) {
25+ HttpURLConnection .setFollowRedirects (true );
2426 this .url = trimURL (url );
2527 this .tls = isTLS (this .url );
2628 this .baseURL = Api .baseURL ("http" , this .tls , this .url );
27- HttpURLConnection .setFollowRedirects (true );
29+ }
30+
31+ public class GameInfo {
32+ @ SerializedName ("name" )
33+ public String name ;
34+ @ SerializedName ("cg_version" )
35+ public String cgVersion ;
36+ @ SerializedName ("display_name" )
37+ public String displayName ;
38+ @ SerializedName ("description" )
39+ public String description ;
40+ @ SerializedName ("version" )
41+ public String version ;
42+ @ SerializedName ("repository_url" )
43+ public String repositoryURL ;
2844 }
2945
3046 public GameInfo fetchInfo () throws IOException {
31- URL obj = new URL (this .baseURL + "/api/info" );
47+ return fetchJSON ("/api/info" , GameInfo .class );
48+ }
49+
50+ public class GameData {
51+ @ SerializedName ("game_id" )
52+ public String Id ;
53+ @ SerializedName ("join_secret" )
54+ public String joinSecret ;
55+ }
56+
57+ public GameData createGame (boolean makePublic , boolean protect ) throws IOException {
58+ return createGame (makePublic , protect , null );
59+ }
60+
61+ private class CreateGameRequest {
62+ @ SerializedName ("public" )
63+ public boolean makePublic ;
64+ @ SerializedName ("protected" )
65+ public boolean protect ;
66+ @ SerializedName ("config" )
67+ public Object config ;
68+ }
69+
70+ public GameData createGame (boolean makePublic , boolean protect , Object config ) throws IOException {
71+ var data = new CreateGameRequest ();
72+ data .makePublic = makePublic ;
73+ data .protect = protect ;
74+ data .config = config ;
75+ return postJSON ("/api/games" , data , GameData .class );
76+ }
77+
78+ public class PlayerData {
79+ @ SerializedName ("player_id" )
80+ public String playerId ;
81+ @ SerializedName ("player_secret" )
82+ public String playerSecret ;
83+ }
84+
85+ public PlayerData createPlayer (String gameId , String username ) throws IOException {
86+ return createPlayer (gameId , username , "" );
87+ }
88+
89+ private class CreatePlayerRequest {
90+ @ SerializedName ("username" )
91+ public String username ;
92+ @ SerializedName ("join_secret" )
93+ public String joinSecret ;
94+ }
95+
96+ public PlayerData createPlayer (String gameId , String username , String joinSecret ) throws IOException {
97+ var data = new CreatePlayerRequest ();
98+ data .username = username ;
99+ data .joinSecret = joinSecret ;
100+ return postJSON ("/api/games/" + gameId + "/players" , data , PlayerData .class );
101+ }
102+
103+ private <T > T postJSON (String endpoint , Object requestData , Class <T > responseType ) throws IOException {
104+ URL obj = new URL (this .baseURL + endpoint );
32105 HttpURLConnection con = (HttpURLConnection ) obj .openConnection ();
33- con .setRequestProperty ("Accept" , "application/json" );
106+ con .setRequestMethod ("POST" );
107+ con .setRequestProperty ("Content-Type" , "application/json" );
108+
109+ var reqData = json .toJson (requestData );
110+ System .out .println ("Data: " + reqData );
111+ con .setDoOutput (true );
112+ var os = con .getOutputStream ();
113+ os .write (reqData .getBytes ());
114+ os .flush ();
115+ os .close ();
116+
117+ int responseCode = con .getResponseCode ();
118+ if (responseCode != HttpURLConnection .HTTP_OK && responseCode != HttpURLConnection .HTTP_CREATED ) {
119+ throw new IOException ("Failed to read response from " + endpoint + " endpoint: unexpected response code: "
120+ + responseCode );
121+ }
122+
34123 InputStreamReader reader = new InputStreamReader (con .getInputStream ());
124+ T data = json .fromJson (reader , responseType );
125+ return data ;
126+ }
127+
128+ private <T > T fetchJSON (String endpoint , Class <T > responseType ) throws IOException {
129+ var obj = new URL (this .baseURL + endpoint );
130+ var con = (HttpURLConnection ) obj .openConnection ();
131+ con .setRequestMethod ("GET" );
132+ con .setRequestProperty ("Accept" , "application/json" );
133+ var reader = new InputStreamReader (con .getInputStream ());
35134 int responseCode = con .getResponseCode ();
36- if (responseCode != 200 )
37- throw new IOException ("Failed to fetch game info: unexpected response code: " + responseCode );
38- GameInfo info = json .fromJson (reader , GameInfo .class );
39- return info ;
135+ if (responseCode != HttpURLConnection .HTTP_OK )
136+ throw new IOException ("Failed to read response from " + endpoint + " endpoint: unexpected response code: "
137+ + responseCode );
138+ T data = json .fromJson (reader , responseType );
139+ return data ;
40140 }
41141
42142 static String trimURL (String url ) {
@@ -57,8 +157,8 @@ static String baseURL(String protocol, boolean tls, String trimmedURL) {
57157
58158 static boolean isTLS (String trimmedURL ) {
59159 try {
60- URL url = new URL (baseURL ("http" , true , trimmedURL ) + "/api/info" );
61- HttpsURLConnection connection = (HttpsURLConnection ) url .openConnection ();
160+ var url = new URL (baseURL ("http" , true , trimmedURL ) + "/api/info" );
161+ var connection = (HttpsURLConnection ) url .openConnection ();
62162 connection .getInputStream ();
63163 if (connection .getSSLSession ().isEmpty ())
64164 return false ;
0 commit comments