mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-10 08:21:21 +00:00
simplified a bit
This commit is contained in:
@@ -43,7 +43,7 @@ public class GHOrganization extends GHPerson {
|
||||
* Teams by their names.
|
||||
*/
|
||||
public Map<String,GHTeam> getTeams() throws IOException {
|
||||
return root.retrieveWithAuth(root.getApiURL("/organizations/"+login+"/teams"),JsonTeams.class).toMap(this);
|
||||
return root.retrieveWithAuth("/organizations/"+login+"/teams",JsonTeams.class).toMap(this);
|
||||
}
|
||||
|
||||
public enum Permission { ADMIN, PUSH, PULL }
|
||||
@@ -56,7 +56,7 @@ public class GHOrganization extends GHPerson {
|
||||
for (GHRepository r : repositories) {
|
||||
post.with("team[repo_names][]",r.getOwnerName()+'/'+r.getName());
|
||||
}
|
||||
return post.to(root.getApiURL("/organizations/"+login+"/teams"),JsonTeam.class).wrap(this);
|
||||
return post.to("/organizations/"+login+"/teams",JsonTeam.class).wrap(this);
|
||||
}
|
||||
|
||||
public GHTeam createTeam(String name, Permission p, GHRepository... repositories) throws IOException {
|
||||
|
||||
@@ -138,7 +138,7 @@ public class GHRepository {
|
||||
private void modifyCollaborators(Collection<GHUser> users, String op) throws IOException {
|
||||
verifyMine();
|
||||
for (GHUser user : users) {
|
||||
new Poster(root).withCredential().to(root.getApiURL("/repos/collaborators/"+name+ op +user.getLogin()));
|
||||
new Poster(root).withCredential().to("/repos/collaborators/"+name+ op +user.getLogin());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ public class GHRepository {
|
||||
*/
|
||||
public void delete() throws IOException {
|
||||
Poster poster = new Poster(root).withCredential();
|
||||
URL url = root.getApiURL("/repos/delete/" + owner +"/"+name);
|
||||
String url = "/repos/delete/" + owner +"/"+name;
|
||||
|
||||
DeleteToken token = poster.to(url, DeleteToken.class);
|
||||
poster.with("delete_token",token.delete_token).to(url);
|
||||
@@ -157,7 +157,7 @@ public class GHRepository {
|
||||
* Forks this repository.
|
||||
*/
|
||||
public GHRepository fork() throws IOException {
|
||||
return new Poster(root).withCredential().to(root.getApiURL("/repos/fork/" + owner + "/" + name), JsonRepository.class).wrap(root);
|
||||
return new Poster(root).withCredential().to("/repos/fork/" + owner + "/" + name, JsonRepository.class).wrap(root);
|
||||
}
|
||||
|
||||
private void verifyMine() throws IOException {
|
||||
|
||||
@@ -31,29 +31,29 @@ public class GHTeam {
|
||||
* Retrieves the current members.
|
||||
*/
|
||||
public Set<GHUser> getMembers() throws IOException {
|
||||
return org.root.retrieveWithAuth(getApiURL("/members"),JsonUsersWithDetails.class).toSet(org.root);
|
||||
return org.root.retrieveWithAuth("/members",JsonUsersWithDetails.class).toSet(org.root);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a member to the team.
|
||||
*/
|
||||
public void add(GHUser u) throws IOException {
|
||||
org.root.retrieveWithAuth(getApiURL("/members?name="+u.getLogin()),null, "POST");
|
||||
org.root.retrieveWithAuth("/members?name="+u.getLogin(),null, "POST");
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a member to the team.
|
||||
*/
|
||||
public void remove(GHUser u) throws IOException {
|
||||
org.root.retrieveWithAuth(getApiURL("/members?name="+u.getLogin()),null, "DELETE");
|
||||
org.root.retrieveWithAuth("/members?name="+u.getLogin(),null, "DELETE");
|
||||
}
|
||||
|
||||
public void add(GHRepository r) throws IOException {
|
||||
org.root.retrieveWithAuth(getApiURL("/repositories?name="+r.getOwnerName()+'/'+r.getName()),null, "POST");
|
||||
org.root.retrieveWithAuth("/repositories?name="+r.getOwnerName()+'/'+r.getName(),null, "POST");
|
||||
}
|
||||
|
||||
public void remove(GHRepository r) throws IOException {
|
||||
org.root.retrieveWithAuth(getApiURL("/repositories?name="+r.getOwnerName()+'/'+r.getName()),null, "DELETE");
|
||||
org.root.retrieveWithAuth("/repositories?name="+r.getOwnerName()+'/'+r.getName(),null, "DELETE");
|
||||
}
|
||||
|
||||
private URL getApiURL(String tail) throws IOException {
|
||||
|
||||
@@ -108,14 +108,14 @@ public class GHUser extends GHPerson {
|
||||
* Follow this user.
|
||||
*/
|
||||
public void follow() throws IOException {
|
||||
new Poster(root).withCredential().to(root.getApiURL("/user/follow/"+login));
|
||||
new Poster(root).withCredential().to("/user/follow/"+login);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unfollow this user.
|
||||
*/
|
||||
public void unfollow() throws IOException {
|
||||
new Poster(root).withCredential().to(root.getApiURL("/user/unfollow/"+login));
|
||||
new Poster(root).withCredential().to("/user/unfollow/"+login);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -106,19 +106,20 @@ public class GitHub {
|
||||
throw new IllegalStateException("This operation requires a credential but none is given to the GitHub constructor");
|
||||
}
|
||||
|
||||
/*package*/ URL getApiURL(String tail) throws IOException {
|
||||
return new URL("http://github.com/api/v2/json"+tail);
|
||||
/*package*/ URL getApiURL(String tailApiUrl) throws IOException {
|
||||
return new URL("http://github.com/api/v2/json"+tailApiUrl);
|
||||
}
|
||||
|
||||
/*package*/ <T> T retrieve(String tail, Class<T> type) throws IOException {
|
||||
return MAPPER.readValue(getApiURL(tail),type);
|
||||
/*package*/ <T> T retrieve(String tailApiUrl, Class<T> type) throws IOException {
|
||||
return MAPPER.readValue(getApiURL(tailApiUrl),type);
|
||||
}
|
||||
|
||||
/*package*/ <T> T retrieveWithAuth(URL url, Class<T> type) throws IOException {
|
||||
return retrieveWithAuth(url,type,"GET");
|
||||
/*package*/ <T> T retrieveWithAuth(String tailApiUrl, Class<T> type) throws IOException {
|
||||
return retrieveWithAuth(tailApiUrl,type,"GET");
|
||||
}
|
||||
/*package*/ <T> T retrieveWithAuth(URL url, Class<T> type, String method) throws IOException {
|
||||
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
|
||||
|
||||
/*package*/ <T> T retrieveWithAuth(String tailApiUrl, Class<T> type, String method) throws IOException {
|
||||
HttpURLConnection uc = (HttpURLConnection) getApiURL(tailApiUrl).openConnection();
|
||||
|
||||
uc.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
|
||||
uc.setRequestMethod(method);
|
||||
@@ -141,7 +142,7 @@ public class GitHub {
|
||||
public GHUser getUser(String login) throws IOException {
|
||||
GHUser u = users.get(login);
|
||||
if (u==null) {
|
||||
u = MAPPER.readValue(getApiURL("/user/show/"+login), JsonUser.class).user;
|
||||
u = retrieve("/user/show/"+login,JsonUser.class).user;
|
||||
u.root = this;
|
||||
users.put(login,u);
|
||||
}
|
||||
@@ -164,7 +165,7 @@ public class GitHub {
|
||||
public GHOrganization getOrganization(String name) throws IOException {
|
||||
GHOrganization o = orgs.get(name);
|
||||
if (o==null) {
|
||||
o = MAPPER.readValue(getApiURL("/organizations/"+name), JsonOrganization.class).organization;
|
||||
o = retrieve("/organizations/"+name,JsonOrganization.class).organization;
|
||||
o.root = this;
|
||||
orgs.put(name,o);
|
||||
}
|
||||
@@ -188,7 +189,7 @@ public class GitHub {
|
||||
public GHRepository createRepository(String name, String description, String homepage, boolean isPublic) throws IOException {
|
||||
return new Poster(this).withCredential()
|
||||
.with("name", name).with("description", description).with("homepage", homepage)
|
||||
.with("public", isPublic ? 1 : 0).to(getApiURL("/repos/create"), JsonRepository.class).wrap(this);
|
||||
.with("public", isPublic ? 1 : 0).to("/repos/create", JsonRepository.class).wrap(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -196,7 +197,7 @@ public class GitHub {
|
||||
*/
|
||||
public boolean isCredentialValid() throws IOException {
|
||||
try {
|
||||
retrieveWithAuth(getApiURL("/user/show"),JsonUser.class);
|
||||
retrieveWithAuth("/user/show",JsonUser.class);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
|
||||
@@ -72,6 +72,14 @@ class Poster {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void to(String tailApiUrl) throws IOException {
|
||||
to(root.getApiURL(tailApiUrl));
|
||||
}
|
||||
|
||||
public <T> T to(String tailApiUrl, Class<T> type) throws IOException {
|
||||
return to(root.getApiURL(tailApiUrl),type);
|
||||
}
|
||||
|
||||
public void to(URL url) throws IOException {
|
||||
to(url,null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user