-
-
Save samis/726e33e93ec2fad571d9 to your computer and use it in GitHub Desktop.
Gist files for refactoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
/** | |
* A Java-wrapper to post and get data from reddapi.com (using REST) | |
* | |
* @author Leonard Simonse | |
* @version 1.0 | |
* <p/> | |
* 2014-04-09 | |
*/ | |
public class API { | |
private final RequestSender requestSender = new RequestSender(this); | |
private String baseURL = "https://api.reddapi.com/v1/json/"; | |
private final String USER_AGENT = "Mozilla/5.0"; | |
private boolean debugMode = false; | |
// Constructor // | |
/** | |
* @param key_GET API-key to perform GET-requests using HTTP(S). | |
* @param key_POST API-key to perform POST-requests using HTTP(S). | |
*/ | |
public API(String key_GET, String key_POST) { | |
this.requestSender.setKey_GET(key_GET); | |
this.requestSender.setKey_POST(key_POST); | |
} | |
// Getters AND Setters // | |
/** | |
* @return A String containing the current API-key (required to GET). | |
*/ | |
public String getKey_GET() { | |
return requestSender.getKey_GET(); | |
} | |
/** | |
* @param key_GET A String containing an API-key (required to GET) as a | |
* parameter. | |
*/ | |
public void setKey_GET(String key_GET) { | |
requestSender.setKey_GET(key_GET); | |
} | |
/** | |
* @return A String containing the current API-key (required to POST). | |
*/ | |
public String getKey_POST() { | |
return requestSender.getKey_POST(); | |
} | |
/** | |
* @param key_POST A String containing an API-key (required to POST) as a | |
* parameter. | |
*/ | |
public void setKey_POST(String key_POST) { | |
requestSender.setKey_POST(key_POST); | |
} | |
// GET and POST templates // | |
/** | |
* @param command The command that will be executed on the API-server using a | |
* GET-request. | |
* @param arg A String containing an additional URL-parameter (null is | |
* allowed). | |
* @return A JSON-object packaged in a String, containing the requested | |
* info. | |
*/ | |
private String requestGET(String command, String arg) { | |
return requestSender.requestGET(command, arg); | |
} | |
/** | |
* @param command The command that will be executed on the API-server using a | |
* POST-request. | |
* @param argMap A HashMap containing RequestData. | |
* @return Return | |
*/ | |
private String requestPOST(String command, HashMap<String, Object> argMap) { | |
return requestSender.requestPOST(command, argMap); | |
} | |
// GET-REQUESTS // | |
/** | |
* @param userName Username attached to a user. | |
* @return The current credit-balance of the given username as a String. | |
*/ | |
public String getUserBalance(String userName) { | |
return requestSender.requestGET("GetUserBalance", userName); | |
} | |
/** | |
* @param userName Username attached to a user. | |
* @return The current confirmed and unconfirmed credit-balance of the | |
* given username as a String. | |
*/ | |
public String getUserBalanceDetail(String userName) { | |
return requestSender.requestGET("GetUserBalanceDetail", userName); | |
} | |
/** | |
* @param userName Username attached to a user. | |
* @return A JSON-object packaged in a String, containing UserInfo. | |
*/ | |
public String getUserInfo(String userName) { | |
return requestSender.requestGET("GetUserInfo", userName); | |
} | |
/** | |
* @return A JSON-object packaged in a String, containing a UserList. | |
*/ | |
public String getUserList() { | |
return requestSender.requestGET("GetUserList", null); | |
} | |
/** | |
* @param userName Username to attach to the newly created user. | |
* @return A JSON-object packaged in a String containing UserInfo about the | |
* newly created user. | |
*/ | |
public String createNewUser(String userName) { | |
HashMap<String, Object> argMap = new HashMap<String, Object>(); | |
argMap.put("APIKey", requestSender.getKey_POST()); | |
argMap.put("Username", userName); | |
return requestSender.requestPOST("CreateNewUser", argMap); | |
} | |
/** | |
* @param userNameFrom Username to send the amount from. | |
* @param userNameTo Username to send the amount to. | |
* @param amount The amount to send. | |
* @return A JSON-object packaged in a String containing information about | |
* state of the requested transaction. | |
*/ | |
public String moveToUser(String userNameFrom, String userNameTo, | |
double amount) { | |
HashMap<String, Object> argMap = new HashMap<String, Object>(); | |
argMap.put("APIKey", requestSender.getKey_POST()); | |
argMap.put("UsernameFrom", userNameFrom); | |
argMap.put("UsernameTo", userNameTo); | |
argMap.put("Amount", amount); | |
return requestSender.requestPOST("MoveToUser", argMap); | |
} | |
/** | |
* @param userNameFrom Username to send the amount from. | |
* @param addressTo Address to send the amount to. | |
* @param amount The amount to send. | |
* @return A JSON-object packaged in a String containing information about | |
* state of the requested transaction. | |
*/ | |
public String sendToAddress(String userNameFrom, String addressTo, | |
double amount) { | |
HashMap<String, Object> argMap = new HashMap<String, Object>(); | |
argMap.put("APIKey", requestSender.getKey_POST()); | |
argMap.put("UsernameFrom", userNameFrom); | |
argMap.put("AddressTo", addressTo); | |
argMap.put("Amount", amount); | |
return requestSender.requestPOST("SendToAddress", argMap); | |
} | |
/** | |
* @param e A Stri |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson; | |
import javax.net.ssl.HttpsURLConnection; | |
import java.io.*; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
import java.util.HashMap; | |
public class RequestSender { | |
private final in.companion.cloudns.jdra.api.API API; | |
private String key_GET; | |
private String key_POST; | |
public RequestSender(in.companion.cloudns.jdra.api.API API) { | |
this.API = API; | |
} | |
/** | |
* @return A String containing the current API-key (required to GET). | |
*/ | |
public String getKey_GET() { | |
return key_GET; | |
} | |
/** | |
* @param key_GET A String containing an API-key (required to GET) as a | |
* parameter. | |
*/ | |
public void setKey_GET(String key_GET) { | |
this.key_GET = key_GET; | |
} | |
/** | |
* @return A String containing the current API-key (required to POST). | |
*/ | |
public String getKey_POST() { | |
return key_POST; | |
} | |
/** | |
* @param key_POST A String containing an API-key (required to POST) as a | |
* parameter. | |
*/ | |
public void setKey_POST(String key_POST) { | |
this.key_POST = key_POST; | |
} | |
/** | |
* @param command The command that will be executed on the API-server using a | |
* GET-request. | |
* @param arg A String containing an additional URL-parameter (null is | |
* allowed). | |
* @return A JSON-object packaged in a String, containing the requested | |
* info. | |
*/ | |
String requestGET(String command, String arg) { | |
String addString = command + "/" + key_GET; | |
if (arg != null) { | |
StringBuilder builder = new StringBuilder(addString); | |
builder.append("/" + arg); | |
addString = builder.toString(); | |
} | |
String url = API.getBaseURL() + addString; | |
URL obj = null; | |
try { | |
obj = new URL(url); | |
} catch (MalformedURLException e) { | |
if (API.isDebugMode()) | |
API.printError(e.toString()); | |
} | |
HttpURLConnection con = null; | |
try { | |
con = (HttpURLConnection) obj.openConnection(); | |
} catch (IOException e) { | |
if (API.isDebugMode()) | |
API.printError(e.toString()); | |
} | |
try { | |
con.setRequestMethod("GET"); | |
} catch (ProtocolException e) { | |
if (API.isDebugMode()) | |
API.printError(e.toString()); | |
} | |
int responseCode; | |
try { | |
con.setRequestProperty("User-Agent", API.getUSER_AGENT()); | |
responseCode = con.getResponseCode(); | |
if (API.isDebugMode()) { | |
System.out.println("\nSending 'GET' request to URL : " + url); | |
System.out.println("Response Code : " + responseCode); | |
} | |
InputStream stream = null; | |
if (con.getResponseCode() >= 400) { | |
stream = con.getErrorStream(); | |
} else { | |
stream = con.getInputStream(); | |
} | |
BufferedReader in = new BufferedReader( | |
new InputStreamReader(stream)); | |
String inputLine; | |
StringBuffer response = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
response.append(inputLine); | |
} | |
in.close(); | |
if (API.isDebugMode()) | |
System.out.println(response.toString()); | |
return response.toString(); | |
} catch (IOException e) { | |
if (API.isDebugMode()) | |
API.printError(e.toString()); | |
} | |
return null; | |
} | |
/** | |
* @param command The command that will be executed on the API-server using a | |
* POST-request. | |
* @param argMap A HashMap containing RequestData. | |
* @return Return | |
*/ | |
String requestPOST(String command, HashMap<String, Object> argMap) { | |
String url = API.getBaseURL() + command; | |
URL obj = null; | |
try { | |
obj = new URL(url); | |
} catch (MalformedURLException e) { | |
if (API.isDebugMode()) | |
API.printError(e.toString()); | |
} | |
HttpsURLConnection con = null; | |
try { | |
con = (HttpsURLConnection) obj.openConnection(); | |
} catch (IOException e) { | |
if (API.isDebugMode()) | |
API.printError(e.toString()); | |
} | |
try { | |
con.setRequestMethod("POST"); | |
con.setRequestProperty("Content-Type", "application/json"); | |
con.setRequestProperty("User-Agent", API.getUSER_AGENT()); | |
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); | |
} catch (ProtocolException e) { | |
if (API.isDebugMode()) | |
API.printError(e.toString()); | |
} | |
Gson gson = new Gson(); | |
String parameters = gson.toJson(argMap); | |
con.setDoOutput(true); | |
DataOutputStream wr; | |
try { | |
wr = new DataOutputStream(con.getOutputStream()); | |
wr.writeBytes(parameters); | |
wr.flush(); | |
wr.close(); | |
int responseCode; | |
responseCode = con.getResponseCode(); | |
if (API.isDebugMode()) { | |
System.out.println("\nSending 'POST' request to URL : " + url); | |
System.out.println("Post parameters : " + parameters); | |
System.out.println("Response Code : " + responseCode); | |
} | |
InputStream stream = null; | |
if (con.getResponseCode() >= 400) { | |
stream = con.getErrorStream(); | |
} else { | |
stream = con.getInputStream(); | |
} | |
BufferedReader in = new BufferedReader( | |
new InputStreamReader(stream)); | |
String inputLine; | |
StringBuffer response = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
response.append(inputLine); | |
} | |
in.close(); | |
if (API.isDebugMode()) | |
System.out.println(response.toString()); | |
return response.toString(); | |
} catch (IOException e) { | |
if (API.isDebugMode()) | |
API.printError(e.toString()); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment