private static String performCall(final URL url, final String method, final String data) { return performCall(url, method, data, null); }
public static String performCall(final String url, final JSONObject json) { return performCall(url, POST, json); }
public static String performCall(final String url, final String method) { try { return performCall(new URL(url), method, ""); } catch (MalformedURLException e) { e.printStackTrace(); } return ""; }
public static String performCall(final String url, final String method, final String data) { try { return performCall(new URL(url), method, data); } catch (MalformedURLException e) { e.printStackTrace(); } return ""; }
public static String performCall(final String url, final String method, final JSONObject json) { try { return performCall(new URL(url), method, json.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } return ""; }
public static String performCall(final String url, final String method, final HashMap<String, String> params) { try { return performCall(new URL(url), method, encodeQuery(params)); } catch (UnsupportedEncodingException | MalformedURLException e) { e.printStackTrace(); } return ""; }
private static JSONObject getUserInfo(String token) { try { return new JSONObject(NetworkUtils.performCall(getUrl( "user?access_token=%s", token), NetworkUtils.GET)); } catch (JSONException e) { e.printStackTrace(); return null; } }
public void run() { // Returns a simple result, handleable without json parser {"key":"feediyujiq"} String ret = NetworkUtils.performCall(server + "/documents", NetworkUtils.POST, text); final String key = (ret.length() > 15) ? ret.split("\"")[3] : ""; handler.post(() -> callback.callback(!key.isEmpty(), server + "/" + key)); } }.start();
public static String getDefaultBranch(final RepoHandler repo) { try { JSONObject result = new JSONObject(NetworkUtils.performCall(getUrl( "repos/%s", repo.toOwnerRepo()), NetworkUtils.GET)); return result.getString("default_branch"); } catch (JSONException | InvalidObjectException e) { e.printStackTrace(); return null; } }
public static JSONObject createIssue(RepoHandler repo, String title, String description, String token) throws InvalidObjectException { try { JSONObject params = new JSONObject(); params.put("title", title); params.put("body", description); return new JSONObject(NetworkUtils.performCall(getUrl("repos/%s/issues?access_token=%s", repo.toOwnerRepo(), token), params)); } catch (JSONException e) { e.printStackTrace(); return null; } }
public static JSONArray getBranches(final RepoHandler repo) { try { return new JSONArray(NetworkUtils.performCall(getUrl( "repos/%s/branches", repo.toOwnerRepo()), NetworkUtils.GET)); } catch (JSONException | InvalidObjectException e) { e.printStackTrace(); return null; } }
public static JSONObject commentIssue(RepoHandler repo, int issueNumber, String body, String token) throws InvalidObjectException { try { JSONObject params = new JSONObject(); params.put("body", body); return new JSONObject(NetworkUtils.performCall(getUrl( "repos/%s/issues/%d/comments?access_token=%s", repo.toOwnerRepo(), issueNumber, token), params)); } catch (JSONException e) { e.printStackTrace(); return null; } }
private static JSONArray getCommits(final String token, final RepoHandler repo) throws InvalidObjectException { try { return new JSONArray(NetworkUtils.performCall(getUrl( "repos/%s/commits?access_token=%s", repo.toOwnerRepo(), token), NetworkUtils.GET)); } catch (JSONException e) { e.printStackTrace(); return null; } }
public static JSONObject createPullRequest(final String token, final RepoHandler originalRepo, final String title, final String head, final String base, final String body) throws InvalidObjectException { try { JSONObject params = new JSONObject(); params.put("title", title); params.put("head", head); params.put("base", base); if (body != null && !body.isEmpty()) params.put("body", body); return new JSONObject(NetworkUtils.performCall(getUrl( "repos/%s/pulls?access_token=%s", originalRepo.toOwnerRepo(), token), params)); } catch (JSONException e) { e.printStackTrace(); return null; } }
public static JSONObject createBranch(final String token, final RepoHandler repo, final String branchName) throws InvalidObjectException { try { JSONArray head = new JSONArray(NetworkUtils.performCall(getUrl( "repos/%s/git/refs/heads?access_token=%s", repo.toOwnerRepo(), token), NetworkUtils.GET)); final String sha = head.getJSONObject(0).getJSONObject("object").getString("sha"); JSONObject params = new JSONObject(); params.put("ref", "refs/heads/" + branchName); params.put("sha", sha); return new JSONObject(NetworkUtils.performCall(getUrl( "repos/%s/git/refs?access_token=%s", repo.toOwnerRepo(), token), params)); } catch (JSONException e) { e.printStackTrace(); return null; } }
public static JSONObject createGist(String description, boolean isPublic, HashMap<String, String> fileContents, String token) { try { JSONObject params = new JSONObject(); if (description != null && !description.isEmpty()) params.put("description", description); params.put("public", isPublic); JSONObject filesObject = new JSONObject(); for (Map.Entry<String, String> fileContent : fileContents.entrySet()) { JSONObject fileObject = new JSONObject(); fileObject.put("content", fileContent.getValue()); // Add this file object (with content) using the filename as key filesObject.put(fileContent.getKey(), fileObject); } params.put("files", filesObject); if (token.isEmpty()) return new JSONObject(NetworkUtils.performCall(getUrl("gists"), params)); else return new JSONObject(NetworkUtils.performCall(getUrl("gists?access_token=" + token), params)); } catch (JSONException e) { e.printStackTrace(); return null; } }
private static JSONArray getCollaborators(String token, RepoHandler repo) throws InvalidObjectException { try { return new JSONArray(NetworkUtils.performCall(getUrl( "repos/%s/collaborators?access_token=%s", repo.toOwnerRepo(), token), NetworkUtils.GET)); } catch (JSONException e) { // We might not have permission so the response isn't an array, rather an object: // { // "message": "Must have push access to view repository collaborators.", // "documentation_url": "https://developer.github.com/v3/repos/collaborators/#list-collaborators" // } e.printStackTrace(); return null; } }
public static CompleteAuthenticationResult completeGitHubAuth(SlAppSettings appSettings, String clientId, String clientSecret, String code) { final HashMap<String, String> map = new HashMap<>(); map.put("client_id", clientId); map.put("client_secret", clientSecret); map.put("code", code); CompleteAuthenticationResult ret = new CompleteAuthenticationResult(); HashMap<String, String> postResult = NetworkUtils.getDataMap( NetworkUtils.performCall(GITHUB_COMPLETE_AUTH_URL, NetworkUtils.POST, map) ); if (postResult.containsKey("error")) { ret.message = postResult.get("error_description"); } else { ret.token = postResult.get("access_token"); ret.grantedScopes = postResult.get("scope"); appSettings.setGitHubAccess(ret.token, ret.grantedScopes); if (appSettings.hasGitHubAuthorization()) { ret.ok = true; } } return ret; } }
public static JSONObject forkRepository(final String token, final RepoHandler repo) throws InvalidObjectException { try { JSONObject result = new JSONObject(NetworkUtils.performCall(getUrl( "repos/%s/forks?access_token=%s", repo.toOwnerRepo(), token), NetworkUtils.POST)); // "Forking a Repository happens asynchronously." // One way to know when forking is done is to fetch the list of commits for the fork. // (http://stackoverflow.com/a/33667417/4759433) int sleep = 1000; do { try { Thread.sleep(sleep); sleep *= 2; // Sleep longer if the repo isn't yet forked not to call so often } catch (InterruptedException e) { e.printStackTrace(); return null; } } while (getCommits(token, repo) == null); return result; } catch (JSONException e) { e.printStackTrace(); return null; } }
JSONObject head = new JSONObject(NetworkUtils.performCall( getUrl("repos/%s/git/refs/heads/%s%s", ownerRepo, branch, tokenQuery), NetworkUtils.GET)); JSONObject commit = new JSONObject(NetworkUtils.performCall(headCommitUrl + tokenQuery, NetworkUtils.GET)); newBlob.put("encoding", "utf-8"); JSONObject blob = new JSONObject(NetworkUtils.performCall( getUrl("repos/%s/git/blobs%s", ownerRepo, tokenQuery), newBlob)); JSONObject baseTree = new JSONObject(NetworkUtils.performCall(treeUrl + tokenQuery, NetworkUtils.GET)); JSONObject createdTree = new JSONObject(NetworkUtils.performCall( getUrl("repos/%s/git/trees%s", ownerRepo, tokenQuery), newTree)); JSONObject repliedNewCommit = new JSONObject(NetworkUtils.performCall( getUrl("repos/%s/git/commits%s", ownerRepo, tokenQuery), newCommit)); patch.put("sha", repliedNewCommit.getString("sha")); return new JSONObject(NetworkUtils.performCall( getUrl("repos/%s/git/refs/heads/%s%s", ownerRepo, branch, tokenQuery), NetworkUtils.PATCH, patch));