@@ -20,66 +20,110 @@ final class AgonesSDKImpl implements AgonesSDK {
2020 private static final String DEFAULT_URL = "http://localhost:9358/" ;
2121 private static final Gson GSON = new GsonBuilder ().create ();
2222
23- private final String url = getUrl () ;
24- private final OkHttpClient client = new OkHttpClient () ;
23+ private final String url ;
24+ private final OkHttpClient client ;
2525
26- private final Request allocateRequest = new Request .Builder ()
27- .url (url + "allocate" )
28- .addHeader ("Content-Type" , "application/json" )
29- .post (RequestBody .create ("{}" , MediaType .get ("application/json" )))
30- .build ();
26+ AgonesSDKImpl () {
27+ this (getUrl ());
28+ }
29+
30+ AgonesSDKImpl (@ NonNull String url ) {
31+ this .url = url .endsWith ("/" ) ? url : url + "/" ;
32+ this .client = new OkHttpClient ();
33+ }
3134
3235 @ Override
3336 public void allocate () throws IOException {
34- try (Response response = client .newCall (allocateRequest ).execute ()) {
37+ Request request = new Request .Builder ()
38+ .url (url + "allocate" )
39+ .addHeader ("Content-Type" , "application/json" )
40+ .post (RequestBody .create ("{}" , MediaType .get ("application/json" )))
41+ .build ();
42+
43+ try (Response response = client .newCall (request ).execute ()) {
3544 if (response .isSuccessful ()) return ;
3645 int code = response .code ();
3746 String message = response .message ();
3847 throw new IOException ("Allocate request failed: " + code + " - " + message );
3948 }
4049 }
4150
42- private final Request healthRequest = new Request .Builder ()
43- .url (url + "health" )
44- .addHeader ("Content-Type" , "application/json" )
45- .post (RequestBody .create ("{}" , MediaType .get ("application/json" )))
46- .build ();
51+ @ Override
52+ public void deallocate () throws IOException {
53+ Request request = readyRequest ();
54+ try (Response response = client .newCall (request ).execute ()) {
55+ if (response .isSuccessful ()) return ;
56+ int code = response .code ();
57+ String message = response .message ();
58+ throw new IOException ("Deallocate request failed: " + code + " - " + message );
59+ }
60+ }
61+
62+ @ Override
63+ public GameServer gameServer () throws IOException {
64+ Request request = new Request .Builder ()
65+ .url (url + "gameserver" )
66+ .addHeader ("Content-Type" , "application/json" )
67+ .get ()
68+ .build ();
69+
70+ try (Response response = client .newCall (request ).execute ()) {
71+ if (response .isSuccessful ()) {
72+ if (response .body () == null ) {
73+ throw new IOException ("GameServer request failed: empty response body" );
74+ }
75+ return GSON .fromJson (response .body ().string (), GameServer .class );
76+ }
77+ int code = response .code ();
78+ String message = response .message ();
79+ throw new IOException ("GameServer request failed: " + code + " - " + message );
80+ }
81+ }
4782
4883 @ Override
4984 public void health () throws IOException {
50- try (Response response = client .newCall (healthRequest ).execute ()) {
85+ Request request = new Request .Builder ()
86+ .url (url + "health" )
87+ .addHeader ("Content-Type" , "application/json" )
88+ .post (RequestBody .create ("{}" , MediaType .get ("application/json" )))
89+ .build ();
90+
91+ try (Response response = client .newCall (request ).execute ()) {
5192 if (response .isSuccessful ()) return ;
5293 int code = response .code ();
5394 String message = response .message ();
5495 throw new IOException ("Health request failed: " + code + " - " + message );
5596 }
5697 }
5798
58- private final Request readyRequest = new Request .Builder ()
59- .url (url + "ready" )
60- .addHeader ("Content-Type" , "application/json" )
61- .post (RequestBody .create ("{}" , MediaType .get ("application/json" )))
62- .build ();
63-
6499 @ Override
65100 public void ready () throws IOException {
66- try (Response response = client .newCall (readyRequest ).execute ()) {
101+ Request request = readyRequest ();
102+ try (Response response = client .newCall (request ).execute ()) {
67103 if (response .isSuccessful ()) return ;
68104 int code = response .code ();
69105 String message = response .message ();
70106 throw new IOException ("Ready request failed: " + code + " - " + message );
71107 }
72108 }
73109
74- private final Request shutdownRequest = new Request .Builder ()
75- .url (url + "shutdown" )
76- .addHeader ("Content-Type" , "application/json" )
77- .post (RequestBody .create ("{}" , MediaType .get ("application/json" )))
78- .build ();
110+ private Request readyRequest () {
111+ return new Request .Builder ()
112+ .url (url + "ready" )
113+ .addHeader ("Content-Type" , "application/json" )
114+ .post (RequestBody .create ("{}" , MediaType .get ("application/json" )))
115+ .build ();
116+ }
79117
80118 @ Override
81119 public void shutdown () throws IOException {
82- try (Response response = client .newCall (shutdownRequest ).execute ()) {
120+ Request request = new Request .Builder ()
121+ .url (url + "shutdown" )
122+ .addHeader ("Content-Type" , "application/json" )
123+ .post (RequestBody .create ("{}" , MediaType .get ("application/json" )))
124+ .build ();
125+
126+ try (Response response = client .newCall (request ).execute ()) {
83127 if (response .isSuccessful ()) return ;
84128 int code = response .code ();
85129 String message = response .message ();
@@ -238,7 +282,7 @@ private Counter getCounter(@NonNull String key) throws IOException {
238282 };
239283 }
240284
241- private String getUrl () {
285+ private static String getUrl () {
242286 String port = System .getenv ("AGONES_SDK_HTTP_PORT" );
243287 if (port == null || port .isEmpty ()) return DEFAULT_URL ;
244288 return "http://localhost:" + port + "/" ;
0 commit comments