Skip to content

Commit 19785cd

Browse files
committed
Added getUsersWithRole endpoint and ability to set the per_page query parameter
1 parent b8942b7 commit 19785cd

7 files changed

Lines changed: 291 additions & 6 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.afrozaar.wordpress</groupId>
66
<artifactId>wp-api-v2-client-java</artifactId>
7-
<version>4.15.3-SNAPSHOT</version>
7+
<version>4.17.1-SNAPSHOT</version>
88

99
<packaging>jar</packaging>
1010

src/main/java/com/afrozaar/wordpress/wpapi/v2/Client.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import javax.annotation.Nullable;
4444
import java.lang.reflect.InvocationTargetException;
4545
import java.net.URI;
46+
import java.net.URISyntaxException;
4647
import java.net.URLDecoder;
4748
import java.util.*;
4849
import java.util.function.BiConsumer;
@@ -85,6 +86,7 @@ public class Client implements Wordpress {
8586
public final boolean debug;
8687
public final boolean permalinkEndpoint;
8788
private Boolean canDeleteMetaViaPost = null;
89+
public Integer perPage;
8890

8991
{
9092
Properties properties = MavenProperties.getProperties();
@@ -132,6 +134,10 @@ public Client(String context, String baseUrl, String username, String password,
132134

133135
}
134136

137+
public void setPerPage(Integer perPage) {
138+
this.perPage = perPage;
139+
}
140+
135141
@Override
136142
public String getContext() {
137143
return ofNullable(this.context).orElse(DEFAULT_CONTEXT);
@@ -786,6 +792,18 @@ public List<User> getUsers(final String contextType) {
786792
return collected;
787793
}
788794

795+
@Override
796+
public List<User> getUsersWithRole(final String role, final String contextType) {
797+
List<User> collected = new ArrayList<>();
798+
PagedResponse<User> usersResponse = this.getPagedResponse(Request.USERS_WITH_ROLE, User.class, role, contextType);
799+
collected.addAll(usersResponse.getList());
800+
while (usersResponse.hasNext()) {
801+
usersResponse = traverse(usersResponse, PagedResponse.NEXT);
802+
collected.addAll(usersResponse.getList());
803+
}
804+
return collected;
805+
}
806+
789807
@SuppressWarnings("unchecked")
790808
@Override
791809
public User createUser(User user, String username, String password) throws WpApiParsedException {
@@ -889,15 +907,34 @@ public User updateUser(User user) {
889907
@SuppressWarnings("unchecked")
890908
@Override
891909
public <T> PagedResponse<T> getPagedResponse(String context, Class<T> typeRef, String... expandParams) {
892-
final URI uri = Request.of(context).usingClient(this).buildAndExpand((Object[]) expandParams).toUri();
910+
URI uri = Request.of(context).usingClient(this).buildAndExpand((Object[]) expandParams).toUri();
911+
if (perPage != null) {
912+
try {
913+
uri = adjustPerPage(uri);
914+
} catch (URISyntaxException e) {
915+
LOG.error("Error setting per_page request parameter: " + e.getMessage());
916+
}
917+
}
893918
return getPagedResponse(uri, typeRef);
894919
}
895920

921+
private URI adjustPerPage(URI uri) throws URISyntaxException {
922+
String perPageQuery = "per_page=" + this.perPage;
923+
String newQuery = uri.getQuery();
924+
if (newQuery == null) {
925+
newQuery = perPageQuery;
926+
} else {
927+
newQuery += "&" + perPageQuery;
928+
}
929+
930+
return new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), newQuery, uri.getFragment());
931+
}
932+
933+
896934
@SuppressWarnings("unchecked")
897935
@Override
898936
public <T> PagedResponse<T> getPagedResponse(final URI uri, Class<T> typeRef) {
899937
try {
900-
901938
final ResponseEntity<String> exchange = doExchange0(HttpMethod.GET, uri, String.class, null, null);
902939
final String body1 = exchange.getBody();
903940
//LOG.debug("about to parse response for paged response {}: {}", typeRef, body1);

src/main/java/com/afrozaar/wordpress/wpapi/v2/api/Users.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public interface Users {
1717

1818
List<User> getUsers(String context);
1919

20+
List<User> getUsersWithRole(String role, String contextType);
21+
2022
User createUser(User user, String username, String password) throws WpApiParsedException;
2123

2224
User getUser(long userId) throws UserNotFoundException;

src/main/java/com/afrozaar/wordpress/wpapi/v2/model/Post.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"sticky",
2323
"password",
2424
"format",
25+
"meta",
2526
"link",
2627
"ping_status",
2728
"excerpt",
@@ -58,6 +59,8 @@ public class Post {
5859
private String password;
5960
@JsonProperty("format")
6061
private String format;
62+
@JsonProperty("meta")
63+
private Map<String, List<String>> meta;
6164
@JsonProperty("link")
6265
private String link;
6366
@JsonProperty("ping_status")
@@ -198,6 +201,14 @@ public void setFormat(String format) {
198201
this.format = format;
199202
}
200203

204+
public Map<String, List<String>> getMeta() {
205+
return meta;
206+
}
207+
208+
public void setMeta(Map<String, List<String>> meta) {
209+
this.meta = meta;
210+
}
211+
201212
@JsonProperty("link")
202213
public String getLink() {
203214
return link;

src/main/java/com/afrozaar/wordpress/wpapi/v2/request/Request.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public abstract class Request {
3636
public static final String PAGE = "/pages/{pageId}";
3737
public static final String USERS = "/users";
3838
public static final String USERS_WITH_CONTEXT = "/users?context={context}";
39+
public static final String USERS_WITH_ROLE = "/users?roles={role}&context={context}";
3940
public static final String USER = "/users/{userId}";
4041
public static final String TAGS = "/tags";
4142
public static final String TAG = "/tags/{tagId}";

src/test/java/com/afrozaar/wordpress/wpapi/v2/response/CustomRenderableParserTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.afrozaar.wordpress.wpapi.v2.response;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
53
import com.afrozaar.wordpress.wpapi.v2.model.DeleteResponse;
64
import com.afrozaar.wordpress.wpapi.v2.model.Media;
75
import com.afrozaar.wordpress.wpapi.v2.model.Term;
8-
96
import org.junit.Test;
107

118
import java.io.IOException;
@@ -15,6 +12,8 @@
1512
import java.util.Arrays;
1613
import java.util.stream.Collectors;
1714

15+
import static org.assertj.core.api.Assertions.assertThat;
16+
1817
public class CustomRenderableParserTest {
1918

2019
@Test

src/test/resources/json/post.json

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
{
2+
"id": 98,
3+
"date": "2020-08-04T11:47:32",
4+
"date_gmt": "2020-08-04T09:47:32",
5+
"guid": {
6+
"rendered": "https://imiginodev.wpengine.com/?p=98"
7+
},
8+
"modified": "2020-08-04T14:41:32",
9+
"modified_gmt": "2020-08-04T12:41:32",
10+
"slug": "jems",
11+
"status": "publish",
12+
"type": "post",
13+
"link": "https://imiginodev.wpengine.com/jems/",
14+
"title": {
15+
"rendered": "Jems"
16+
},
17+
"content": {
18+
"rendered": "",
19+
"protected": false
20+
},
21+
"excerpt": {
22+
"rendered": "",
23+
"protected": false
24+
},
25+
"author": 3,
26+
"featured_media": 99,
27+
"comment_status": "open",
28+
"ping_status": "open",
29+
"sticky": false,
30+
"template": "",
31+
"format": "standard",
32+
"meta": {
33+
"_edit_lock": [
34+
"1600086490:6"
35+
],
36+
"_edit_last": [
37+
"3"
38+
],
39+
"_thumbnail_id": [
40+
"99"
41+
],
42+
"player_group_name": [
43+
"Clarion"
44+
],
45+
"player_download_enabled": [
46+
""
47+
],
48+
"player_watermark_enabled": [
49+
"true"
50+
],
51+
"player_vast_enabled": [
52+
""
53+
],
54+
"player_vast_tag": [
55+
""
56+
],
57+
"player_watermark_image": [
58+
"https://clarion-cdn.imigino.com/Jems.svg"
59+
],
60+
"player_watermark_position": [
61+
"top-left"
62+
],
63+
"player_ga_enabled": [
64+
""
65+
],
66+
"player_ga_code": [
67+
""
68+
],
69+
"player_parsely_enabled": [
70+
""
71+
],
72+
"player_parsely_code": [
73+
""
74+
],
75+
"player_brand_enabled": [
76+
""
77+
],
78+
"player_brand_title": [
79+
""
80+
],
81+
"player_brand_image": [
82+
""
83+
],
84+
"player_brand_destination": [
85+
""
86+
],
87+
"player_autoplay_enabled": [
88+
""
89+
],
90+
"player_seek_enabled": [
91+
"true"
92+
],
93+
"player_seek_forward": [
94+
""
95+
],
96+
"player_seek_backward": [
97+
""
98+
],
99+
"player_object_schema_enabled": [
100+
""
101+
],
102+
"player_object_schema_organization": [
103+
""
104+
],
105+
"player_not_fluid": [
106+
""
107+
],
108+
"player_preload": [
109+
"auto"
110+
],
111+
"player_standalone_enabled": [
112+
"true"
113+
],
114+
"player_share_enabled": [
115+
""
116+
],
117+
"player_share_facebook_enabled": [
118+
""
119+
],
120+
"player_share_fbmetaid": [
121+
""
122+
],
123+
"player_share_twitter_enabled": [
124+
""
125+
],
126+
"player_share_tumblr_enabled": [
127+
""
128+
],
129+
"player_share_pinterest_enabled": [
130+
""
131+
],
132+
"player_share_linkedin_enabled": [
133+
""
134+
],
135+
"player_endcap_enabled": [
136+
""
137+
],
138+
"player_playlist": [
139+
""
140+
],
141+
"player_playlist_enabled_static": [
142+
""
143+
],
144+
"player_playlist_static_urls": [
145+
""
146+
],
147+
"player_playlist_enabled_bibliodam": [
148+
""
149+
],
150+
"player_playlist_bibliodam": [
151+
""
152+
],
153+
"player_playlist_bibliodam_key": [
154+
""
155+
],
156+
"player_playlist_vertical": [
157+
""
158+
],
159+
"player_playlist_horizontal": [
160+
""
161+
],
162+
"player_playlist_none": [
163+
""
164+
]
165+
},
166+
"categories": [
167+
1
168+
],
169+
"tags": [],
170+
"_links": {
171+
"self": [
172+
{
173+
"href": "https://imiginodev.wpengine.com/wp-json/wp/v2/posts/98"
174+
}
175+
],
176+
"collection": [
177+
{
178+
"href": "https://imiginodev.wpengine.com/wp-json/wp/v2/posts"
179+
}
180+
],
181+
"about": [
182+
{
183+
"href": "https://imiginodev.wpengine.com/wp-json/wp/v2/types/post"
184+
}
185+
],
186+
"author": [
187+
{
188+
"embeddable": true,
189+
"href": "https://imiginodev.wpengine.com/wp-json/wp/v2/users/3"
190+
}
191+
],
192+
"replies": [
193+
{
194+
"embeddable": true,
195+
"href": "https://imiginodev.wpengine.com/wp-json/wp/v2/comments?post=98"
196+
}
197+
],
198+
"version-history": [
199+
{
200+
"count": 0,
201+
"href": "https://imiginodev.wpengine.com/wp-json/wp/v2/posts/98/revisions"
202+
}
203+
],
204+
"wp:featuredmedia": [
205+
{
206+
"embeddable": true,
207+
"href": "https://imiginodev.wpengine.com/wp-json/wp/v2/media/99"
208+
}
209+
],
210+
"wp:attachment": [
211+
{
212+
"href": "https://imiginodev.wpengine.com/wp-json/wp/v2/media?parent=98"
213+
}
214+
],
215+
"wp:term": [
216+
{
217+
"taxonomy": "category",
218+
"embeddable": true,
219+
"href": "https://imiginodev.wpengine.com/wp-json/wp/v2/categories?post=98"
220+
},
221+
{
222+
"taxonomy": "post_tag",
223+
"embeddable": true,
224+
"href": "https://imiginodev.wpengine.com/wp-json/wp/v2/tags?post=98"
225+
}
226+
],
227+
"curies": [
228+
{
229+
"name": "wp",
230+
"href": "https://api.w.org/{rel}",
231+
"templated": true
232+
}
233+
]
234+
}
235+
}

0 commit comments

Comments
 (0)