Skip to content

Instantly share code, notes, and snippets.

@gje4
Created July 20, 2018 19:30
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 gje4/5564ca4bd8c13ca1d54bd8d36f795cd9 to your computer and use it in GitHub Desktop.
Save gje4/5564ca4bd8c13ca1d54bd8d36f795cd9 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;
/**
* This is a class that takes care of Authorizing your Moltin API and gives you basic get/update/delete functions to interact with the API. The data ret
returned from Moltin will be JSON objects.
*/
public class MoltinAdapter {
private static final String MOLTIN_AUTH_URL_BASE
= "https://api.moltin.com/oauth/access_token";
private static String moltinToken;
public static void authMoltin(final Context context, String clientId) {
AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Accept", "application/json");
client.addHeader("Content-Type", "application/json");
RequestParams params = new RequestParams();
params.put("client_id", clientId);
params.put("grant_type", "implicit");
client.post(MOLTIN_AUTH_URL_BASE, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject jsonObject) {
SharedPreferences sharedPreferences = context.getApplicationContext().getSharedPreferences("MoltinToken", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
try {
//TODO class or application
editor.putString("MoltinToken", jsonObject.getString("access_token"));
setMoltinToken(jsonObject.getString("access_token"));
} catch (JSONException e) {
e.printStackTrace();
}
editor.apply();
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Log error message
Log.e("Moltin", statusCode + " " + throwable.getMessage());
}
});
}
//GET: Returns the get response from the endpoint passed
/**
* @param endpoint the specific API endpoint you wish to hit https://docs.moltin.com/.
* @param attributes String, key map containing any includes, filtering, sorting or id look up you may want to do.
*/
public static Object getMoltin(String endpoint, Map<String, String> attributes) {
AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Accept", "application/json");
client.addHeader("Content-Type", "application/json");
RequestParams params = new RequestParams();
params.put("Authorization:", "bearer" + "-" + getMoltinToken());
//TODO map and look for common things
final Object[] responseObject = new Object[1];
client.post(MOLTIN_AUTH_URL_BASE, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject jsonObject) {
responseObject[0] = jsonObject;
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Log error message
Log.e("Moltin Get", statusCode + " " + throwable.getMessage());
}
});
return responseObject[0];
}
//UPDATE: Returns the update response from the endpoint passed
/**
* @param endpoint the specific API endpoint you wish to hit https://docs.moltin.com/.
* @param attributes String, key map containing any includes, filtering, sorting or id look up you may want to do.
*/
public static Object updateMoltin(String endpoint, Map<String, String> attributes) {
AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Accept", "application/json");
client.addHeader("Content-Type", "application/json");
RequestParams params = new RequestParams();
params.put("Authorization:", "bearer" + "-" + getMoltinToken());
final Object[] responseObject = new Object[1];
client.post(MOLTIN_AUTH_URL_BASE, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject jsonObject) {
responseObject[0] = jsonObject;
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Log error message
Log.e("Moltin update", statusCode + " " + throwable.getMessage());
}
});
return responseObject[0];
}
//DELETE: Returns the delete response from the endpoint passed
/**
* @param endpoint the specific API endpoint you wish to hit https://docs.moltin.com/.
* @param attributes String, key map containing any includes, filtering, sorting or id look up you may want to do.
*/
public static Object deleteMoltin(String endpoint, Map<String, String> attributes) {
AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Accept", "application/json");
client.addHeader("Content-Type", "application/json");
RequestParams params = new RequestParams();
params.put("Authorization:", "bearer" + "-" + getMoltinToken());
final Object[] responseObject = new Object[1];
client.post(MOLTIN_AUTH_URL_BASE, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject jsonObject) {
responseObject[0] = jsonObject;
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Log error message
Log.e("Moltin delete", statusCode + " " + throwable.getMessage());
}
});
return responseObject[0];
}
//Token handlers
public static String getMoltinToken() {
return moltinToken;
}
public static void setMoltinToken(String moltinToken) {
MoltinAdapter.moltinToken = moltinToken;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment