-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWatchLiveGame.java
More file actions
70 lines (56 loc) · 2.28 KB
/
WatchLiveGame.java
File metadata and controls
70 lines (56 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import callconfig.CallConfig;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import ui.GeneralGameInfo;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import utils.Utils;
import java.io.IOException;
public class WatchLiveGame {
public static JsonArray displayActiveGames(){
String RESOURCE = "/live-games";
Request request = new Request.Builder()
.url(CallConfig.BASE_URL + RESOURCE)
.build();
try (Response response = CallConfig.HTTP_CLIENT.newCall(request).execute()){
String responseBody = response.body().string();
if(response.isSuccessful()){
JsonArray activeGamesJson = JsonParser.parseString(responseBody).getAsJsonArray();
return GeneralGameInfo.printAllActiveGames(activeGamesJson);
}
else{
System.out.println("Request Failed: Code " + response.code());
System.out.println("Error messege: " + responseBody);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
public static boolean displayLiveGameByChoice(int adminChoice){
String RESOURCE = "/live-games";
HttpUrl.Builder urlBuilder = HttpUrl.parse(CallConfig.BASE_URL + RESOURCE).newBuilder();
urlBuilder.addQueryParameter("Live game choice", Integer.toString(adminChoice));
String finalUrl = urlBuilder.build().toString();
Request request = new Request.Builder()
.url(finalUrl)
.build();
try (Response response = CallConfig.HTTP_CLIENT.newCall(request).execute()){
String responseBody = response.body().string();
if(response.isSuccessful()){
JsonObject liveGame = JsonParser.parseString(responseBody).getAsJsonObject();
GeneralGameInfo.printActiveGameInfo(liveGame);
}
else{
//System.out.println("Request Failed: Code " + response.code());
System.out.println(responseBody);
return false;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
}