water.bindings.proxies.retrofit.Example Maven / Gradle / Ivy
package water.bindings.proxies.retrofit;
import water.bindings.pojos.*;
import com.google.gson.*;
import retrofit2.*;
import retrofit2.http.*;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.Call;
import java.io.IOException;
import java.lang.reflect.Type;
public class Example {
/**
* Keys get sent as Strings and returned as objects also containing the type and URL,
* so they need a custom GSON serializer.
*/
private static class KeySerializer implements JsonSerializer {
public JsonElement serialize(KeyV3 key, Type typeOfKey, JsonSerializationContext context) {
return new JsonPrimitive(key.name);
}
}
public static JobV3 poll(Retrofit retrofit, String job_id) {
Jobs jobsService = retrofit.create(Jobs.class);
Response jobs_response = null;
int retries = 3;
JobsV3 jobs = null;
do {
try {
jobs_response = jobsService.fetch(job_id).execute();
}
catch (IOException e) {
System.err.println("Caught exception: " + e);
}
if (! jobs_response.isSuccessful())
if (retries-- > 0)
continue;
else
throw new RuntimeException("/3/Jobs/{job_id} failed 3 times.");
jobs = jobs_response.body();
if (null == jobs.jobs || jobs.jobs.length != 1)
throw new RuntimeException("Failed to find Job: " + job_id);
if (! "RUNNING".equals(jobs.jobs[0].status)) try { Thread.sleep(100); } catch (InterruptedException e) {} // wait 100mS
} while ("RUNNING".equals(jobs.jobs[0].status));
return jobs.jobs[0];
}
public static void main (String[] args) {
Gson gson = new GsonBuilder().registerTypeAdapter(KeyV3.class, new KeySerializer()).create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://localhost:54321/") // note trailing slash for Retrofit 2
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
CreateFrame createFrameService = retrofit.create(CreateFrame.class);
Frames framesService = retrofit.create(Frames.class);
Models modelsService = retrofit.create(Models.class);
try {
// NOTE: the Call objects returned by the service can't be reused, but they can be cloned.
Response all_frames_response = framesService.list().execute();
Response all_models_response = modelsService.list().execute();
if (all_frames_response.isSuccessful()) {
FramesV3 all_frames = all_frames_response.body();
System.out.println("All Frames: ");
System.out.println(all_frames);
} else {
System.err.println("framesService.list() failed");
}
if (all_models_response.isSuccessful()) {
ModelsV3 all_models = all_models_response.body();
System.out.println("All Models: ");
System.out.println(all_models);
} else {
System.err.println("modelsService.list() failed");
}
Response create_frame_response = createFrameService.run(null, 1000, 100, 42, 42, true, 0, 100000, 0.2, 100, 0.2, 32767, 0.2, 0.5, 0.2, 0, 0.2, 2, true, null).execute();
if (create_frame_response.isSuccessful()) {
JobV3 job = create_frame_response.body();
if (null == job || null == job.key)
throw new RuntimeException("CreateFrame returned a bad Job: " + job);
job = poll(retrofit, job.key.name);
KeyV3 new_frame = job.dest;
System.out.println("Created frame: " + new_frame);
all_frames_response = framesService.list().execute();
if (all_frames_response.isSuccessful()) {
FramesV3 all_frames = all_frames_response.body();
System.out.println("All Frames (after createFrame): ");
System.out.println(all_frames);
} else {
System.err.println("framesService.list() failed");
}
Response one_frame_response = framesService.fetch(new_frame.name).execute();
if (one_frame_response.isSuccessful()) {
FramesV3 one_frames = one_frame_response.body();
System.out.println("One Frame (after createFrame): ");
System.out.println(one_frames);
} else {
System.err.println("framesService.fetch() failed");
}
} else {
System.err.println("createFrameService.run() failed");
}
}
catch (IOException e) {
System.err.println("Caught exception: " + e);
}
}
}