Created
July 20, 2018 18:53
-
-
Save gje4/96e3222a50b85b9d2f10fe36bac83703 to your computer and use it in GitHub Desktop.
This is the 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
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
package com.example.omgandroid; | |
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 the 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 String moltinToken; | |
public 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) { | |
Log.e("APi worked", String.valueOf(jsonObject)); | |
SharedPreferences sharedPreferences = context.getApplicationContext().getSharedPreferences("MoltinToken", Context.MODE_PRIVATE); | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
try { | |
//Store in class or in application. In class may be seen as memory leak | |
editor.putString("MoltinToken", jsonObject.getString("access_token")); | |
setMoltinToken(jsonObject.getString("access_token")); | |
Log.e("Token", 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 Object getMoltin(String endpoint, Map<String, String> attributes) { | |
// Create a client to perform networking | |
AsyncHttpClient client = new AsyncHttpClient(); | |
client.addHeader("Accept", "application/json"); | |
client.addHeader("Content-Type", "application/json"); | |
RequestParams params = new RequestParams(); | |
params.put("autherization", "bearer" + "-" + getMoltinToken()); | |
//V2 go through 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) { | |
Log.e("APi worked", String.valueOf(jsonObject)); | |
responseObject[0] = jsonObject; | |
} | |
@Override | |
public void onFailure(int statusCode, Throwable throwable, JSONObject error) { | |
// Log error message | |
Log.e("Moltin", 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 Object updateMoltin(String endpoint, Map<String, String> attributes) { | |
// Create a client to perform networking | |
AsyncHttpClient client = new AsyncHttpClient(); | |
client.addHeader("Accept", "application/json"); | |
client.addHeader("Content-Type", "application/json"); | |
RequestParams params = new RequestParams(); | |
params.put("autherization", "bearer" + "-" + getMoltinToken()); | |
final Object[] responseObject = new Object[1]; | |
client.post(MOLTIN_AUTH_URL_BASE, params, new JsonHttpResponseHandler() { | |
@Override | |
public void onSuccess(JSONObject jsonObject) { | |
Log.e("APi worked", String.valueOf(jsonObject)); | |
responseObject[0] = jsonObject; | |
} | |
@Override | |
public void onFailure(int statusCode, Throwable throwable, JSONObject error) { | |
// Log error message | |
Log.e("Moltin", 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 Object deleteMoltin(String endpoint, Map<String, String> attributes) { | |
// Create a client to perform networking | |
AsyncHttpClient client = new AsyncHttpClient(); | |
client.addHeader("Accept", "application/json"); | |
client.addHeader("Content-Type", "application/json"); | |
RequestParams params = new RequestParams(); | |
params.put("autherization", "bearer" + "-" + getMoltinToken()); | |
final Object[] responseObject = new Object[1]; | |
client.post(MOLTIN_AUTH_URL_BASE, params, new JsonHttpResponseHandler() { | |
@Override | |
public void onSuccess(JSONObject jsonObject) { | |
Log.e("APi worked", String.valueOf(jsonObject)); | |
responseObject[0] = jsonObject; | |
} | |
@Override | |
public void onFailure(int statusCode, Throwable throwable, JSONObject error) { | |
// Log error message | |
Log.e("Moltin", statusCode + " " + throwable.getMessage()); | |
} | |
}); | |
return responseObject[0]; | |
} | |
//Token handlers | |
public String getMoltinToken() { | |
return moltinToken; | |
} | |
public void setMoltinToken(String moltinToken) { | |
this.moltinToken = moltinToken; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment