Skip to content

Instantly share code, notes, and snippets.

@clackbib
Created January 22, 2016 23:22
Show Gist options
  • Save clackbib/588efd78d40a6da47baf to your computer and use it in GitHub Desktop.
Save clackbib/588efd78d40a6da47baf to your computer and use it in GitHub Desktop.
GithubApi using GSON for parsing
package gitit.com.gitit.service.contract;
/**
* 2015
* Created by ho on 05/08/15.
*/
public class Comment {
public User user;
public String body;
}
package gitit.com.gitit.service;
import android.os.AsyncTask;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import gitit.com.gitit.service.contract.Comment;
import gitit.com.gitit.service.contract.Issue;
/**
* 2015
* Created by ho on 05/08/15.
*/
public class GithubApi {
/** Best thing to do would have been to use lib such as retrofit
* if there was no limitation, and provide the api using a DI frameword like dagger.
* Not a big fan of using static methods, but it'll do for now.
*
* Accessing github api without Token is subject to Limitations by IP by the way.
*/
private static final String BASE_URL = "https://api.github.com";
/** Hard coded arguments for sorting by last updated and state open **/
private static final String REPO_ISSUES_ENDPOINT = BASE_URL + "/repos/%s/%s/issues?sort=updated&state=open";
private static final String REPO_COMMENTS_ENDPOINT = BASE_URL + "/repos/%s/%s/issues/%s/comments";
public static void getIssues(String owner, String repo, WebCallBack<ArrayList<Issue>> callBack) {
Type type = new TypeToken<ArrayList<Issue>>() {
}.getType();
submitHttpRequest(String.format(REPO_ISSUES_ENDPOINT, owner, repo), callBack, type);
}
public static void getComments(String owner, String repo, long issueNumber, WebCallBack<ArrayList<Comment>> callback) {
Type type = new TypeToken<ArrayList<Comment>>() {
}.getType();
submitHttpRequest(String.format(REPO_COMMENTS_ENDPOINT, owner, repo, String.valueOf(issueNumber)), callback, type);
}
/**
*
* @param endpoint Endpoint being hit with a connection
* @param callBack Abstract callback
* @param type Type to be used for response deserialization
* @param <T> Type to be used as a cast after response deserialization
*/
@SuppressWarnings("unchecked")
private static <T> void submitHttpRequest(final String endpoint, final WebCallBack<T> callBack, final Type type) {
new AsyncTask<Void, Void, T>() {
@Override
protected T doInBackground(Void... params) {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(endpoint);
urlConnection = (HttpURLConnection) url
.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
/** Only parse json on success. If the parsing fails, the exception will be caught. **/
if (urlConnection.getResponseCode() == 200 || urlConnection.getResponseCode() == 201) {
return (T) (new Gson().fromJson(isw, type));
}
} catch (Exception e) {
Log.e("Exception", "Exception while performing call");
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
/** Callback is coming from the UI thread, so should only be called on post execute.**/
@Override
protected void onPostExecute(T t) {
super.onPostExecute(t);
if (t != null) {
callBack.onSuccess(t);
} else {
callBack.onFailure(new Exception("Failure"));
}
}
}.execute();
}
}
package gitit.com.gitit.service.contract;
/**
* 2015
* Created by ho on 05/08/15.
*/
public class Issue {
public String body;
public String title;
public long number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment