Commit aecdac90 authored by Greg Messner's avatar Greg Messner
Browse files

Refactored HTTP method implementations.

parent 2b2aaf0e
package com.messners.gitlab.api;
import java.io.IOException;
import java.net.URL;
import javax.ws.rs.core.MultivaluedMap;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.representation.Form;
public abstract class AbstractApi {
......@@ -28,16 +25,27 @@ public abstract class AbstractApi {
* Perform an HTTP GET call with the specified query parameters and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws IOException
* @throws GitLabApiException
*/
protected ClientResponse get (MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws UniformInterfaceException, ClientHandlerException, IOException {
return (getApiClient().get(queryParams, pathArgs));
protected ClientResponse get (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws GitLabApiException {
try {
ClientResponse response = getApiClient().get(queryParams, pathArgs);
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
}
......@@ -45,15 +53,27 @@ public abstract class AbstractApi {
* Perform an HTTP GET call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws GitLabApiException
*/
protected ClientResponse get (MultivaluedMap<String, String> queryParams, URL url)
throws UniformInterfaceException, ClientHandlerException {
return (getApiClient().get(queryParams, url));
protected ClientResponse get (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url)
throws GitLabApiException {
try {
ClientResponse response = getApiClient().get(queryParams, url);
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
}
......@@ -61,16 +81,26 @@ public abstract class AbstractApi {
* Perform an HTTP POST call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param formData
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws IOException
* @throws GitLabApiException
*/
protected ClientResponse post (Form formData, Object ... pathArgs)
throws UniformInterfaceException, ClientHandlerException, IOException {
return (getApiClient().post(formData, pathArgs));
protected ClientResponse post (ClientResponse.Status expectedStatus, Form formData, Object ... pathArgs) throws GitLabApiException {
try {
ClientResponse response = getApiClient().post(formData, pathArgs);
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
}
......@@ -78,14 +108,26 @@ public abstract class AbstractApi {
* Perform an HTTP POST call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param formData
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws GitLabApiException
*/
protected ClientResponse post (Form formData, URL url) throws UniformInterfaceException, ClientHandlerException {
return (getApiClient().post(formData, url));
protected ClientResponse post (ClientResponse.Status expectedStatus, Form formData, URL url) throws GitLabApiException {
try {
ClientResponse response = getApiClient().post(formData, url);
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
}
......@@ -94,16 +136,26 @@ public abstract class AbstractApi {
* Perform an HTTP PUT call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws IOException
* @throws GitLabApiException
*/
protected ClientResponse put (MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws UniformInterfaceException, ClientHandlerException, IOException {
return (getApiClient().put(queryParams, pathArgs));
protected ClientResponse put (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs) throws GitLabApiException {
try {
ClientResponse response = getApiClient().put(queryParams, pathArgs);
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
}
......@@ -111,33 +163,54 @@ public abstract class AbstractApi {
* Perform an HTTP PUT call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws GitLabApiException
*/
protected ClientResponse put (MultivaluedMap<String, String> queryParams, URL url)
throws UniformInterfaceException, ClientHandlerException {
return (getApiClient().put(queryParams, url));
}
protected ClientResponse put (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
try {
ClientResponse response = getApiClient().put(queryParams, url);
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
}
/**
* Perform an HTTP DELETE call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws IOException
* @throws GitLabApiException
*/
protected ClientResponse delete (MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws UniformInterfaceException, ClientHandlerException, IOException {
return (getApiClient().delete(queryParams, pathArgs));
protected ClientResponse delete (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws GitLabApiException {
try {
ClientResponse response = getApiClient().delete(queryParams, pathArgs);
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
}
......@@ -145,15 +218,37 @@ public abstract class AbstractApi {
* Perform an HTTP DELETE call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws GitLabApiException
*/
protected ClientResponse delete (ClientResponse.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
try {
ClientResponse response = getApiClient().delete(queryParams, url);
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
}
/**
*
* @param formData
* @param string
* @param email
*/
protected ClientResponse delete (MultivaluedMap<String, String> queryParams, URL url)
throws UniformInterfaceException, ClientHandlerException {
return (getApiClient().delete(queryParams, url));
protected void addFormParam(Form formData, String name, Object value) throws IllegalArgumentException {
addFormParam(formData, name, value, false);
}
......
package com.messners.gitlab.api;
import java.io.IOException;
import java.util.List;
import com.messners.gitlab.api.models.Commit;
......@@ -19,9 +18,10 @@ public class CommitsApi extends AbstractApi {
*
* @param branch
* @return
* @throws GitLabApiException
*/
public List<Commit> getCommits (int projectId, String branch) throws IOException {
ClientResponse response = get(null, "projects", projectId, "repository", "commits", branch);
public List<Commit> getCommits (int projectId, String branch) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "repository", "commits", branch);
return (response.getEntity(new GenericType<List<Commit>>() {}));
}
}
package com.messners.gitlab.api;
import java.io.IOException;
import java.util.List;
import com.messners.gitlab.api.models.Group;
import com.messners.gitlab.api.models.Member;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.representation.Form;
public class GroupApi extends AbstractApi {
......@@ -16,67 +16,109 @@ public class GroupApi extends AbstractApi {
/**
* Get a list of groups. (As user: my groups, as admin: all groups)
*
* GET /groups
*
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<Group> getGroups () throws IOException {
ClientResponse response = get(null, "groups");
return (response.getEntity(new GenericType<List<Group>>() {}));
public List<Group> getGroups () throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "groups");
return (response.getEntity(new GenericType<List<Group>>() {}));
}
/**
* Get all details of a group.
*
* GET /groups/:id
*
* @param groupId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public Group getGroup (int groupId) throws IOException {
ClientResponse response = get(null, "groups", groupId);
public Group getGroup (int groupId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "groups", groupId);
return (response.getEntity(Group.class));
}
/**
* Removes group with all projects inside.
*
* DELETE /groups/:id
*
* @param groupId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public boolean deleteGroup (Integer groupId) throws IOException {
public void deleteGroup (Integer groupId) throws GitLabApiException {
if (groupId == null) {
throw new RuntimeException("groupId cannot be null");
}
ClientResponse response = delete(null, "groups", groupId);
return (response.getStatus() == ClientResponse.Status.OK.getStatusCode());
delete(ClientResponse.Status.OK, null, "groups", groupId);
}
/**
* Removes group with all projects inside.
*
* DELETE /groups/:id
*
* @param group
* @return
* @throws IOException
* @throws GitLabApiException
*/
public boolean deleteGroup (Group group) throws IOException {
return (deleteGroup(group.getId()));
public void deleteGroup (Group group) throws GitLabApiException {
deleteGroup(group.getId());
}
/**
* Get a list of group members viewable by the authenticated user.
*
* GET /groups/:id/members
*
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<Member> getMembers (int groupId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "groups", groupId, "members");
return (response.getEntity(new GenericType<List<Member>>() {}));
}
/**
* Adds a user to the list of group members.
*
* POST /groups/:id/members
*
* @param groupId
* @param userId
* @param accessLevel
* @return
* @throws GitLabApiException
*/
public Member addMember (Integer groupId, Integer userId, Integer accessLevel) throws GitLabApiException {
Form formData = new Form();
formData.add("user_id", userId);
formData.add("access_level", accessLevel);
ClientResponse response = post(ClientResponse.Status.OK, formData, "groups", groupId, "members");
return (response.getEntity(Member.class));
}
/**
* Removes member from the group team.
*
* DELETE /groups/:id/members/:user_id
*
* @param projectId
* @param userId
* @throws GitLabApiException
*/
public List<Member> getMembers (int groupId) throws IOException {
ClientResponse response = get(null, "groups", groupId, "members");
return (response.getEntity(new GenericType<List<Member>>() {}));
public void removeMember (Integer projectId, Integer userId) throws GitLabApiException {
delete(ClientResponse.Status.OK, null, "groups", projectId, "members", userId);
}
}
package com.messners.gitlab.api;
import java.io.IOException;
import java.util.List;
import com.messners.gitlab.api.models.ErrorMessage;
import com.messners.gitlab.api.models.MergeRequest;
import com.messners.gitlab.api.models.MergeRequestComment;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.representation.Form;
public class MergeRequestApi extends AbstractApi {
......@@ -13,31 +13,43 @@ public class MergeRequestApi extends AbstractApi {
MergeRequestApi (GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* POST /projects/:id/merge_request/:merge_request_id/comments
* Get all merge requests for this project.
*
* GET /projects/:id/merge_requests
*
* @param projectId
* @return
* @throws GitLabApiException
*/
public List<MergeRequest> getMergeRequests (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "merge_requests");
return (response.getEntity(new GenericType<List<MergeRequest>>() {}));
}
/**
* Get information about a single merge request.
*
* GET /projects/:id/merge_request/:merge_request_id
*
* @param projectId
* @param mergeRequestId
* @param comments
* @throws IOException
* @return
* @throws GitLabApiException
*/
public MergeRequestComment addMergeRequestComment (Integer projectId, Integer mergeRequestId, String comments) throws IOException {
Form formData = new Form();
formData.add("note", comments);
ClientResponse response = post(formData, "projects", projectId, "merge_request", mergeRequestId, "comments");
if (response.getStatus() != ClientResponse.Status.CREATED.getStatusCode()) {
ErrorMessage errorMessage = response.getEntity(ErrorMessage.class);
throw new IOException(errorMessage.getMessage());
}
return (response.getEntity(MergeRequestComment.class));
public MergeRequest getMergeRequest (Integer projectId, Integer mergeRequestId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "merge_request", mergeRequestId);
return (response.getEntity(MergeRequest.class));
}
/**
* Creates a merge request and optionally assignes it.
* Creates a merge request and optionally assigns a reviewer to it.
*
* POST /projects/:id/merge_requests
*
* @param projectId the ID of a project, required
* @param sourceBranch the source branch, required
......@@ -46,43 +58,80 @@ public class MergeRequestApi extends AbstractApi {
* @param description the description of the merge request
* @param assigneeId the Assignee user ID, optional
* @return the created MergeRequest instance
* @throws IOException
* @throws GitLabApiException
*/
public MergeRequest createMergeRequest (Integer projectId, String sourceBranch, String targetBranch, String title, String description, Integer assigneeId)
throws IOException {
throws GitLabApiException {
/*
* Parameters:
* id (required) - The ID of a project
* source_branch (required) - The source branch
* target_branch (required) - The target branch
* assignee_id (optional) - Assignee user ID
* title (required) - Title of MR
*/
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Form formData = new Form();
formData.add("source_branch", sourceBranch);
formData.add("target_branch", targetBranch);
formData.add("title", title);
addFormParam(formData, "source_branch", sourceBranch, true);
addFormParam(formData, "target_branch", targetBranch, true);
addFormParam(formData, "title", title, true);
addFormParam(formData, "description", description, false);
addFormParam(formData, "assignee_id", assigneeId, false);
if (description != null) {
formData.add("description", description);
}
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "projects", projectId, "merge_requests");
return (response.getEntity(MergeRequest.class));
}
/**
* Updates an existing merge request. You can change branches, title, or even close the MR.
*
* PUT /projects/:id/merge_request/:merge_request_id
*
* @param projectId
* @param mergeRequestId
* @param sourceBranch
* @param targetBranch
* @param title
* @param description
* @param assigneeId
* @return
* @throws GitLabApiException
*/
public MergeRequest updateMergeRequest (Integer projectId, Integer mergeRequestId,
String sourceBranch, String targetBranch, String title, String description, Integer assigneeId) throws GitLabApiException {
if (assigneeId != null) {
formData.add("assignee_id", assigneeId);
if (projectId == null) {
throw new RuntimeException("mergeRequestId cannot be null");
}
ClientResponse response = post(formData, "projects", projectId, "merge_requests");
if (response.getStatus() != ClientResponse.Status.CREATED.getStatusCode()) {
ErrorMessage errorMessage = response.getEntity(ErrorMessage.class);
throw new IOException(errorMessage.getMessage());
if (mergeRequestId == null) {
throw new RuntimeException("projectId cannot be null");
}
return (response.getEntity(MergeRequest.class));
Form formData = new Form();
addFormParam(formData, "source_branch", sourceBranch, false);
addFormParam(formData, "target_branch", targetBranch, false);
addFormParam(formData, "title", title, false);
addFormParam(formData, "description", description, false);
addFormParam(formData, "assignee_id", assigneeId, false);
ClientResponse response = put(ClientResponse.Status.OK, formData, "projects", projectId, "merge_request", mergeRequestId);
return (response.getEntity(MergeRequest.class));
}
/**
* Adds a comment to a merge request.
*
* POST /projects/:id/merge_request/:merge_request_id/comments
*
* @param projectId
* @param mergeRequestId
* @param comments
* @throws GitLabApiException
*/
public MergeRequestComment addMergeRequestComment (Integer projectId, Integer mergeRequestId, String comments) throws GitLabApiException {
Form formData = new Form();
formData.add("note", comments);
ClientResponse response = post(ClientResponse.Status.OK, formData, "projects", projectId, "merge_request", mergeRequestId, "comments");
return (response.getEntity(MergeRequestComment.class));
}
}
package com.messners.gitlab.api;
import java.io.IOException;
import java.util.List;
import com.messners.gitlab.api.models.ErrorMessage;
import com.messners.gitlab.api.models.Event;
import com.messners.gitlab.api.models.Member;
import com.messners.gitlab.api.models.Project;
......@@ -25,11 +23,11 @@ public class ProjectApi extends AbstractApi {
* GET /projects
*
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<Project> getProjects () throws IOException {
ClientResponse response = get(null, "projects");
return (response.getEntity(new GenericType<List<Project>>() {}));
public List<Project> getProjects () throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects");
return (response.getEntity(new GenericType<List<Project>>() {}));
}
......@@ -39,11 +37,11 @@ public class ProjectApi extends AbstractApi {
* GET /projects/all
*
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<Project> getAllProjects () throws IOException {
ClientResponse response = get(null, "projects", "all");
return (response.getEntity(new GenericType<List<Project>>() {}));
public List<Project> getAllProjects () throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", "all");
return (response.getEntity(new GenericType<List<Project>>() {}));
}
......@@ -53,11 +51,11 @@ public class ProjectApi extends AbstractApi {
* GET /projects/owned
*
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<Project> getOwnedProjects () throws IOException {
ClientResponse response = get(null, "projects", "owned");
return (response.getEntity(new GenericType<List<Project>>() {}));
public List<Project> getOwnedProjects () throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", "owned");
return (response.getEntity(new GenericType<List<Project>>() {}));
}
......@@ -68,13 +66,32 @@ public class ProjectApi extends AbstractApi {
*
* @param projectId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public Project getProject (Integer projectId) throws IOException {
ClientResponse response = get(null, "projects", projectId);
public Project getProject (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId);
return (response.getEntity(Project.class));
}
/**
* Create a new project in the specified group.
*
* @param groupId
* @param projectName
* @return
* @throws GitLabApiException
*/
public Project createProject (Integer groupId, String projectName) throws GitLabApiException {
Form formData = new Form();
addFormParam(formData, "namespace_id", groupId);
addFormParam(formData, "name", projectName, true);
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "projects");
return (response.getEntity(Project.class));
}
/**
* Creates new project owned by the current user.
......@@ -82,8 +99,9 @@ public class ProjectApi extends AbstractApi {
* @param project the Project instance with the configuration for the new project
* @return a Project instance with the newly created project info
* @throws IOException
* @throws GitLabApiException
*/
public Project createProject (Project project) throws IOException {
public Project createProject (Project project) throws GitLabApiException {
return (createProject(project, null));
}
......@@ -104,9 +122,9 @@ public class ProjectApi extends AbstractApi {
* @param project the Project instance with the configuration for the new project
* @param importUrl
* @return a Project instance with the newly created project info
* @throws IOException
* @throws GitLabApiException
*/
public Project createProject (Project project, String importUrl) throws IOException {
public Project createProject (Project project, String importUrl) throws GitLabApiException {
if (project == null) {
return (null);
......@@ -118,23 +136,22 @@ public class ProjectApi extends AbstractApi {
}
Form formData = new Form();
formData.add("name", name);
formData.add("description", project.getDescription());
formData.add("issues_enabled", project.getIssuesEnabled());
formData.add("wall_enabled", project.getWallEnabled());
formData.add("merge_requests_enabled", project.getMergeRequestsEnabled());
formData.add("wiki_enabled", project.getWikiEnabled());
formData.add("snippets_enabled", project.getSnippetsEnabled());
formData.add("public", project.getPublic());
formData.add("visibility_level", project.getVisibilityLevel());
formData.add("import_url", importUrl);
ClientResponse response = post(formData, "projects");
if (response.getStatus() != ClientResponse.Status.CREATED.getStatusCode()) {
ErrorMessage errorMessage = response.getEntity(ErrorMessage.class);
throw new RuntimeException(errorMessage.getMessage());
}
if (project.getNamespace() != null) {
addFormParam(formData, "namespace_id", project.getNamespace().getId());
}
addFormParam(formData, "name", name, true);
addFormParam(formData, "description", project.getDescription());
addFormParam(formData, "issues_enabled", project.getIssuesEnabled());
addFormParam(formData, "wall_enabled", project.getWallEnabled());
addFormParam(formData, "merge_requests_enabled", project.getMergeRequestsEnabled());
addFormParam(formData, "wiki_enabled", project.getWikiEnabled());
addFormParam(formData, "snippets_enabled", project.getSnippetsEnabled());
addFormParam(formData, "public", project.getPublic());
addFormParam(formData, "visibility_level", project.getVisibilityLevel());
addFormParam(formData, "import_url", importUrl);
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "projects");
return (response.getEntity(Project.class));
}
......@@ -145,17 +162,15 @@ public class ProjectApi extends AbstractApi {
* DELETE /projects/:id
*
* @param projectId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public boolean deleteProject (Integer projectId) throws IOException {
public void deleteProject (Integer projectId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
ClientResponse response = delete(null, "projects", projectId);
return (response.getStatus() == ClientResponse.Status.OK.getStatusCode());
delete(ClientResponse.Status.OK, null, "projects", projectId);
}
......@@ -165,11 +180,10 @@ public class ProjectApi extends AbstractApi {
* DELETE /projects/:id
*
* @param project
* @return
* @throws IOException
* @throws GitLabApiException
*/
public boolean deleteProject (Project project) throws IOException {
return (deleteProject(project.getId()));
public void deleteProject (Project project) throws GitLabApiException {
deleteProject(project.getId());
}
......@@ -180,10 +194,10 @@ public class ProjectApi extends AbstractApi {
*
* @param projectId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<Member> getMembers (Integer projectId) throws IOException {
ClientResponse response = get(null, "project", projectId, "members");
public List<Member> getMembers (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "members");
return (response.getEntity(new GenericType<List<Member>>() {}));
}
......@@ -196,10 +210,10 @@ public class ProjectApi extends AbstractApi {
* @param projectId
* @param userId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public Member getMember (Integer projectId, Integer userId) throws IOException {
ClientResponse response = get(null, "project", projectId, "members", userId);
public Member getMember (Integer projectId, Integer userId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "members", userId);
return (response.getEntity(Member.class));
}
......@@ -215,14 +229,14 @@ public class ProjectApi extends AbstractApi {
* @param userId
* @param accessLevel
* @return
* @throws IOException
* @throws GitLabApiException
*/
public Member addMember (Integer projectId, Integer userId, Integer accessLevel) throws IOException {
public Member addMember (Integer projectId, Integer userId, Integer accessLevel) throws GitLabApiException {
Form formData = new Form();
formData.add("user_id", userId);
formData.add("access_level", accessLevel);
ClientResponse response = post(formData, "project", projectId, "members");
ClientResponse response = post(ClientResponse.Status.OK, formData, "projects", projectId, "members");
return (response.getEntity(Member.class));
}
......@@ -234,10 +248,10 @@ public class ProjectApi extends AbstractApi {
*
* @param projectId
* @param userId
* @throws IOException
* @throws GitLabApiException
*/
public boolean removeMember (Integer projectId, Integer userId) throws IOException {
ClientResponse response = delete(null, "project", projectId, "members", userId);
public boolean removeMember (Integer projectId, Integer userId) throws GitLabApiException {
ClientResponse response = delete(ClientResponse.Status.OK, null, "projects", projectId, "members", userId);
return (response.getStatus() == ClientResponse.Status.OK.getStatusCode());
}
......@@ -249,10 +263,10 @@ public class ProjectApi extends AbstractApi {
*
* @param projectId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<Event> getProjectEvents (Integer projectId) throws IOException {
ClientResponse response = get(null, "project", projectId, "events");
public List<Event> getProjectEvents (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "events");
return (response.getEntity(new GenericType<List<Event>>() {}));
}
......@@ -264,11 +278,11 @@ public class ProjectApi extends AbstractApi {
*
* @param projectId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<ProjectHook> getHooks (Integer projectId) throws IOException {
ClientResponse response = get(null, "projects", projectId, "hooks");
return (response.getEntity(new GenericType<List<ProjectHook>>() {} ));
public List<ProjectHook> getHooks (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "hooks");
return (response.getEntity(new GenericType<List<ProjectHook>>() {} ));
}
......@@ -280,11 +294,11 @@ public class ProjectApi extends AbstractApi {
* @param projectId
* @param hookId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public ProjectHook getHook (Integer projectId, Integer hookId) throws IOException {
ClientResponse response = get(null, "projects", projectId, "hooks", hookId);
return (response.getEntity(ProjectHook.class));
public ProjectHook getHook (Integer projectId, Integer hookId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "hooks", hookId);
return (response.getEntity(ProjectHook.class));
}
......@@ -299,11 +313,11 @@ public class ProjectApi extends AbstractApi {
* @param doIssuesEvents
* @param doMergeRequestsEvents
* @return
* @throws IOException
* @throws GitLabApiException
*/
public ProjectHook addHook (Project project, String url,
boolean doPushEvents, boolean doIssuesEvents, boolean doMergeRequestsEvents)
throws IOException {
throws GitLabApiException {
if (project == null) {
return (null);
......@@ -324,11 +338,11 @@ public class ProjectApi extends AbstractApi {
* @param doIssuesEvents
* @param doMergeRequestsEvents
* @return
* @throws IOException
* @throws GitLabApiException
*/
public ProjectHook addHook (Integer projectId, String url,
boolean doPushEvents, boolean doIssuesEvents, boolean doMergeRequestsEvents)
throws IOException {
throws GitLabApiException {
Form formData = new Form();
formData.add("url", url);
......@@ -336,12 +350,7 @@ public class ProjectApi extends AbstractApi {
formData.add("issues_enabled", doIssuesEvents);
formData.add("merge_requests_events", doMergeRequestsEvents);
ClientResponse response = post(formData, "projects", projectId, "hooks");
if (response.getStatus() != ClientResponse.Status.CREATED.getStatusCode()) {
ErrorMessage errorMessage = response.getEntity(ErrorMessage.class);
throw new RuntimeException(errorMessage.getMessage());
}
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "projects", projectId, "hooks");
return (response.getEntity(ProjectHook.class));
}
......@@ -357,10 +366,10 @@ public class ProjectApi extends AbstractApi {
* @param doIssuesEvents
* @param doMergeRequestsEvents
* @return
* @throws IOException
* @throws GitLabApiException
*/
public ProjectHook modifyHook (ProjectHook hook)
throws IOException {
throws GitLabApiException {
Form formData = new Form();
formData.add("url", hook.getUrl());
......@@ -368,7 +377,7 @@ public class ProjectApi extends AbstractApi {
formData.add("issues_enabled", hook.getIssuesEvents());
formData.add("merge_requests_events", hook.getMergeRequestsEvents());
ClientResponse response = put(formData, "projects", hook.getProjectId(), "hooks", hook.getId());
ClientResponse response = put(ClientResponse.Status.OK, formData, "projects", hook.getProjectId(), "hooks", hook.getId());
return (response.getEntity(ProjectHook.class));
}
}
package com.messners.gitlab.api;
import java.io.IOException;
import java.util.List;
import com.messners.gitlab.api.models.Branch;
......@@ -8,6 +7,7 @@ import com.messners.gitlab.api.models.Tag;
import com.messners.gitlab.api.models.TreeItem;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.representation.Form;
/**
......@@ -23,97 +23,115 @@ public class RepositoryApi extends AbstractApi {
/**
* Get a list of repository branches from a project, sorted by name alphabetically.
*
* GET /projects/:id/repository/branches
*
* @param projectId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<Branch> getBranches (Integer projectId) throws IOException {
ClientResponse response = get(null, "projects", projectId, "repository", "branches");
public List<Branch> getBranches (Integer projectId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "repository", "branches");
return (response.getEntity(new GenericType<List<Branch>>() {}));
}
/**
* Get a single project repository branch.
*
* GET /projects/:id/repository/branches/:branch
*
* @param projectId
* @param branchName
* @return
* @throws IOException
* @throws GitLabApiException
*/
public Branch getBranch (Integer projectId, String branchName) throws IOException {
ClientResponse response = get(null, "projects", projectId, "repository", "branches", branchName);
public Branch getBranch (Integer projectId, String branchName) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "projects", projectId, "repository", "branches", branchName);
return (response.getEntity(Branch.class));
}
/**
* Protects a single project repository branch. This is an idempotent function,
* protecting an already protected repository branch will not produce an error.
*
* PUT /projects/:id/repository/branches/:branch/protect
*
* @param projectId
* @param branchName
* @return
* @throws IOException
* @throws GitLabApiException
*/
public Branch protectBranch (Integer projectId, String branchName) throws IOException {
ClientResponse response = put(null, "projects", projectId, "repository", "branches", branchName, "protect");
public Branch protectBranch (Integer projectId, String branchName) throws GitLabApiException {
ClientResponse response = put(ClientResponse.Status.OK, null, "projects", projectId, "repository", "branches", branchName, "protect");
return (response.getEntity(Branch.class));
}
/**
* Unprotects a single project repository branch. This is an idempotent function, unprotecting an
* already unprotected repository branch will not produce an error.
*
* PUT /projects/:id/repository/branches/:branch/unprotect
*
* @param projectId
* @param branchName
* @return
* @throws IOException
* @throws GitLabApiException
*/
public Branch unprotectBranch (Integer projectId, String branchName) throws IOException {
ClientResponse response = put(null, "projects", projectId, "repository", "branches", branchName, "unprotect");
public Branch unprotectBranch (Integer projectId, String branchName) throws GitLabApiException {
ClientResponse response = put(ClientResponse.Status.OK, null, "projects", projectId, "repository", "branches", branchName, "unprotect");
return (response.getEntity(Branch.class));
}
/**
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
*
* GET /projects/:id/repository/tags
*
* @param projectId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<Tag> getTags (Integer projectId) throws IOException {
ClientResponse response = put(null, "projects", projectId, "repository", "tags");
public List<Tag> getTags (Integer projectId) throws GitLabApiException {
ClientResponse response = put(ClientResponse.Status.OK, null, "projects", projectId, "repository", "tags");
return (response.getEntity(new GenericType<List<Tag>>() {}));
}
/**
* Get a list of repository files and directories in a project.
*
* GET /projects/:id/repository/tree
*
* @param projectId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<TreeItem> getTree (Integer projectId) throws IOException {
ClientResponse response = put(null, "projects", projectId, "repository", "tree");
public List<TreeItem> getTree (Integer projectId) throws GitLabApiException {
ClientResponse response = put(ClientResponse.Status.OK, null, "projects", projectId, "repository", "tree");
return (response.getEntity(new GenericType<List<TreeItem>>() {}));
}
/**
* Get the raw file contents for a file by commit sha and path.
*
* GET /projects/:id/repository/blobs/:sha
*
* @param projectId
* @param commitOrBranchName
* @return
* @throws IOException
* @throws GitLabApiException
*/
public String getRawFileContent (Integer projectId, String commitOrBranchName) throws IOException {
ClientResponse response = get(null, "projects", projectId, "repository", "blobs", commitOrBranchName);
public String getRawFileContent (Integer projectId, String commitOrBranchName, String filepath) throws GitLabApiException {
Form formData = new Form();
addFormParam(formData, "filepath", filepath, true);
ClientResponse response = get(ClientResponse.Status.OK, formData, "projects", projectId, "repository", "blobs", commitOrBranchName);
return (response.getEntity(String.class));
}
}
package com.messners.gitlab.api;
import java.io.IOException;
import java.util.List;
import com.messners.gitlab.api.models.ErrorMessage;
import com.messners.gitlab.api.models.User;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
......@@ -17,31 +15,37 @@ public class UserApi extends AbstractApi {
/**
* Get a list of users.
*
* GET /users
*
* @return
* @throws IOException
* @throws GitLabApiException
*/
public List<User> getProjects () throws IOException {
ClientResponse response = get(null, "users");
return (response.getEntity(new GenericType<List<User>>() {}));
public List<User> getProjects () throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "users");
return (response.getEntity(new GenericType<List<User>>() {}));
}
/**
* Get a single user.
*
* GET /users/:id
*
* @param userId
* @return
* @throws IOException
* @throws GitLabApiException
*/
public User getUser (int userId) throws IOException {
ClientResponse response = get(null, "users", userId);
public User getUser (int userId) throws GitLabApiException {
ClientResponse response = get(ClientResponse.Status.OK, null, "users", userId);
return (response.getEntity(User.class));
}
/**
* Creates a new user. Note only administrators can create new users.
*
* POST /users
*
* email (required) - Email
......@@ -61,9 +65,9 @@ public class UserApi extends AbstractApi {
*
* @param user
* @return
* @throws IOException
* @throws GitLabApiException
*/
public User createUser (User user, String password, Integer projectsLimit) throws IOException {
public User createUser (User user, String password, Integer projectsLimit) throws GitLabApiException {
Form formData = new Form();
addFormParam(formData, "email", user.getEmail(), true);
......@@ -81,18 +85,15 @@ public class UserApi extends AbstractApi {
addFormParam(formData, "admin", user.getIsAdmin(), false);
addFormParam(formData, "can_create_group", user.getCanCreateGroup(), false);
ClientResponse response = post(formData, "users");
if (response.getStatus() != ClientResponse.Status.CREATED.getStatusCode()) {
ErrorMessage errorMessage = response.getEntity(ErrorMessage.class);
throw new RuntimeException(errorMessage.getMessage());
}
ClientResponse response = post(ClientResponse.Status.CREATED, formData, "users");
return (response.getEntity(User.class));
}
/**
* POST /users
* Modifies an existing user. Only administrators can change attributes of a user.
*
* PUT /users/:id
*
* email (required) - Email
* password (required) - Password
......@@ -111,9 +112,9 @@ public class UserApi extends AbstractApi {
*
* @param user
* @return
* @throws IOException
* @throws GitLabApiException
*/
public User modifyUser (User user, String password, Integer projectsLimit) throws IOException {
public User modifyUser (User user, String password, Integer projectsLimit) throws GitLabApiException {
Form formData = new Form();
addFormParam(formData, "email", user.getEmail(), false);
......@@ -131,33 +132,38 @@ public class UserApi extends AbstractApi {
addFormParam(formData, "admin", user.getIsAdmin(), false);
addFormParam(formData, "can_create_group", user.getCanCreateGroup(), false);
ClientResponse response = put(formData, "users", user.getId());
ClientResponse response = put(ClientResponse.Status.OK, formData, "users", user.getId());
return (response.getEntity(User.class));
}
/**
* Deletes a user. Available only for administrators.
*
* DELETE /users/:id
*
* @param userId
* @throws GitLabApiException
*/
public boolean deleteUser (Integer userId) throws IOException {
public void deleteUser (Integer userId) throws GitLabApiException {
if (userId == null) {
throw new RuntimeException("userId cannot be null");
}
ClientResponse response = delete(null, "users", userId);
return (response.getStatus() == ClientResponse.Status.OK.getStatusCode());
delete(ClientResponse.Status.OK, null, "users", userId);
}
/**
* Deletes a user. Available only for administrators.
*
* DELETE /users/:id
*
* @param user
* @throws GitLabApiException
*/
public boolean deleteUser (User user) throws IOException {
return (deleteUser(user.getId()));
public void deleteUser (User user) throws GitLabApiException {
deleteUser(user.getId());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment