@@ -24,6 +24,20 @@ public class UserApi extends AbstractApi {
24
24
super (gitLabApi );
25
25
}
26
26
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
+
27
41
/**
28
42
* Get a list of users. Only returns the first page
29
43
* <p>
@@ -64,8 +78,7 @@ public List<User> getUsers(int page, int perPage) throws GitLabApiException {
64
78
* @throws GitLabApiException if any exception occurs
65
79
*/
66
80
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" ));
69
82
}
70
83
71
84
/**
@@ -77,8 +90,7 @@ public Pager<User> getUsers(int itemsPerPage) throws GitLabApiException {
77
90
* @throws GitLabApiException if any exception occurs
78
91
*/
79
92
public List <User > getActiveUsers () throws GitLabApiException {
80
- GitLabApiForm formData = new GitLabApiForm ()
81
- .withParam ("with_custom_attributes" , customAttributesEnabled )
93
+ GitLabApiForm formData = creatGitLabApiForm ()
82
94
.withParam ("active" , true )
83
95
.withParam (PER_PAGE_PARAM , getDefaultPerPage ());
84
96
Response response = get (Response .Status .OK , formData .asMap (), "users" );
@@ -97,8 +109,7 @@ public List<User> getActiveUsers() throws GitLabApiException {
97
109
* @throws GitLabApiException if any exception occurs
98
110
*/
99
111
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 ()
102
113
.withParam ("active" , true )
103
114
.withParam (PAGE_PARAM , page )
104
115
.withParam (PER_PAGE_PARAM , perPage );
@@ -117,9 +128,7 @@ public List<User> getActiveUsers(int page, int perPage) throws GitLabApiExceptio
117
128
* @throws GitLabApiException if any exception occurs
118
129
*/
119
130
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 );
123
132
return (new Pager <User >(this , User .class , itemsPerPage , formData .asMap (), "users" ));
124
133
}
125
134
@@ -173,8 +182,7 @@ public void unblockUser(Integer userId) throws GitLabApiException {
173
182
* @throws GitLabApiException if any exception occurs
174
183
*/
175
184
public List <User > getBlockedUsers () throws GitLabApiException {
176
- GitLabApiForm formData = new GitLabApiForm ()
177
- .withParam ("with_custom_attributes" , customAttributesEnabled )
185
+ GitLabApiForm formData = creatGitLabApiForm ()
178
186
.withParam ("blocked" , true )
179
187
.withParam (PER_PAGE_PARAM , getDefaultPerPage ());
180
188
Response response = get (Response .Status .OK , formData .asMap (), "users" );
@@ -193,8 +201,7 @@ public List<User> getBlockedUsers() throws GitLabApiException {
193
201
* @throws GitLabApiException if any exception occurs
194
202
*/
195
203
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 ()
198
205
.withParam ("blocked" , true )
199
206
.withParam (PAGE_PARAM , page )
200
207
.withParam (PER_PAGE_PARAM , perPage );
@@ -213,9 +220,7 @@ public List<User> getblockedUsers(int page, int perPage) throws GitLabApiExcepti
213
220
* @throws GitLabApiException if any exception occurs
214
221
*/
215
222
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 );
219
224
return (new Pager <User >(this , User .class , itemsPerPage , formData .asMap (), "users" ));
220
225
}
221
226
@@ -262,9 +267,7 @@ public Optional<User> getOptionalUser(int userId) {
262
267
* @throws GitLabApiException if any exception occurs
263
268
*/
264
269
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 );
268
271
Response response = get (Response .Status .OK , formData .asMap (), "users" );
269
272
List <User > users = response .readEntity (new GenericType <List <User >>() {
270
273
});
@@ -299,10 +302,9 @@ public Optional<User> getOptionalUser(String username) {
299
302
* @throws GitLabApiException if any exception occurs
300
303
*/
301
304
public List <User > findUsers (String emailOrUsername ) throws GitLabApiException {
302
- GitLabApiForm formData = new GitLabApiForm ()
305
+ GitLabApiForm formData = creatGitLabApiForm ()
303
306
.withParam ("search" , emailOrUsername , true )
304
- .withParam (PER_PAGE_PARAM , getDefaultPerPage ())
305
- .withParam ("with_custom_attributes" , customAttributesEnabled );
307
+ .withParam (PER_PAGE_PARAM , getDefaultPerPage ());
306
308
Response response = get (Response .Status .OK , formData .asMap (), "users" );
307
309
return (response .readEntity (new GenericType <List <User >>() {
308
310
}));
@@ -320,11 +322,10 @@ public List<User> findUsers(String emailOrUsername) throws GitLabApiException {
320
322
* @throws GitLabApiException if any exception occurs
321
323
*/
322
324
public List <User > findUsers (String emailOrUsername , int page , int perPage ) throws GitLabApiException {
323
- GitLabApiForm formData = new GitLabApiForm ()
325
+ GitLabApiForm formData = creatGitLabApiForm ()
324
326
.withParam ("search" , emailOrUsername , true )
325
327
.withParam (PAGE_PARAM , page )
326
- .withParam (PER_PAGE_PARAM , perPage )
327
- .withParam ("with_custom_attributes" , customAttributesEnabled );
328
+ .withParam (PER_PAGE_PARAM , perPage );
328
329
Response response = get (Response .Status .OK , formData .asMap (), "users" );
329
330
return (response .readEntity (new GenericType <List <User >>() {
330
331
}));
@@ -341,9 +342,7 @@ public List<User> findUsers(String emailOrUsername, int page, int perPage) throw
341
342
* @throws GitLabApiException if any exception occurs
342
343
*/
343
344
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 );
347
346
return (new Pager <User >(this , User .class , itemsPerPage , formData .asMap (), "users" ));
348
347
}
349
348
@@ -908,12 +907,15 @@ Form userToForm(User user, Integer projectsLimit, CharSequence password, Boolean
908
907
.withParam ("shared_runners_minutes_limit" , user .getSharedRunnersMinutesLimit (), false ));
909
908
}
910
909
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 );
918
920
}
919
921
}
0 commit comments