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

General code cleanup.

parent 882e016e
...@@ -8,6 +8,9 @@ import javax.ws.rs.core.Response; ...@@ -8,6 +8,9 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.User; import org.gitlab4j.api.models.User;
/**
* This class provides an entry point to all the GitLab API users calls.
*/
public class UserApi extends AbstractApi { public class UserApi extends AbstractApi {
UserApi(GitLabApi gitLabApi) { UserApi(GitLabApi gitLabApi) {
...@@ -40,9 +43,9 @@ public class UserApi extends AbstractApi { ...@@ -40,9 +43,9 @@ public class UserApi extends AbstractApi {
*/ */
public List<User> getUsers(int page, int perPage) throws GitLabApiException { public List<User> getUsers(int page, int perPage) throws GitLabApiException {
Form formData = new Form(); GitLabApiForm formData = new GitLabApiForm()
addFormParam(formData, "page", page, false); .withParam("page", page, false)
addFormParam(formData, "per_page", perPage, false); .withParam("per_page", perPage, false);
Response response = get(Response.Status.OK, formData.asMap(), "users"); Response response = get(Response.Status.OK, formData.asMap(), "users");
return (response.readEntity(new GenericType<List<User>>() { return (response.readEntity(new GenericType<List<User>>() {
})); }));
...@@ -72,8 +75,7 @@ public class UserApi extends AbstractApi { ...@@ -72,8 +75,7 @@ public class UserApi extends AbstractApi {
* @throws GitLabApiException * @throws GitLabApiException
*/ */
public List<User> findUsers(String emailOrUsername) throws GitLabApiException { public List<User> findUsers(String emailOrUsername) throws GitLabApiException {
Form formData = new Form(); GitLabApiForm formData = new GitLabApiForm().withParam("search", emailOrUsername, true);
addFormParam(formData, "search", emailOrUsername, true);
Response response = get(Response.Status.OK, formData.asMap(), "users"); Response response = get(Response.Status.OK, formData.asMap(), "users");
return (response.readEntity(new GenericType<List<User>>() { return (response.readEntity(new GenericType<List<User>>() {
})); }));
...@@ -104,7 +106,7 @@ public class UserApi extends AbstractApi { ...@@ -104,7 +106,7 @@ public class UserApi extends AbstractApi {
* @throws GitLabApiException * @throws GitLabApiException
*/ */
public User createUser(User user, String password, Integer projectsLimit) throws GitLabApiException { public User createUser(User user, String password, Integer projectsLimit) throws GitLabApiException {
Form formData = user2form(user, projectsLimit, password, true); Form formData = userToForm(user, projectsLimit, password, true);
Response response = post(Response.Status.CREATED, formData, "users"); Response response = post(Response.Status.CREATED, formData, "users");
return (response.readEntity(User.class)); return (response.readEntity(User.class));
} }
...@@ -134,7 +136,7 @@ public class UserApi extends AbstractApi { ...@@ -134,7 +136,7 @@ public class UserApi extends AbstractApi {
* @throws GitLabApiException * @throws GitLabApiException
*/ */
public User modifyUser(User user, String password, Integer projectsLimit) throws GitLabApiException { public User modifyUser(User user, String password, Integer projectsLimit) throws GitLabApiException {
Form form = user2form(user, projectsLimit, password, false); Form form = userToForm(user, projectsLimit, password, false);
Response response = put(Response.Status.OK, form.asMap(), "users", user.getId()); Response response = put(Response.Status.OK, form.asMap(), "users", user.getId());
return (response.readEntity(User.class)); return (response.readEntity(User.class));
} }
...@@ -168,24 +170,32 @@ public class UserApi extends AbstractApi { ...@@ -168,24 +170,32 @@ public class UserApi extends AbstractApi {
deleteUser(user.getId()); deleteUser(user.getId());
} }
private Form user2form(User user, Integer projectsLimit, String password, boolean isCreate) { /**
Form form = new Form(); * Populate the REST form with data from the User instance.
addFormParam(form, "email", user.getEmail(), isCreate); *
addFormParam(form, "password", password, isCreate); * @param user the User iunstance to populate the Form instance with
addFormParam(form, "username", user.getUsername(), isCreate); * @param projectsLimit the maximum number of projects the user is allowed (optional)
addFormParam(form, "name", user.getName(), isCreate); * @param password the password, required when creating a new user
addFormParam(form, "skype", user.getSkype(), false); * @param create whether the form is being populated to create a new user
addFormParam(form, "linkedin", user.getLinkedin(), false); * @return the populated Form instance
addFormParam(form, "twitter", user.getTwitter(), false); */
addFormParam(form, "website_url", user.getWebsiteUrl(), false); Form userToForm(User user, Integer projectsLimit, String password, boolean create) {
addFormParam(form, "projects_limit", projectsLimit, false); return (new GitLabApiForm()
addFormParam(form, "external", user.getExternal(), false); .withParam("email", user.getEmail(), create)
addFormParam(form, "provider", user.getProvider(), false); .withParam("password", password, create)
addFormParam(form, "bio", user.getBio(), false); .withParam("username", user.getUsername(), create)
addFormParam(form, "location", user.getLocation(), false); .withParam("name", user.getName(), create)
addFormParam(form, "admin", user.getIsAdmin(), false); .withParam("skype", user.getSkype(), false)
addFormParam(form, "can_create_group", user.getCanCreateGroup(), false); .withParam("linkedin", user.getLinkedin(), false)
addFormParam(form, "external", user.getExternal(), false); .withParam("twitter", user.getTwitter(), false)
return form; .withParam("website_url", user.getWebsiteUrl(), false)
.withParam("projects_limit", projectsLimit, false)
.withParam("external", user.getExternal(), false)
.withParam("provider", user.getProvider(), false)
.withParam("bio", user.getBio(), false)
.withParam("location", user.getLocation(), false)
.withParam("admin", user.getIsAdmin(), false)
.withParam("can_create_group", user.getCanCreateGroup(), false)
.withParam("external", user.getExternal(), false));
} }
} }
...@@ -6,6 +6,9 @@ import java.util.Scanner; ...@@ -6,6 +6,9 @@ import java.util.Scanner;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
/**
* This class provides static utility methods used throughout GitLab4J.
*/
public class Utils { public class Utils {
/** /**
...@@ -13,10 +16,10 @@ public class Utils { ...@@ -13,10 +16,10 @@ public class Utils {
* filename exists in the directory, "-#" will be appended to the filename until * filename exists in the directory, "-#" will be appended to the filename until
* a unique filename can be created. * a unique filename can be created.
* *
* @param directory * @param directory the directory to create the file in
* @param filename * @param filename the base filename with extension
* @return a File that is unique in the specified directory * @return a File that is unique in the specified directory
* @throws IOException * @throws IOException if any error occurs during file creation
*/ */
public static File createUniqueFile(File directory, String filename) throws IOException { public static File createUniqueFile(File directory, String filename) throws IOException {
......
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