mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-10 08:21:21 +00:00
got rid of all retrieveXYZ methods in favor of Poster
This commit is contained in:
@@ -22,7 +22,7 @@ public class GHMyself extends GHUser {
|
||||
* Always non-null.
|
||||
*/
|
||||
public List<String> getEmails() throws IOException {
|
||||
String[] addresses = root.retrieveWithAuth("/user/emails", String[].class);
|
||||
String[] addresses = root.retrieve().withCredential().to("/user/emails", String[].class);
|
||||
return Collections.unmodifiableList(Arrays.asList(addresses));
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public class GHMyself extends GHUser {
|
||||
* Always non-null.
|
||||
*/
|
||||
public List<GHKey> getPublicKeys() throws IOException {
|
||||
return Collections.unmodifiableList(Arrays.asList(root.retrieveWithAuth("/user/keys", GHKey[].class)));
|
||||
return Collections.unmodifiableList(Arrays.asList(root.retrieve().withCredential().to("/user/keys", GHKey[].class)));
|
||||
}
|
||||
|
||||
// public void addEmails(Collection<String> emails) throws IOException {
|
||||
|
||||
@@ -42,7 +42,7 @@ public class GHOrganization extends GHPerson {
|
||||
* Teams by their names.
|
||||
*/
|
||||
public Map<String,GHTeam> getTeams() throws IOException {
|
||||
GHTeam[] teams = root.retrieveWithAuth("/orgs/" + login + "/teams", GHTeam[].class);
|
||||
GHTeam[] teams = root.retrieve().withCredential().to("/orgs/" + login + "/teams", GHTeam[].class);
|
||||
Map<String,GHTeam> r = new TreeMap<String, GHTeam>();
|
||||
for (GHTeam t : teams) {
|
||||
r.put(t.getName(),t.wrapUp(this));
|
||||
@@ -54,7 +54,7 @@ public class GHOrganization extends GHPerson {
|
||||
* Publicizes the membership.
|
||||
*/
|
||||
public void publicize(GHUser u) throws IOException {
|
||||
root.retrieveWithAuth("/orgs/" + login + "/public_members/" + u.getLogin(), null, "PUT");
|
||||
root.retrieve().withCredential().method("PUT").to("/orgs/" + login + "/public_members/" + u.getLogin(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,7 +64,7 @@ public class GHOrganization extends GHPerson {
|
||||
return new AbstractList<GHUser>() {
|
||||
// these are shallow objects with only some limited values filled out
|
||||
// TODO: it's better to allow objects to fill themselves in later when missing values are requested
|
||||
final GHUser[] shallow = root.retrieveWithAuth("/orgs/" + login + "/members", GHUser[].class);
|
||||
final GHUser[] shallow = root.retrieve().withCredential().to("/orgs/" + login + "/members", GHUser[].class);
|
||||
|
||||
@Override
|
||||
public GHUser get(int index) {
|
||||
@@ -86,7 +86,7 @@ public class GHOrganization extends GHPerson {
|
||||
* Conceals the membership.
|
||||
*/
|
||||
public void conceal(GHUser u) throws IOException {
|
||||
root.retrieveWithAuth("/orgs/" + login + "/public_members/" + u.getLogin(), null, "DELETE");
|
||||
root.retrieve().withCredential().method("DELETE").to("/orgs/" + login + "/public_members/" + u.getLogin(), null);
|
||||
}
|
||||
|
||||
public enum Permission { ADMIN, PUSH, PULL }
|
||||
|
||||
@@ -85,7 +85,7 @@ public abstract class GHPerson {
|
||||
*/
|
||||
public GHRepository getRepository(String name) throws IOException {
|
||||
try {
|
||||
return root.retrieveWithAuth("/repos/" + login + '/' + name, GHRepository.class).wrap(root);
|
||||
return root.retrieve().withCredential().to("/repos/" + login + '/' + name, GHRepository.class).wrap(root);
|
||||
} catch (FileNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ public class GHRepository {
|
||||
}
|
||||
|
||||
public List<GHIssue> getIssues(GHIssueState state) throws IOException {
|
||||
return Arrays.asList(GHIssue.wrap(root.retrieve("/repos/" + owner.login + "/" + name + "/issues?state=" + state.toString().toLowerCase(), GHIssue[].class), this));
|
||||
return Arrays.asList(GHIssue.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/issues?state=" + state.toString().toLowerCase(), GHIssue[].class), this));
|
||||
}
|
||||
|
||||
protected String getOwnerName() {
|
||||
@@ -213,7 +213,7 @@ public class GHRepository {
|
||||
*/
|
||||
@WithBridgeMethods(Set.class)
|
||||
public GHPersonSet<GHUser> getCollaborators() throws IOException {
|
||||
return new GHPersonSet<GHUser>(GHUser.wrap(root.retrieve("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root));
|
||||
return new GHPersonSet<GHUser>(GHUser.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,7 +222,7 @@ public class GHRepository {
|
||||
*/
|
||||
public Set<String> getCollaboratorNames() throws IOException {
|
||||
Set<String> r = new HashSet<String>();
|
||||
for (GHUser u : GHUser.wrap(root.retrieve("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root))
|
||||
for (GHUser u : GHUser.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root))
|
||||
r.add(u.login);
|
||||
return r;
|
||||
}
|
||||
@@ -231,7 +231,7 @@ public class GHRepository {
|
||||
* If this repository belongs to an organization, return a set of teams.
|
||||
*/
|
||||
public Set<GHTeam> getTeams() throws IOException {
|
||||
return Collections.unmodifiableSet(new HashSet<GHTeam>(Arrays.asList(GHTeam.wrapUp(root.retrieveWithAuth("/repos/" + owner.login + "/" + name + "/teams", GHTeam[].class), root.getOrganization(owner.login)))));
|
||||
return Collections.unmodifiableSet(new HashSet<GHTeam>(Arrays.asList(GHTeam.wrapUp(root.retrieve().withCredential().to("/repos/" + owner.login + "/" + name + "/teams", GHTeam[].class), root.getOrganization(owner.login)))));
|
||||
}
|
||||
|
||||
public void addCollaborators(GHUser... users) throws IOException {
|
||||
@@ -352,7 +352,7 @@ public class GHRepository {
|
||||
* Retrieves a specified pull request.
|
||||
*/
|
||||
public GHPullRequest getPullRequest(int i) throws IOException {
|
||||
return root.retrieveWithAuth("/repos/" + owner.login + '/' + name + "/pulls/" + i, GHPullRequest.class).wrapUp(this);
|
||||
return root.retrieve().withCredential().to("/repos/" + owner.login + '/' + name + "/pulls/" + i, GHPullRequest.class).wrapUp(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -386,14 +386,14 @@ public class GHRepository {
|
||||
*/
|
||||
public List<GHHook> getHooks() throws IOException {
|
||||
List<GHHook> list = new ArrayList<GHHook>(Arrays.asList(
|
||||
root.retrieveWithAuth(String.format("/repos/%s/%s/hooks", owner.login, name), GHHook[].class)));
|
||||
root.retrieve().withCredential().to(String.format("/repos/%s/%s/hooks", owner.login, name), GHHook[].class)));
|
||||
for (GHHook h : list)
|
||||
h.wrap(this);
|
||||
return list;
|
||||
}
|
||||
|
||||
public GHHook getHook(int id) throws IOException {
|
||||
return root.retrieveWithAuth(String.format("/repos/%s/%s/hooks/%d", owner.login, name, id), GHHook.class).wrap(this);
|
||||
return root.retrieve().withCredential().to(String.format("/repos/%s/%s/hooks/%d", owner.login, name, id), GHHook.class).wrap(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -402,7 +402,7 @@ public class GHRepository {
|
||||
public GHCommit getCommit(String sha1) throws IOException {
|
||||
GHCommit c = commits.get(sha1);
|
||||
if (c==null) {
|
||||
c = root.retrieve(String.format("/repos/%s/%s/commits/%s", owner.login, name, sha1), GHCommit.class).wrapUp(this);
|
||||
c = root.retrieve().to(String.format("/repos/%s/%s/commits/%s", owner.login, name, sha1), GHCommit.class).wrapUp(this);
|
||||
commits.put(sha1,c);
|
||||
}
|
||||
return c;
|
||||
@@ -610,7 +610,7 @@ public class GHRepository {
|
||||
*/
|
||||
public Map<String,GHBranch> getBranches() throws IOException {
|
||||
Map<String,GHBranch> r = new TreeMap<String,GHBranch>();
|
||||
for (GHBranch p : root.retrieve("/repos/" + owner.login + "/" + name + "/branches", GHBranch[].class)) {
|
||||
for (GHBranch p : root.retrieve().to("/repos/" + owner.login + "/" + name + "/branches", GHBranch[].class)) {
|
||||
p.wrap(this);
|
||||
r.put(p.getName(),p);
|
||||
}
|
||||
@@ -619,7 +619,7 @@ public class GHRepository {
|
||||
|
||||
public Map<Integer, GHMilestone> getMilestones() throws IOException {
|
||||
Map<Integer,GHMilestone> milestones = new TreeMap<Integer, GHMilestone>();
|
||||
GHMilestone[] ms = root.retrieve("/repos/" + owner.login + "/" + name + "/milestones", GHMilestone[].class);
|
||||
GHMilestone[] ms = root.retrieve().to("/repos/" + owner.login + "/" + name + "/milestones", GHMilestone[].class);
|
||||
for (GHMilestone m : ms) {
|
||||
m.owner = this;
|
||||
m.root = root;
|
||||
@@ -631,7 +631,7 @@ public class GHRepository {
|
||||
public GHMilestone getMilestone(int number) throws IOException {
|
||||
GHMilestone m = milestones.get(number);
|
||||
if (m == null) {
|
||||
m = root.retrieve("/repos/" + owner.login + "/" + name + "/milestones/" + number, GHMilestone.class);
|
||||
m = root.retrieve().to("/repos/" + owner.login + "/" + name + "/milestones/" + number, GHMilestone.class);
|
||||
m.owner = this;
|
||||
m.root = root;
|
||||
milestones.put(m.getNumber(), m);
|
||||
|
||||
@@ -46,11 +46,11 @@ public class GHTeam {
|
||||
* Retrieves the current members.
|
||||
*/
|
||||
public Set<GHUser> getMembers() throws IOException {
|
||||
return new HashSet<GHUser>(Arrays.asList(GHUser.wrap(org.root.retrieveWithAuth(api("/members"), GHUser[].class), org.root)));
|
||||
return new HashSet<GHUser>(Arrays.asList(GHUser.wrap(org.root.retrieve().withCredential().to(api("/members"), GHUser[].class), org.root)));
|
||||
}
|
||||
|
||||
public Map<String,GHRepository> getRepositories() throws IOException {
|
||||
GHRepository[] repos = org.root.retrieveWithAuth(api("/repos"), GHRepository[].class);
|
||||
GHRepository[] repos = org.root.retrieve().withCredential().to(api("/repos"), GHRepository[].class);
|
||||
Map<String,GHRepository> m = new TreeMap<String, GHRepository>();
|
||||
for (GHRepository r : repos) {
|
||||
m.put(r.getName(),r.wrap(org.root));
|
||||
@@ -62,22 +62,22 @@ public class GHTeam {
|
||||
* Adds a member to the team.
|
||||
*/
|
||||
public void add(GHUser u) throws IOException {
|
||||
org.root.retrieveWithAuth(api("/members/" + u.getLogin()), null, "PUT");
|
||||
org.root.retrieve().withCredential().method("PUT").to(api("/members/" + u.getLogin()), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a member to the team.
|
||||
*/
|
||||
public void remove(GHUser u) throws IOException {
|
||||
org.root.retrieveWithAuth(api("/members/" + u.getLogin()), null, "DELETE");
|
||||
org.root.retrieve().withCredential().method("DELETE").to(api("/members/" + u.getLogin()), null);
|
||||
}
|
||||
|
||||
public void add(GHRepository r) throws IOException {
|
||||
org.root.retrieveWithAuth(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null, "PUT");
|
||||
org.root.retrieve().withCredential().method("PUT").to(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null);
|
||||
}
|
||||
|
||||
public void remove(GHRepository r) throws IOException {
|
||||
org.root.retrieveWithAuth(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null, "DELETE");
|
||||
org.root.retrieve().withCredential().method("DELETE").to(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null);
|
||||
}
|
||||
|
||||
private String api(String tail) {
|
||||
|
||||
@@ -56,7 +56,7 @@ public class GHUser extends GHPerson {
|
||||
*/
|
||||
@WithBridgeMethods(Set.class)
|
||||
public GHPersonSet<GHUser> getFollows() throws IOException {
|
||||
GHUser[] followers = root.retrieve("/users/" + login + "/following", GHUser[].class);
|
||||
GHUser[] followers = root.retrieve().to("/users/" + login + "/following", GHUser[].class);
|
||||
return new GHPersonSet<GHUser>(Arrays.asList(wrap(followers,root)));
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class GHUser extends GHPerson {
|
||||
*/
|
||||
@WithBridgeMethods(Set.class)
|
||||
public GHPersonSet<GHUser> getFollowers() throws IOException {
|
||||
GHUser[] followers = root.retrieve("/users/" + login + "/followers", GHUser[].class);
|
||||
GHUser[] followers = root.retrieve().to("/users/" + login + "/followers", GHUser[].class);
|
||||
return new GHPersonSet<GHUser>(Arrays.asList(wrap(followers,root)));
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ public class GHUser extends GHPerson {
|
||||
public GHPersonSet<GHOrganization> getOrganizations() throws IOException {
|
||||
GHPersonSet<GHOrganization> orgs = new GHPersonSet<GHOrganization>();
|
||||
Set<String> names = new HashSet<String>();
|
||||
for (GHOrganization o : root.retrieve("/users/" + login + "/orgs", GHOrganization[].class)) {
|
||||
for (GHOrganization o : root.retrieve().to("/users/" + login + "/orgs", GHOrganization[].class)) {
|
||||
if (names.add(o.getLogin())) // I've seen some duplicates in the data
|
||||
orgs.add(root.getOrganization(o.getLogin()));
|
||||
}
|
||||
|
||||
@@ -174,29 +174,6 @@ public class GitHub {
|
||||
return new Poster(this).method("GET");
|
||||
}
|
||||
|
||||
/*package*/ <T> T retrieve(String tailApiUrl, Class<T> type) throws IOException {
|
||||
return _retrieve(tailApiUrl, type, "GET", false);
|
||||
}
|
||||
|
||||
/*package*/ <T> T retrieveWithAuth(String tailApiUrl, Class<T> type) throws IOException {
|
||||
return _retrieve(tailApiUrl, type, "GET", true);
|
||||
}
|
||||
|
||||
/*package*/ <T> T retrieveWithAuth(String tailApiUrl, Class<T> type, String method) throws IOException {
|
||||
return _retrieve(tailApiUrl, type, method, true);
|
||||
}
|
||||
|
||||
private <T> T _retrieve(String tailApiUrl, Class<T> type, String method, boolean withAuth) throws IOException {
|
||||
while (true) {// loop while API rate limit is hit
|
||||
HttpURLConnection uc = setupConnection(method, withAuth, getApiURL(tailApiUrl));
|
||||
try {
|
||||
return parse(uc,type);
|
||||
} catch (IOException e) {
|
||||
handleApiError(e,uc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads pagenated resources.
|
||||
*
|
||||
@@ -359,7 +336,7 @@ public class GitHub {
|
||||
* Gets the current rate limit.
|
||||
*/
|
||||
public GHRateLimit getRateLimit() throws IOException {
|
||||
return retrieveWithAuth("/rate_limit", JsonRateLimit.class).rate;
|
||||
return retrieve().withCredential().to("/rate_limit", JsonRateLimit.class).rate;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -369,7 +346,7 @@ public class GitHub {
|
||||
public GHMyself getMyself() throws IOException {
|
||||
requireCredential();
|
||||
|
||||
GHMyself u = retrieveWithAuth("/user", GHMyself.class);
|
||||
GHMyself u = retrieve().withCredential().to("/user", GHMyself.class);
|
||||
|
||||
u.root = this;
|
||||
users.put(u.getLogin(), u);
|
||||
@@ -406,7 +383,7 @@ public class GitHub {
|
||||
public GHOrganization getOrganization(String name) throws IOException {
|
||||
GHOrganization o = orgs.get(name);
|
||||
if (o==null) {
|
||||
o = retrieve("/orgs/" + name, GHOrganization.class).wrapUp(this);
|
||||
o = retrieve().to("/orgs/" + name, GHOrganization.class).wrapUp(this);
|
||||
orgs.put(name,o);
|
||||
}
|
||||
return o;
|
||||
@@ -429,7 +406,7 @@ public class GitHub {
|
||||
* TODO: make this automatic.
|
||||
*/
|
||||
public Map<String, GHOrganization> getMyOrganizations() throws IOException {
|
||||
GHOrganization[] orgs = retrieveWithAuth("/user/orgs", GHOrganization[].class);
|
||||
GHOrganization[] orgs = retrieve().withCredential().to("/user/orgs", GHOrganization[].class);
|
||||
Map<String, GHOrganization> r = new HashMap<String, GHOrganization>();
|
||||
for (GHOrganization o : orgs) {
|
||||
// don't put 'o' into orgs because they are shallow
|
||||
@@ -443,7 +420,7 @@ public class GitHub {
|
||||
*/
|
||||
public List<GHEventInfo> getEvents() throws IOException {
|
||||
// TODO: pagenation
|
||||
GHEventInfo[] events = retrieve("/events", GHEventInfo[].class);
|
||||
GHEventInfo[] events = retrieve().to("/events", GHEventInfo[].class);
|
||||
for (GHEventInfo e : events)
|
||||
e.wrapUp(this);
|
||||
return Arrays.asList(events);
|
||||
@@ -480,7 +457,7 @@ public class GitHub {
|
||||
*/
|
||||
public boolean isCredentialValid() throws IOException {
|
||||
try {
|
||||
retrieveWithAuth("/user", GHUser.class);
|
||||
retrieve().withCredential().to("/user", GHUser.class);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user