
org.deeplearning4j.gym.ClientUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gym-java-client Show documentation
Show all versions of gym-java-client Show documentation
A Java client for Open AI's Reinforcement Learning Gym
package org.deeplearning4j.gym;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.json.JSONException;
import org.json.JSONObject;
/**
* @author rubenfiszel ([email protected]) on 7/8/16.
*
* ClientUtils contain the utility methods to post and get data from the server REST API through the library unirest.
*/
public class ClientUtils {
static public JsonNode post(String url, JSONObject json) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.post(url)
.header("content-type", "application/json")
.body(json)
.asJson();
} catch (UnirestException e) {
unirestCrash(e);
}
return jsonResponse.getBody();
}
static public JSONObject get(String url) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.get(url)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
unirestCrash(e);
}
checkReply(jsonResponse, url);
return jsonResponse.getBody().getObject();
}
static public void checkReply(HttpResponse res, String url) {
if (res.getBody() == null)
throw new RuntimeException("Invalid reply at: " + url);
}
static public void unirestCrash(UnirestException e) {
//if couldn't parse json
if (e.getCause().getCause().getCause() instanceof JSONException)
throw new RuntimeException("Couldn't parse json reply.");
else
throw new RuntimeException("Connection error");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy