Skip to content

Commit 2bd1f51

Browse files
committed
Now only includes with_custom_attributes param if enabled (#252).
1 parent 161e6aa commit 2bd1f51

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed

src/main/java/org/gitlab4j/api/AbstractApi.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,12 @@ protected MultivaluedMap<String, String> getPageQueryParams(int page, int perPag
538538
* @return a MultivaluedMap instance containing "page" and "per_page" params
539539
*/
540540
protected MultivaluedMap<String, String> getPageQueryParams(int page, int perPage, boolean customAttributesEnabled) {
541+
542+
GitLabApiForm form = new GitLabApiForm().withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage);
541543
if (customAttributesEnabled)
542-
return (new GitLabApiForm().withParam("with_custom_attributes", true).withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage).asMap());
543-
return (new GitLabApiForm().withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage).asMap());
544+
return (form.withParam("with_custom_attributes", true).asMap());
545+
546+
return (form.asMap());
544547
}
545548

546549
/**
@@ -559,8 +562,11 @@ protected MultivaluedMap<String, String> getDefaultPerPageParam() {
559562
* @return a MultivaluedMap instance containing the "per_page" param with the default value
560563
*/
561564
protected MultivaluedMap<String, String> getDefaultPerPageParam(boolean customAttributesEnabled) {
565+
566+
GitLabApiForm form = new GitLabApiForm().withParam(PER_PAGE_PARAM, getDefaultPerPage());
562567
if (customAttributesEnabled)
563-
return (new GitLabApiForm().withParam("with_custom_attributes", true).withParam(PER_PAGE_PARAM, getDefaultPerPage()).asMap());
564-
return (new GitLabApiForm().withParam(PER_PAGE_PARAM, getDefaultPerPage()).asMap());
568+
return (form.withParam("with_custom_attributes", true).asMap());
569+
570+
return (form.asMap());
565571
}
566572
}

src/main/java/org/gitlab4j/api/UserApi.java

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ public class UserApi extends AbstractApi {
2424
super(gitLabApi);
2525
}
2626

27+
/**
28+
* Enables custom attributes to be returned when fetching User instances.
29+
*/
30+
public void enableCustomAttributes() {
31+
customAttributesEnabled = true;
32+
}
33+
34+
/**
35+
* Disables custom attributes to be returned when fetching User instances.
36+
*/
37+
public void disableCustomAttributes() {
38+
customAttributesEnabled = false;
39+
}
40+
2741
/**
2842
* Get a list of users. Only returns the first page
2943
* <p>
@@ -64,8 +78,7 @@ public List<User> getUsers(int page, int perPage) throws GitLabApiException {
6478
* @throws GitLabApiException if any exception occurs
6579
*/
6680
public Pager<User> getUsers(int itemsPerPage) throws GitLabApiException {
67-
GitLabApiForm formData = new GitLabApiForm().withParam("with_custom_attributes", customAttributesEnabled);
68-
return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "users"));
81+
return (new Pager<User>(this, User.class, itemsPerPage, creatGitLabApiForm().asMap(), "users"));
6982
}
7083

7184
/**
@@ -77,8 +90,7 @@ public Pager<User> getUsers(int itemsPerPage) throws GitLabApiException {
7790
* @throws GitLabApiException if any exception occurs
7891
*/
7992
public List<User> getActiveUsers() throws GitLabApiException {
80-
GitLabApiForm formData = new GitLabApiForm()
81-
.withParam("with_custom_attributes", customAttributesEnabled)
93+
GitLabApiForm formData = creatGitLabApiForm()
8294
.withParam("active", true)
8395
.withParam(PER_PAGE_PARAM, getDefaultPerPage());
8496
Response response = get(Response.Status.OK, formData.asMap(), "users");
@@ -97,8 +109,7 @@ public List<User> getActiveUsers() throws GitLabApiException {
97109
* @throws GitLabApiException if any exception occurs
98110
*/
99111
public List<User> getActiveUsers(int page, int perPage) throws GitLabApiException {
100-
GitLabApiForm formData = new GitLabApiForm()
101-
.withParam("with_custom_attributes", customAttributesEnabled)
112+
GitLabApiForm formData = creatGitLabApiForm()
102113
.withParam("active", true)
103114
.withParam(PAGE_PARAM, page)
104115
.withParam(PER_PAGE_PARAM, perPage);
@@ -117,9 +128,7 @@ public List<User> getActiveUsers(int page, int perPage) throws GitLabApiExceptio
117128
* @throws GitLabApiException if any exception occurs
118129
*/
119130
public Pager<User> getActiveUsers(int itemsPerPage) throws GitLabApiException {
120-
GitLabApiForm formData = new GitLabApiForm()
121-
.withParam("active", true)
122-
.withParam("with_custom_attributes", customAttributesEnabled);
131+
GitLabApiForm formData = creatGitLabApiForm().withParam("active", true);
123132
return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "users"));
124133
}
125134

@@ -173,8 +182,7 @@ public void unblockUser(Integer userId) throws GitLabApiException {
173182
* @throws GitLabApiException if any exception occurs
174183
*/
175184
public List<User> getBlockedUsers() throws GitLabApiException {
176-
GitLabApiForm formData = new GitLabApiForm()
177-
.withParam("with_custom_attributes", customAttributesEnabled)
185+
GitLabApiForm formData = creatGitLabApiForm()
178186
.withParam("blocked", true)
179187
.withParam(PER_PAGE_PARAM, getDefaultPerPage());
180188
Response response = get(Response.Status.OK, formData.asMap(), "users");
@@ -193,8 +201,7 @@ public List<User> getBlockedUsers() throws GitLabApiException {
193201
* @throws GitLabApiException if any exception occurs
194202
*/
195203
public List<User> getblockedUsers(int page, int perPage) throws GitLabApiException {
196-
GitLabApiForm formData = new GitLabApiForm()
197-
.withParam("with_custom_attributes", customAttributesEnabled)
204+
GitLabApiForm formData = creatGitLabApiForm()
198205
.withParam("blocked", true)
199206
.withParam(PAGE_PARAM, page)
200207
.withParam(PER_PAGE_PARAM, perPage);
@@ -213,9 +220,7 @@ public List<User> getblockedUsers(int page, int perPage) throws GitLabApiExcepti
213220
* @throws GitLabApiException if any exception occurs
214221
*/
215222
public Pager<User> getBlockedUsers(int itemsPerPage) throws GitLabApiException {
216-
GitLabApiForm formData = new GitLabApiForm()
217-
.withParam("blocked", true)
218-
.withParam("with_custom_attributes", customAttributesEnabled);
223+
GitLabApiForm formData = creatGitLabApiForm().withParam("blocked", true);
219224
return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "users"));
220225
}
221226

@@ -262,9 +267,7 @@ public Optional<User> getOptionalUser(int userId) {
262267
* @throws GitLabApiException if any exception occurs
263268
*/
264269
public User getUser(String username) throws GitLabApiException {
265-
GitLabApiForm formData = new GitLabApiForm()
266-
.withParam("username", username, true)
267-
.withParam("with_custom_attributes", customAttributesEnabled);
270+
GitLabApiForm formData = creatGitLabApiForm().withParam("username", username, true);
268271
Response response = get(Response.Status.OK, formData.asMap(), "users");
269272
List<User> users = response.readEntity(new GenericType<List<User>>() {
270273
});
@@ -299,10 +302,9 @@ public Optional<User> getOptionalUser(String username) {
299302
* @throws GitLabApiException if any exception occurs
300303
*/
301304
public List<User> findUsers(String emailOrUsername) throws GitLabApiException {
302-
GitLabApiForm formData = new GitLabApiForm()
305+
GitLabApiForm formData = creatGitLabApiForm()
303306
.withParam("search", emailOrUsername, true)
304-
.withParam(PER_PAGE_PARAM, getDefaultPerPage())
305-
.withParam("with_custom_attributes", customAttributesEnabled);
307+
.withParam(PER_PAGE_PARAM, getDefaultPerPage());
306308
Response response = get(Response.Status.OK, formData.asMap(), "users");
307309
return (response.readEntity(new GenericType<List<User>>() {
308310
}));
@@ -320,11 +322,10 @@ public List<User> findUsers(String emailOrUsername) throws GitLabApiException {
320322
* @throws GitLabApiException if any exception occurs
321323
*/
322324
public List<User> findUsers(String emailOrUsername, int page, int perPage) throws GitLabApiException {
323-
GitLabApiForm formData = new GitLabApiForm()
325+
GitLabApiForm formData = creatGitLabApiForm()
324326
.withParam("search", emailOrUsername, true)
325327
.withParam(PAGE_PARAM, page)
326-
.withParam(PER_PAGE_PARAM, perPage)
327-
.withParam("with_custom_attributes", customAttributesEnabled);
328+
.withParam(PER_PAGE_PARAM, perPage);
328329
Response response = get(Response.Status.OK, formData.asMap(), "users");
329330
return (response.readEntity(new GenericType<List<User>>() {
330331
}));
@@ -341,9 +342,7 @@ public List<User> findUsers(String emailOrUsername, int page, int perPage) throw
341342
* @throws GitLabApiException if any exception occurs
342343
*/
343344
public Pager<User> findUsers(String emailOrUsername, int itemsPerPage) throws GitLabApiException {
344-
GitLabApiForm formData = new GitLabApiForm()
345-
.withParam("search", emailOrUsername, true)
346-
.withParam("with_custom_attributes", customAttributesEnabled);
345+
GitLabApiForm formData = creatGitLabApiForm().withParam("search", emailOrUsername, true);
347346
return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "users"));
348347
}
349348

@@ -908,12 +907,15 @@ Form userToForm(User user, Integer projectsLimit, CharSequence password, Boolean
908907
.withParam("shared_runners_minutes_limit", user.getSharedRunnersMinutesLimit(), false));
909908
}
910909

911-
public void enableCustomAttributes() {
912-
this.customAttributesEnabled = true;
913-
}
914-
915-
public UserApi withCustomAttributes() {
916-
enableCustomAttributes();
917-
return this;
910+
/**
911+
* Creates a GitLabApiForm instance that will optionally include the
912+
* with_custom_attributes query param if enabled.
913+
*
914+
* @return a GitLabApiForm instance that will optionally include the
915+
* with_custom_attributes query param if enabled
916+
*/
917+
private GitLabApiForm creatGitLabApiForm() {
918+
GitLabApiForm formData = new GitLabApiForm();
919+
return (customAttributesEnabled ? formData.withParam("with_custom_attributes", true) : formData);
918920
}
919921
}

0 commit comments

Comments
 (0)