Skip to content

Commit a9b416d

Browse files
committed
Added endpoint for getting all posts for a categoryId and set the default Context for getMedia to VIEW instead of EDIT
1 parent 52e61f9 commit a9b416d

4 files changed

Lines changed: 23 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.14.0-SNAPSHOT</version>
7+
<version>4.15.2-SNAPSHOT</version>
88

99
<packaging>jar</packaging>
1010

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ public List<Media> getPostMedias(Long postId, @Nullable String context) {
252252
return collected;
253253
}
254254

255+
@Override
256+
public List<Post> getCategoryPosts(Long categoryId) {
257+
// final ResponseEntity<Post[]> exchange = doExchange1(Request.CATEGORY_POSTS, HttpMethod.GET, Post[].class, forExpand(categoryId), null, null);
258+
// return Arrays.asList(exchange.getBody());
259+
List<Post> collected = new ArrayList<>();
260+
PagedResponse<Post> pagedResponse = this.getPagedResponse(Request.CATEGORY_POSTS, Post.class, String.valueOf(categoryId), context);
261+
collected.addAll(pagedResponse.getList());
262+
while (pagedResponse.hasNext()) {
263+
pagedResponse = this.traverse(pagedResponse, PagedResponse.NEXT);
264+
collected.addAll(pagedResponse.getList());
265+
}
266+
return collected;
267+
268+
}
269+
255270
@Override
256271
public List<Media> getMedia() {
257272
List<Media> collected = new ArrayList<>();
@@ -271,7 +286,7 @@ public Media getMedia(Long id) {
271286

272287
@Override
273288
public Media getMedia(Long id, @Nullable String context) {
274-
final ImmutableMap<String, Object> queryParams = ImmutableMap.of(CONTEXT_, ofNullable(context).orElse(Contexts.EDIT));
289+
final ImmutableMap<String, Object> queryParams = ImmutableMap.of(CONTEXT_, ofNullable(context).orElse(Contexts.VIEW));
275290
return CustomRenderableParser.parse(doExchange1(Request.MEDIA, HttpMethod.GET, String.class, forExpand(id), queryParams, null), Media.class);
276291
}
277292

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.afrozaar.wordpress.wpapi.v2.request.Request;
88
import com.afrozaar.wordpress.wpapi.v2.request.SearchRequest;
99

10+
import java.util.List;
1011
import java.util.Map;
1112

1213
public interface Posts {
@@ -49,6 +50,8 @@ public interface Posts {
4950

5051
Post deletePost(Post post);
5152

53+
List<Post> getCategoryPosts(Long categoryId);
54+
5255
/**
5356
* Search request just returning the first page of posts.
5457
*/

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

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

3-
import static java.util.stream.Collectors.toMap;
4-
53
import com.afrozaar.wordpress.wpapi.v2.Client;
6-
74
import com.google.common.collect.ImmutableMap;
8-
95
import org.springframework.web.util.UriComponentsBuilder;
106

117
import java.net.URI;
@@ -14,6 +10,8 @@
1410
import java.util.List;
1511
import java.util.Map;
1612

13+
import static java.util.stream.Collectors.toMap;
14+
1715
public abstract class Request {
1816
public static final String POSTS = "/posts";
1917
public static final String POST = "/posts/{id}";
@@ -43,6 +41,7 @@ public abstract class Request {
4341
public static final String TAG = "/tags/{tagId}";
4442
public static final String CATEGORIES = "/categories";
4543
public static final String CATEGORY = "/categories/{categoryId}";
44+
public static final String CATEGORY_POSTS = "/posts?categories={categoryId}";
4645
public static final String QP_CONTEXT = "context";
4746
public static final String QP_ORDER_BY = "orderby";
4847
public static final String QP_ORDER = "order";

0 commit comments

Comments
 (0)