Skip to content

Instantly share code, notes, and snippets.

@ckxng
Created May 21, 2015 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckxng/f7399f40515839979a5b to your computer and use it in GitHub Desktop.
Save ckxng/f7399f40515839979a5b to your computer and use it in GitHub Desktop.
A class to interface with Enjin
/**
*
*/
package com.ckxng.enjinbenefits;
import java.net.*;
import java.util.HashMap;
import java.io.*;
import org.json.*;
/**
* @author Cameron
*
*/
public class EnjinAPI {
/** The key provided by the Enjin admin panel - SECRET! */
private String key;
/** The API URL provided by the Enjin admin panel */
private String url;
/**
* An interface to the Enjin API for a Minecraft site
* @param inUrl The API URL provided by the Enjin admin panel
* @param inKey The key provided by the Enjin admin panel - SECRET!
*/
public EnjinAPI(String inUrl, String inKey) {
url = inUrl;
key = inKey;
}
/**
* An interface to the Enjin API for a Minecraft site. If
* setKey(String) is not called, only unauthenticated
* requests will be possible.
* @param inUrl The API URL provided by the Enjin admin panel
*/
public EnjinAPI(String inUrl) {
url = inUrl;
}
/**
* Set the site key so that unauthenticated requests can be made.
* @param inKey The key provided by the Enjin admin panel - SECRET!
* @return this object
*/
public EnjinAPI setUrl(String inKey) {
key = inKey;
return this;
}
/**
* Generate a JSONObject payload to be sent with the API request.
* @param inMethod the API method to call
* @param inParams the parameters to be passed
* @return the crafted payload
*/
private static JSONObject generatePayload(String inMethod, JSONObject inParams) {
JSONObject payload = new JSONObject();
payload.put("jsonrpc","2.0");
payload.put("id", (int)(Math.random()*1000));
payload.put("params", inParams);
payload.put("method", inMethod);
return payload;
}
/**
* Generate a JSONObject payload to be sent with the API request.
* @param inMethod the API method to call
* @param inParams the parameters to be passed
* @return the crafted payload
*/
private static JSONObject generatePayload(String inMethod, HashMap<String,Object> inParams) {
return generatePayload(inMethod, new JSONObject(inParams));
}
/**
* Submit the request and return a result as a single String
* @param inMethod the API method to call
* @param inParams the parameters to be passed
* @return the response from the server
*/
public JSONObject submit(String inMethod, JSONObject inParams) {
JSONObject payload = generatePayload(inMethod, inParams);
// connect to collect stats
try {
URL enjinStatsUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)enjinStatsUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
} catch(Exception ex) {
System.err.println("Error connection to Enjin API");
return new JSONObject();
}
// send stats payload
try {
System.out.println("Sending request: "+payload.toString());
OutputStreamWriter out = new OutputStreamWriter(
conn.getOutputStream());
out.write(payload.toString());
out.flush();
} catch(Exception ex) {
System.err.println("Error sending payload to Enjin API");
return new JSONObject();
}
// read stats
String buffer = "";
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
String lineIn;
while((lineIn = in.readLine()) != null) {
buffer += lineIn;
}
in.close();
System.out.println("Recieved: "+buffer);
} catch(Exception ex) {
System.err.println("Unable to recieve response from Enjin API");
return new JSONObject();
}
// convert to JSONObject
try {
return new JSONObject(buffer);
} catch(Exception ex) {
System.err.println("Unable to parse response from Enjin API");
}
return new JSONObject();
}
/**
* Submit the request and return a result as a single String
* @param inMethod the API method to call
* @param inParams the parameters to be passed
* @return the response from the server
*/
public JSONObject submit(String inMethod, HashMap<String,Object> inParams) {
return submit(inMethod, new JSONObject(inParams));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment