Skip to content

Commit 6dcc241

Browse files
committed
Use Query classes to retrieve projects
Allow most flexibility and forward-compatiblity with new parameters, added in the future.
1 parent 198ea33 commit 6dcc241

File tree

4 files changed

+268
-28
lines changed

4 files changed

+268
-28
lines changed

src/main/java/org/gitlab/api/GitlabAPI.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.gitlab.api.http.GitlabHTTPRequestor;
66
import org.gitlab.api.http.Query;
77
import org.gitlab.api.models.*;
8+
import org.gitlab.api.query.PaginationQuery;
9+
import org.gitlab.api.query.ProjectsQuery;
810
import org.slf4j.Logger;
911
import org.slf4j.LoggerFactory;
1012

@@ -843,6 +845,16 @@ public List<GitlabProject> getProjectsWithPagination(int page, int perPage) thro
843845
return getProjectsWithPagination(pagination);
844846
}
845847

848+
/**
849+
* Get a list of projects accessible by the authenticated user.
850+
*
851+
* @return A list of gitlab projects
852+
*/
853+
public List<GitlabProject> getProjects(ProjectsQuery projectsQuery) {
854+
String tailUrl = GitlabProject.URL + projectsQuery;
855+
return retrieve().getAll(tailUrl, GitlabProject[].class);
856+
}
857+
846858
/**
847859
* Get a list of projects by pagination accessible by the authenticated user.
848860
*
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
11
package org.gitlab.api;
22

33
import org.gitlab.api.http.Query;
4+
import org.gitlab.api.query.PaginationQuery;
45

5-
import java.io.UnsupportedEncodingException;
6+
/**
7+
* @deprecated Use {@link PaginationQuery#PARAM_PAGE} instead.
8+
*/
9+
@Deprecated
10+
public class Pagination extends PaginationQuery {
611

7-
public class Pagination {
8-
public static final String PARAM_PAGE = "page";
9-
public static final String PARAM_PER_PAGE = "per_page";
10-
public static final int MAX_ITEMS_PER_PAGE = 100;
11-
private final Query paginationQuery = new Query();
12+
/**
13+
* @deprecated Use {@link PaginationQuery#PARAM_PAGE} instead.
14+
*/
15+
@Deprecated
16+
public static final String PARAM_PAGE = PaginationQuery.PARAM_PAGE;
1217

13-
public void setPage(int page) {
14-
try {
15-
paginationQuery.append(PARAM_PAGE, String.valueOf(page));
16-
} catch (UnsupportedEncodingException ignored) {
17-
}
18-
}
18+
/**
19+
@deprecated Use {@link PaginationQuery#PARAM_PER_PAGE} instead.
20+
*/
21+
@Deprecated
22+
public static final String PARAM_PER_PAGE = PaginationQuery.PARAM_PER_PAGE;
23+
24+
/**
25+
@deprecated Use {@link PaginationQuery#MAX_ITEMS_PER_PAGE} instead.
26+
*/
27+
@Deprecated
28+
public static final int MAX_ITEMS_PER_PAGE = PaginationQuery.MAX_ITEMS_PER_PAGE;
1929

20-
public void setPerPage(int perPage) {
21-
if (perPage > MAX_ITEMS_PER_PAGE) {
22-
throw new IllegalArgumentException("Max value for perPage is " + MAX_ITEMS_PER_PAGE);
23-
}
24-
try {
25-
paginationQuery.append(PARAM_PER_PAGE, String.valueOf(perPage));
26-
} catch (UnsupportedEncodingException ignored) {
27-
}
28-
}
29-
3030
public Pagination withPage(int page) {
3131
setPage(page);
3232
return this;
3333
}
34-
34+
3535
public Pagination withPerPage(int perPage) {
3636
setPerPage(perPage);
3737
return this;
3838
}
3939

4040
public Query asQuery() {
41-
return paginationQuery;
41+
return this;
4242
}
4343

44-
@Override
45-
public String toString() {
46-
return paginationQuery.toString();
47-
}
4844
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.gitlab.api.query;
2+
3+
import org.gitlab.api.http.Query;
4+
5+
import java.io.UnsupportedEncodingException;
6+
7+
public class PaginationQuery extends Query {
8+
9+
public static final String PARAM_PAGE = "page";
10+
public static final String PARAM_PER_PAGE = "per_page";
11+
public static final int MAX_ITEMS_PER_PAGE = 100;
12+
13+
public void setPage(int page) {
14+
try {
15+
append(PARAM_PAGE, String.valueOf(page));
16+
} catch (UnsupportedEncodingException ignored) {
17+
}
18+
}
19+
20+
public void setPerPage(int perPage) {
21+
if (perPage > MAX_ITEMS_PER_PAGE) {
22+
throw new IllegalArgumentException("Max value for perPage is " + MAX_ITEMS_PER_PAGE);
23+
}
24+
try {
25+
append(PARAM_PER_PAGE, String.valueOf(perPage));
26+
} catch (UnsupportedEncodingException ignored) {
27+
}
28+
}
29+
30+
public PaginationQuery withPage(int page) {
31+
setPage(page);
32+
return this;
33+
}
34+
35+
public PaginationQuery withPerPage(int perPage) {
36+
setPerPage(perPage);
37+
return this;
38+
}
39+
40+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package org.gitlab.api.query;
2+
3+
import org.gitlab.api.models.GitlabAccessLevel;
4+
5+
import java.io.UnsupportedEncodingException;
6+
7+
public class ProjectsQuery extends PaginationQuery {
8+
9+
public static final String PARAM_ARCHIVED = "archived";
10+
public static final String PARAM_VISIBILITY = "visibility";
11+
public static final String PARAM_ORDER_BY = "order_by";
12+
public static final String PARAM_SORT = "sort";
13+
public static final String PARAM_SEARCH = "search";
14+
public static final String PARAM_SIMPLE = "simple";
15+
public static final String PARAM_OWNED = "owned";
16+
public static final String PARAM_MEMBERSHIP = "membership";
17+
public static final String PARAM_STARRED = "starred";
18+
public static final String PARAM_STATISTICS = "statistics";
19+
public static final String PARAM_WITH_CUSTOM_ATTRIBUTES = "with_custom_attributes";
20+
public static final String PARAM_WITH_ISSUES_ENABLED = "with_issues_enabled";
21+
public static final String PARAM_WITH_MERGE_REQUESTS_ENABLED = "with_merge_requests_enabled";
22+
public static final String PARAM_WITH_PROGRAMMING_LANGUAGE = "with_programming_language";
23+
public static final String PARAM_WIKI_CHECKSUM_FAILED = "wiki_checksum_failed";
24+
public static final String PARAM_REPOSITORY_CHECKSUM_FAILED = "repository_checksum_failed";
25+
public static final String PARAM_MIN_ACCESS_LEVEL = "min_access_level";
26+
27+
public void setArchived(Boolean archived) throws UnsupportedEncodingException {
28+
appendIf(PARAM_ARCHIVED, archived);
29+
}
30+
31+
public ProjectsQuery withArchived(Boolean archived) throws UnsupportedEncodingException {
32+
setArchived(archived);
33+
return this;
34+
}
35+
36+
public void setVisibility(String visibility) throws UnsupportedEncodingException {
37+
appendIf(PARAM_VISIBILITY, visibility);
38+
}
39+
40+
public ProjectsQuery withVisibility(String visibility) throws UnsupportedEncodingException {
41+
setVisibility(visibility);
42+
return this;
43+
}
44+
45+
public void setOrderBy(String orderBy) throws UnsupportedEncodingException {
46+
appendIf(PARAM_ORDER_BY, orderBy);
47+
}
48+
49+
public ProjectsQuery withOrderBy(String orderBy) throws UnsupportedEncodingException {
50+
setOrderBy(orderBy);
51+
return this;
52+
}
53+
54+
public void setSort(String sort) throws UnsupportedEncodingException {
55+
appendIf(PARAM_SORT, sort);
56+
}
57+
58+
public ProjectsQuery withSort(String sort) throws UnsupportedEncodingException {
59+
setSort(sort);
60+
return this;
61+
}
62+
63+
public void setSearch(String search) throws UnsupportedEncodingException {
64+
appendIf(PARAM_SEARCH, search);
65+
}
66+
67+
public ProjectsQuery withSearch(String search) throws UnsupportedEncodingException {
68+
setSearch(search);
69+
return this;
70+
}
71+
72+
public void setSimple(Boolean simple) throws UnsupportedEncodingException {
73+
appendIf(PARAM_SIMPLE, simple);
74+
}
75+
76+
public ProjectsQuery withSimple(Boolean simple) throws UnsupportedEncodingException {
77+
setSimple(simple);
78+
return this;
79+
}
80+
81+
public void setOwned(Boolean owned) throws UnsupportedEncodingException {
82+
appendIf(PARAM_OWNED, owned);
83+
}
84+
85+
public ProjectsQuery withOwned(Boolean owned) throws UnsupportedEncodingException {
86+
setOwned(owned);
87+
return this;
88+
}
89+
90+
public void setMembership(Boolean membership) throws UnsupportedEncodingException {
91+
appendIf(PARAM_MEMBERSHIP, membership);
92+
}
93+
94+
public ProjectsQuery withMembership(Boolean membership) throws UnsupportedEncodingException {
95+
setMembership(membership);
96+
return this;
97+
}
98+
99+
public void setStarred(Boolean starred) throws UnsupportedEncodingException {
100+
appendIf(PARAM_STARRED, starred);
101+
}
102+
103+
public ProjectsQuery withStarred(Boolean starred) throws UnsupportedEncodingException {
104+
setStarred(starred);
105+
return this;
106+
}
107+
108+
public void setStatistics(Boolean statistics) throws UnsupportedEncodingException {
109+
appendIf(PARAM_STATISTICS, statistics);
110+
}
111+
112+
public ProjectsQuery withStatistics(Boolean statistics) throws UnsupportedEncodingException {
113+
setStatistics(statistics);
114+
return this;
115+
}
116+
117+
public void setWithCustomAttributes(Boolean withCustomAttributes) throws UnsupportedEncodingException {
118+
appendIf(PARAM_WITH_CUSTOM_ATTRIBUTES, withCustomAttributes);
119+
}
120+
121+
public ProjectsQuery withWithCustomAttributes(Boolean withCustomAttributes) throws UnsupportedEncodingException {
122+
setWithCustomAttributes(withCustomAttributes);
123+
return this;
124+
}
125+
126+
public void setWithIssuesEnabled(Boolean withIssuesEnabled) throws UnsupportedEncodingException {
127+
appendIf(PARAM_WITH_ISSUES_ENABLED, withIssuesEnabled);
128+
}
129+
130+
public ProjectsQuery withWithIssuesEnabled(Boolean withIssuesEnabled) throws UnsupportedEncodingException {
131+
setWithIssuesEnabled(withIssuesEnabled);
132+
return this;
133+
}
134+
135+
public void setWithMergeRequestsEnabled(Boolean withMergeRequestsEnabled) throws UnsupportedEncodingException {
136+
appendIf(PARAM_WITH_MERGE_REQUESTS_ENABLED, withMergeRequestsEnabled);
137+
}
138+
139+
public ProjectsQuery withWithMergeRequestsEnabled(Boolean withMergeRequestsEnabled) throws UnsupportedEncodingException {
140+
setWithMergeRequestsEnabled(withMergeRequestsEnabled);
141+
return this;
142+
}
143+
144+
public void setWithProgrammingLanguage(String withProgrammingLanguage) throws UnsupportedEncodingException {
145+
appendIf(PARAM_WITH_PROGRAMMING_LANGUAGE, withProgrammingLanguage);
146+
}
147+
148+
public ProjectsQuery withWithProgrammingLanguage(String withProgrammingLanguage) throws UnsupportedEncodingException {
149+
setWithProgrammingLanguage(withProgrammingLanguage);
150+
return this;
151+
}
152+
153+
public void setWikiChecksumFailed(Boolean wikiChecksumFailed) throws UnsupportedEncodingException {
154+
appendIf(PARAM_WIKI_CHECKSUM_FAILED, wikiChecksumFailed);
155+
}
156+
157+
public ProjectsQuery withWikiChecksumFailed(Boolean wikiChecksumFailed) throws UnsupportedEncodingException {
158+
setWikiChecksumFailed(wikiChecksumFailed);
159+
return this;
160+
}
161+
162+
public void setRepositoryChecksumFailed(Boolean repositoryChecksumFailed) throws UnsupportedEncodingException {
163+
appendIf(PARAM_REPOSITORY_CHECKSUM_FAILED, repositoryChecksumFailed);
164+
}
165+
166+
public ProjectsQuery withRepositoryChecksumFailed(Boolean repositoryChecksumFailed) throws UnsupportedEncodingException {
167+
setRepositoryChecksumFailed(repositoryChecksumFailed);
168+
return this;
169+
}
170+
171+
public void setMinAccessLevel(GitlabAccessLevel minAccessLevel) throws UnsupportedEncodingException {
172+
appendIf(PARAM_MIN_ACCESS_LEVEL, minAccessLevel);
173+
}
174+
175+
public ProjectsQuery withMinAccessLevel(GitlabAccessLevel minAccessLevel) throws UnsupportedEncodingException {
176+
setMinAccessLevel(minAccessLevel);
177+
return this;
178+
}
179+
180+
@Override
181+
public ProjectsQuery withPage(int page) {
182+
super.withPage(page);
183+
return this;
184+
}
185+
186+
@Override
187+
public ProjectsQuery withPerPage(int perPage) {
188+
super.withPerPage(perPage);
189+
return this;
190+
}
191+
192+
}

0 commit comments

Comments
 (0)