Skip to content

Instantly share code, notes, and snippets.

@six519
Created September 25, 2014 00:00
Show Gist options
  • Save six519/a0854eb4bf922c19d34b to your computer and use it in GitHub Desktop.
Save six519/a0854eb4bf922c19d34b to your computer and use it in GitHub Desktop.
AMP API Client
package com.ferdinandsilva.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
public class AMPAPIClient {
private String username;
private String password;
public static final String BASE_API_URL = "http://162.243.250.142:8888/forwards/";
public static final String AUTH_SESSION_METH = "AuthenticateSession";
public static final String ACTIVE_SESSION_METH = "AuthenticateSession";
public static final String LOGIN_METH = "Login";
public static final String FORGOT_PASS_METH = "ForgotPassword";
public static final String REG_MEMBER_METH = "RegisterMember";
public static final String UPDATE_PROF_METH = "UpdateProfile";
public static final String PROF_METH = "GetProfile";
public static final String CHKPOINTS_METH = "CheckPoints";
public static final String LST_ITEMS_METH = "ListItems";
public static final String REDEEM_ITEMS_METH = "RedeemItems";
public static final String GENDER_METH = "GeGender";
public static final String ID_PRESENTED_METH = "GetIDPresented";
public static final String NATIONALITY_METH = "GetNationality";
public static final String OCCUPATION_METH = "GetOccupation";
public static final String IS_SMOKER_METH = "GetIsSmoker";
public static final String REFERRER_METH = "GetReferrer";
public static final String REGION_METH = "GetRegion";
public static final String CITY_METH = "GetCity";
public static final String LOGOUT_METH = "Logout";
public static void main(String[] args) {
AMPAPIClient apiClient = new AMPAPIClient("xxxx", "xxxxx");
System.out.println("Running client...");
apiClient.authenticateSession();
}
public AMPAPIClient(String user, String pass){
this.username = user;
this.password = pass;
}
public JSONObject executeMethod(String methodType, JSONObject jsonParam) {
JSONObject ret = null;
StringBuilder strBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
String urlToConnect = AMPAPIClient.BASE_API_URL + methodType.toLowerCase();
HttpPost httpPost = new HttpPost(urlToConnect);
System.out.println("Executing method...");
System.out.println("The url is: " + urlToConnect);
try {
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(jsonParam.toString().getBytes("UTF8"));
httpPost.setEntity(byteArrayEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
System.out.println("Checking status code...");
System.out.println("Status code is: " + statusCode);
if(statusCode==200) {
System.out.println("HTTP OK!");
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line = bufferedReader.readLine()) != null) {
strBuilder.append(line);
}
System.out.println("The return is: " + strBuilder.toString());
ret = new JSONObject(strBuilder.toString());
}
} catch (ClientProtocolException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (JSONException e) {
System.out.println(e.getMessage());
}
return ret;
}
public JSONObject authenticateSession() {
JSONObject ret = null;
try {
JSONObject jsonObj = new JSONObject();
jsonObj.put("Username", this.username);
jsonObj.put("Password", this.password);
ret = this.executeMethod(AMPAPIClient.AUTH_SESSION_METH, jsonObj);
} catch (JSONException e) {
System.out.println(e.getMessage());
}
return ret;
}
public JSONObject authenticateSession(String user, String pass) {
JSONObject ret = null;
this.username = user;
this.password = pass;
try {
JSONObject jsonObj = new JSONObject();
jsonObj.put("Username", this.username);
jsonObj.put("Password", this.password);
ret = this.executeMethod(AMPAPIClient.AUTH_SESSION_METH, jsonObj);
} catch (JSONException e) {
System.out.println(e.getMessage());
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment