Commit 1fdccb14 authored by Patrik Beno's avatar Patrik Beno
Browse files

refactoring: simplify / optimize / eliminate duplicates

parent ebe5d24b
......@@ -37,21 +37,12 @@ public abstract class AbstractApi {
*/
protected Response get (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws GitLabApiException {
Response response = null;
try {
response = getApiClient().get(queryParams, pathArgs);
try {
return validate(getApiClient().get(queryParams, pathArgs), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
throw handle(e);
}
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
}
}
/**
* Perform an HTTP GET call with the specified query parameters and URL, returning
......@@ -65,20 +56,12 @@ public abstract class AbstractApi {
*/
protected Response get (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url)
throws GitLabApiException {
Response response = null;
try {
response = getApiClient().get(queryParams, url);
try {
return validate(getApiClient().get(queryParams, url), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
throw handle(e);
}
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
}
}
/**
......@@ -92,19 +75,11 @@ public abstract class AbstractApi {
* @throws GitLabApiException
*/
protected Response post (Response.Status expectedStatus, Form formData, Object ... pathArgs) throws GitLabApiException {
Response response = null;
try {
response = getApiClient().post(formData, pathArgs);
try {
return validate(getApiClient().post(formData, pathArgs), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
throw handle(e);
}
return (response);
}
......@@ -119,19 +94,11 @@ public abstract class AbstractApi {
* @throws GitLabApiException
*/
protected Response post (Response.Status expectedStatus, Form formData, URL url) throws GitLabApiException {
Response response = null;
try {
response = getApiClient().post(formData, url);
try {
return validate(getApiClient().post(formData, url), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
throw handle(e);
}
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
}
......@@ -146,20 +113,12 @@ public abstract class AbstractApi {
* @throws GitLabApiException
*/
protected Response put (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs) throws GitLabApiException {
Response response = null;
try {
response = getApiClient().put(queryParams, pathArgs);
try {
return validate(getApiClient().put(queryParams, pathArgs), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
throw handle(e);
}
return (response);
}
}
/**
......@@ -173,20 +132,12 @@ public abstract class AbstractApi {
* @throws GitLabApiException
*/
protected Response put (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
Response response = null;
try {
response = getApiClient().put(queryParams, url);
try {
return validate(getApiClient().put(queryParams, url), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
throw handle(e);
}
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
}
}
/**
......@@ -201,20 +152,12 @@ public abstract class AbstractApi {
*/
protected Response delete (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object ... pathArgs)
throws GitLabApiException {
Response response = null;
try {
response = getApiClient().delete(queryParams, pathArgs);
try {
return validate(getApiClient().delete(queryParams, pathArgs), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
throw handle(e);
}
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
}
return (response);
}
}
/**
......@@ -228,19 +171,11 @@ public abstract class AbstractApi {
* @throws GitLabApiException
*/
protected Response delete (Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, URL url) throws GitLabApiException {
Response response = null;
try {
response = getApiClient().delete(queryParams, url);
try {
return validate(getApiClient().delete(queryParams, url), expectedStatus);
} catch (Exception e) {
throw (new GitLabApiException(e));
}
if (response.getStatus() != expectedStatus.getStatusCode()) {
throw (new GitLabApiException(response));
throw handle(e);
}
return (response);
}
......@@ -267,7 +202,7 @@ public abstract class AbstractApi {
* @throws IllegalArgumentException if a required parameter is null or empty
*/
protected void addFormParam(Form formData, String name, Object value, boolean required) throws IllegalArgumentException {
if (value == null) {
if (required) {
......@@ -284,4 +219,29 @@ public abstract class AbstractApi {
formData.param(name, stringValue);
}
/**
* Validates response.
* @param response response
* @param expected expected respone status
* @return original response if the response status is expected
* @throws GitLabApiException in case of unexpected response status
*/
protected Response validate(Response response, Response.Status expected) throws GitLabApiException {
if (response.getStatus() != expected.getStatusCode()) {
throw new GitLabApiException(response);
}
return response;
}
/**
* Wraps exception if needed
* @param thrown exception
* @return never returns
* @throws GitLabApiException always
*/
protected GitLabApiException handle(Exception thrown) throws GitLabApiException {
if (thrown instanceof GitLabApiException) { throw (GitLabApiException) thrown; }
throw new GitLabApiException(thrown);
}
}
......@@ -88,28 +88,12 @@ public class UserApi extends AbstractApi {
* @throws GitLabApiException
*/
public User createUser (User user, String password, Integer projectsLimit) throws GitLabApiException {
Form formData = new Form();
addFormParam(formData, "email", user.getEmail(), true);
addFormParam(formData, "password", password, true);
addFormParam(formData, "username", user.getUsername(), true);
addFormParam(formData, "name", user.getName(), true);
addFormParam(formData, "skype", user.getSkype(), false);
addFormParam(formData, "linkedin", user.getLinkedin(), false);
addFormParam(formData, "twitter", user.getTwitter(), false);
addFormParam(formData, "website_url", user.getWebsiteUrl(), false);
addFormParam(formData, "projects_limit", projectsLimit, false);
addFormParam(formData, "extern_uid", user.getExternUid(), false);
addFormParam(formData, "provider", user.getProvider(), false);
addFormParam(formData, "bio", user.getBio(), false);
addFormParam(formData, "admin", user.getIsAdmin(), false);
addFormParam(formData, "can_create_group", user.getCanCreateGroup(), false);
Form formData = user2form(user, projectsLimit, password, true);
Response response = post(Response.Status.CREATED, formData, "users");
return (response.readEntity(User.class));
}
/**
* Modifies an existing user. Only administrators can change attributes of a user.
*
......@@ -135,24 +119,8 @@ public class UserApi extends AbstractApi {
* @throws GitLabApiException
*/
public User modifyUser (User user, String password, Integer projectsLimit) throws GitLabApiException {
Form formData = new Form();
addFormParam(formData, "email", user.getEmail(), false);
addFormParam(formData, "password", password, false);
addFormParam(formData, "username", user.getUsername(), false);
addFormParam(formData, "name", user.getName(), false);
addFormParam(formData, "skype", user.getSkype(), false);
addFormParam(formData, "linkedin", user.getLinkedin(), false);
addFormParam(formData, "twitter", user.getTwitter(), false);
addFormParam(formData, "website_url", user.getWebsiteUrl(), false);
addFormParam(formData, "projects_limit", projectsLimit, false);
addFormParam(formData, "extern_uid", user.getExternUid(), false);
addFormParam(formData, "provider", user.getProvider(), false);
addFormParam(formData, "bio", user.getBio(), false);
addFormParam(formData, "admin", user.getIsAdmin(), false);
addFormParam(formData, "can_create_group", user.getCanCreateGroup(), false);
Response response = put(Response.Status.OK, formData.asMap(), "users", user.getId());
Form form = user2form(user, projectsLimit, password, false);
Response response = put(Response.Status.OK, form.asMap(), "users", user.getId());
return (response.readEntity(User.class));
}
......@@ -186,4 +154,23 @@ public class UserApi extends AbstractApi {
public void deleteUser (User user) throws GitLabApiException {
deleteUser(user.getId());
}
private Form user2form(User user, Integer projectsLimit, String password, boolean isCreate) {
Form form = new Form();
addFormParam(form, "email", user.getEmail(), isCreate);
addFormParam(form, "password", password, isCreate);
addFormParam(form, "username", user.getUsername(), isCreate);
addFormParam(form, "name", user.getName(), isCreate);
addFormParam(form, "skype", user.getSkype(), false);
addFormParam(form, "linkedin", user.getLinkedin(), false);
addFormParam(form, "twitter", user.getTwitter(), false);
addFormParam(form, "website_url", user.getWebsiteUrl(), false);
addFormParam(form, "projects_limit", projectsLimit, false);
addFormParam(form, "extern_uid", user.getExternUid(), false);
addFormParam(form, "provider", user.getProvider(), false);
addFormParam(form, "bio", user.getBio(), false);
addFormParam(form, "admin", user.getIsAdmin(), false);
addFormParam(form, "can_create_group", user.getCanCreateGroup(), false);
return form;
}
}
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