Skip to content

Commit c1b1ef9

Browse files
committed
Merged develop into master and stabilized
2 parents 13af24b + d94ea44 commit c1b1ef9

10 files changed

Lines changed: 127 additions & 39 deletions

File tree

pom.xml

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

44
<groupId>com.afrozaar.wordpress</groupId>
55
<artifactId>wp-api-v2-client-java</artifactId>
6-
<version>4.8.3</version>
6+
<version>4.8.5</version>
77

88
<packaging>jar</packaging>
99

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

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

33
import static com.afrozaar.wordpress.wpapi.v2.util.FieldExtractor.extractField;
4-
4+
import static com.afrozaar.wordpress.wpapi.v2.util.Tuples.tuple;
55
import static java.lang.String.format;
66
import static java.net.URLDecoder.decode;
77
import static java.util.Objects.isNull;
@@ -37,7 +37,7 @@
3737
import com.afrozaar.wordpress.wpapi.v2.response.PagedResponse;
3838
import com.afrozaar.wordpress.wpapi.v2.util.AuthUtil;
3939
import com.afrozaar.wordpress.wpapi.v2.util.MavenProperties;
40-
import com.afrozaar.wordpress.wpapi.v2.util.Tuple2;
40+
import com.afrozaar.wordpress.wpapi.v2.util.Tuples.Tuple2;
4141

4242
import com.google.common.base.Preconditions;
4343
import com.google.common.collect.ImmutableMap;
@@ -70,7 +70,6 @@
7070
import com.fasterxml.jackson.databind.DeserializationFeature;
7171
import com.fasterxml.jackson.databind.JsonMappingException;
7272
import com.fasterxml.jackson.databind.ObjectMapper;
73-
7473
import org.apache.commons.beanutils.BeanUtils;
7574
import org.assertj.core.util.VisibleForTesting;
7675
import org.slf4j.Logger;
@@ -125,7 +124,7 @@ public class Client implements Wordpress {
125124

126125
{
127126
Properties properties = MavenProperties.getProperties();
128-
userAgentTuple = Tuple2.of("User-Agent", format("%s/%s", properties.getProperty(ARTIFACT_ID), properties.getProperty(VERSION)));
127+
userAgentTuple = tuple("User-Agent", format("%s/%s", properties.getProperty(ARTIFACT_ID), properties.getProperty(VERSION)));
129128
}
130129

131130
public Client(String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug) {
@@ -187,15 +186,24 @@ public Post createPost(Post post, PostStatus status) throws PostCreateException
187186
return createPost(fieldsFrom(post), status);
188187
}
189188

189+
@Override
190+
public Post getCustomPost(Long id, String requestPath) throws PostNotFoundException {
191+
return getPost(id, requestPath, Contexts.VIEW);
192+
}
193+
190194
@Override
191195
public Post getPost(Long id) throws PostNotFoundException {
192196
return getPost(id, Contexts.VIEW);
193197
}
194198

195199
@Override
196200
public Post getPost(Long id, String context) throws PostNotFoundException {
201+
return getPost(id, Request.POST, context);
202+
}
203+
204+
public Post getPost(Long id, String postTypeName, String context) throws PostNotFoundException {
197205
try {
198-
return doExchange1(Request.POST, HttpMethod.GET, Post.class, forExpand(id), ImmutableMap.of(CONTEXT_, context), null).getBody();
206+
return doExchange1(postTypeName, HttpMethod.GET, Post.class, forExpand(id), ImmutableMap.of(CONTEXT_, context), null).getBody();
199207
} catch (HttpClientErrorException e) {
200208
if (e.getStatusCode().is4xxClientError() && e.getStatusCode().value() == 404) {
201209
throw new PostNotFoundException(e);
@@ -327,6 +335,14 @@ public PostMeta createMeta(Long postId, String key, String value) {
327335
return exchange.getBody();
328336
}
329337

338+
@Override
339+
public PostMeta createCustomPostMeta(Long postId, String key, String value, String customPostTypeName) {
340+
final Map<String, String> body = ImmutableMap.of(META_KEY, key, META_VALUE, value);
341+
final ResponseEntity<PostMeta> exchange = doExchange1(Request.CUSTOM_POST_METAS, HttpMethod.POST, PostMeta.class, forExpand(customPostTypeName, postId), null, body,
342+
MediaType.APPLICATION_JSON);
343+
return exchange.getBody();
344+
}
345+
330346
@Override
331347
public List<PostMeta> getPostMetas(Long postId) {
332348
final ResponseEntity<PostMeta[]> exchange = doExchange1(Request.METAS, HttpMethod.GET, PostMeta[].class, forExpand(postId), null, null);
@@ -339,6 +355,18 @@ public PostMeta getPostMeta(Long postId, Long metaId) {
339355
return exchange.getBody();
340356
}
341357

358+
@Override
359+
public List<PostMeta> getCustomPostMetas(Long postId, String customPostTypeName) {
360+
final ResponseEntity<PostMeta[]> exchange = doExchange1(Request.CUSTOM_POST_METAS, HttpMethod.GET, PostMeta[].class, forExpand(customPostTypeName, postId), null, null);
361+
return Arrays.asList(exchange.getBody());
362+
}
363+
364+
@Override
365+
public PostMeta getCustomPostMeta(Long postId, Long metaId, String customPostTypeName) {
366+
final ResponseEntity<PostMeta> exchange = doExchange1(Request.CUSTOM_POST_META, HttpMethod.GET, PostMeta.class, forExpand(customPostTypeName, postId, metaId), null, null);
367+
return exchange.getBody();
368+
}
369+
342370
@Override
343371
public PostMeta updatePostMetaValue(Long postId, Long metaId, String value) {
344372
return updatePostMeta(postId, metaId, null, value);
@@ -356,6 +384,18 @@ public PostMeta updatePostMeta(Long postId, Long metaId, String key, String valu
356384
return exchange.getBody();
357385
}
358386

387+
@Override
388+
public PostMeta updateCustomPostMeta(Long postId, Long metaId, String key, String value, String customPostTypeName) {
389+
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
390+
BiConsumer<String, Object> biConsumer = (key1, value1) -> ofNullable(value1).ifPresent(v -> builder.put(key1, v));
391+
392+
biConsumer.accept(META_KEY, key);
393+
biConsumer.accept(META_VALUE, value);
394+
final ResponseEntity<PostMeta> exchange = doExchange1(Request.CUSTOM_POST_META, HttpMethod.POST, PostMeta.class, forExpand(customPostTypeName, postId, metaId), null, builder.build());
395+
396+
return exchange.getBody();
397+
}
398+
359399
private BiFunction<Long, Long, Boolean> supportsMetaDeleteViaPostMethod = (pid, mid) -> {
360400
if (nonNull(canDeleteMetaViaPost)) {
361401
return canDeleteMetaViaPost;
@@ -661,6 +701,11 @@ public List<Term> deleteCategories(boolean force, Term... terms) {
661701
return deletedTerms;
662702
}
663703

704+
@Override
705+
public Term updateCategory(Term categoryTerm) {
706+
return doExchange1(Request.CATEGORY, HttpMethod.POST, Term.class, forExpand(categoryTerm.getId()), null, categoryTerm.asMap()).getBody();
707+
}
708+
664709
@Override
665710
public Page createPage(Page page, PostStatus status) {
666711
final Map<String, Object> map = page.asMap();
@@ -893,23 +938,23 @@ protected Map<String, Object> fieldsFrom(Post post) {
893938

894939
Arrays.stream(post.getClass().getDeclaredFields())
895940
.filter(field -> field.getAnnotationsByType(JsonProperty.class).length > 0)
896-
.map(field -> Tuple2.of(field, field.getAnnotationsByType(JsonProperty.class)[0]))
897-
.filter(fieldTuple -> processableFields.contains(fieldTuple.b.value()))
941+
.map(field -> tuple(field, field.getAnnotationsByType(JsonProperty.class)[0]))
942+
.filter(fieldTuple -> processableFields.contains(fieldTuple.v2.value()))
898943
.forEach(field -> {
899944
try {
900-
ReflectionUtils.makeAccessible(field.a);
901-
Object theField = field.a.get(post);
945+
ReflectionUtils.makeAccessible(field.v1);
946+
Object theField = field.v1.get(post);
902947
if (nonNull(theField)) {
903948
final Object value;
904949
if (theField instanceof RenderableField) {
905950
value = ((RenderableField) theField).getRendered();
906951
} else {
907952
value = theField;
908953
}
909-
biConsumer.accept(field.b.value(), value);
954+
biConsumer.accept(field.v2.value(), value);
910955
}
911956
} catch (IllegalAccessException e) {
912-
LOG.error("Error populating post fields builder.", e);
957+
LOG.error("Error populating post fields builder for field '{}'", field.v1.getName(), e);
913958
}
914959
});
915960

@@ -918,7 +963,9 @@ protected Map<String, Object> fieldsFrom(Post post) {
918963

919964
private <T, B> ResponseEntity<T> doExchange0(HttpMethod method, URI uri, Class<T> typeRef, B body, @Nullable MediaType mediaType) {
920965
final Tuple2<String, String> authTuple = AuthUtil.authTuple(username, password);
921-
final RequestEntity.BodyBuilder builder = RequestEntity.method(method, uri).header(authTuple.a, authTuple.b).header(userAgentTuple.a, userAgentTuple.b);
966+
final RequestEntity.BodyBuilder builder = RequestEntity.method(method, uri)
967+
.header(authTuple.v1, authTuple.v2)
968+
.header(userAgentTuple.v1, userAgentTuple.v2);
922969

923970
ofNullable(mediaType).ifPresent(builder::contentType);
924971

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public interface Categories {
1616
Term deleteCategory(Term categoryTerm) throws TermNotFoundException;
1717
Term deleteCategory(Term categoryTerm, boolean force) throws TermNotFoundException;
1818

19+
Term updateCategory(Term categoryTerm);
20+
1921
List<Term> deleteCategories(Term... terms);
2022
List<Term> deleteCategories(boolean force,Term... terms);
2123
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ public interface PostMetas {
1111

1212
PostMeta createMeta(Long postId, String key, String value);
1313

14+
PostMeta createCustomPostMeta(Long postId, String key, String value, String customPostTypeName);
15+
1416
List<PostMeta> getPostMetas(Long postId);
1517

1618
PostMeta getPostMeta(Long postId, Long metaId);
1719

20+
List<PostMeta> getCustomPostMetas(Long postId, String requestPath);
21+
22+
PostMeta getCustomPostMeta(Long postId, Long metaId, String requestPath);
23+
1824
PostMeta updatePostMetaValue(Long postId, Long metaId, String value);
1925

2026
PostMeta updatePostMeta(Long postId, Long metaId, String key, String value);
2127

28+
PostMeta updateCustomPostMeta(Long postId, Long metaId, String key, String value, String customPostTypeName);
29+
2230
boolean deletePostMeta(Long postId, Long metaId);
2331

2432
boolean deletePostMeta(Long postId, Long metaId, Boolean force);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public interface Posts {
3333

3434
Post createPost(Post post, PostStatus status) throws PostCreateException;
3535

36+
Post getCustomPost(Long id, String postTypeName) throws PostNotFoundException;
37+
3638
Post getPost(Long id) throws PostNotFoundException;
3739
Post getPost(Long id, String context) throws PostNotFoundException;
3840

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public abstract class Request {
2323

2424
public static final String METAS = "/posts/{postId}/meta";
2525
public static final String META = "/posts/{postId}/meta/{metaId}";
26+
public static final String CUSTOM_POST_METAS = "/{postType}/{postId}/meta";
27+
public static final String CUSTOM_POST_META = "/{postType}/{postId}/meta/{metaId}";
2628
public static final String META_POST_DELETE = "/posts/{postId}/meta/{metaId}/delete";
2729
public static final String TAXONOMIES = "/taxonomies";
2830
public static final String TAXONOMY = "/taxonomies/{slug}";

src/main/java/com/afrozaar/wordpress/wpapi/v2/util/AuthUtil.java

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

3+
import com.afrozaar.wordpress.wpapi.v2.util.Tuples.Tuple2;
4+
35
import org.springframework.http.HttpEntity;
46
import org.springframework.http.HttpHeaders;
57
import org.springframework.http.MediaType;
@@ -11,15 +13,15 @@ public class AuthUtil {
1113
public static HttpHeaders createHeaders(String username, String password) {
1214
HttpHeaders httpHeaders = new HttpHeaders();
1315
final Tuple2<String, String> authHeader = authTuple(username, password);
14-
httpHeaders.set(authHeader.a, authHeader.b);
16+
httpHeaders.set(authHeader.v1, authHeader.v2);
1517
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
1618

1719
return httpHeaders;
1820
}
1921

2022
public static Tuple2<String, String> authTuple(String username, String password) {
2123
final byte[] encodedAuth = Base64.getEncoder().encode((username + ":" + password).getBytes());
22-
return Tuple2.of("Authorization", "Basic " + new String(encodedAuth));
24+
return Tuples.tuple("Authorization", "Basic " + new String(encodedAuth));
2325
}
2426

2527
public static HttpEntity<String> basicAuth(String username, String password) {

src/main/java/com/afrozaar/wordpress/wpapi/v2/util/Tuple2.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.afrozaar.wordpress.wpapi.v2.util;
2+
3+
public class Tuples {
4+
5+
public static <T1, T2> Tuple2<T1, T2> tuple(T1 v1, T2 v2) {
6+
return new Tuple2<>(v1, v2);
7+
}
8+
public static <T1, T2, T3, T4, T5> Tuple5<T1, T2, T3, T4, T5> tuple(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5) {
9+
return new Tuple5<>(v1, v2, v3, v4, v5);
10+
}
11+
12+
public static class Tuple2<T1, T2> {
13+
public final T1 v1;
14+
public final T2 v2;
15+
16+
private Tuple2(T1 v1, T2 v2) {
17+
this.v1 = v1;
18+
this.v2 = v2;
19+
}
20+
}
21+
22+
public static class Tuple5<V1, V2, V3, V4, V5> {
23+
public final V1 v1;
24+
public final V2 v2;
25+
public final V3 v3;
26+
public final V4 v4;
27+
public final V5 v5;
28+
29+
private Tuple5(V1 v1, V2 v2, V3 v3, V4 v4, V5 v5) {
30+
this.v1 = v1;
31+
this.v2 = v2;
32+
this.v3 = v3;
33+
this.v4 = v4;
34+
this.v5 = v5;
35+
}
36+
}
37+
38+
39+
}

src/test/java/com/afrozaar/wordpress/wpapi/v2/ClientLiveIT.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static com.afrozaar.wordpress.wpapi.v2.model.builder.TitleBuilder.aTitle;
1111
import static com.afrozaar.wordpress.wpapi.v2.model.builder.UserBuilder.aUser;
1212
import static com.afrozaar.wordpress.wpapi.v2.request.SearchRequest.Builder.aSearchRequest;
13+
import static com.afrozaar.wordpress.wpapi.v2.util.Tuples.tuple;
1314

1415
import static org.assertj.core.api.Assertions.assertThat;
1516

@@ -40,7 +41,7 @@
4041
import com.afrozaar.wordpress.wpapi.v2.request.Request;
4142
import com.afrozaar.wordpress.wpapi.v2.request.SearchRequest;
4243
import com.afrozaar.wordpress.wpapi.v2.response.PagedResponse;
43-
import com.afrozaar.wordpress.wpapi.v2.util.Tuple2;
44+
import com.afrozaar.wordpress.wpapi.v2.util.Tuples.Tuple2;
4445

4546
import org.springframework.core.io.ClassPathResource;
4647
import org.springframework.core.io.Resource;
@@ -190,7 +191,7 @@ public void testSearchForMetaKey() throws PostCreateException {
190191

191192
final Tuple2<Post, PostMeta> postWithMeta = newTestPostWithRandomDataWithMeta();
192193

193-
final PagedResponse<Post> response = client.search(aSearchRequest(Post.class).withFilter("meta_key", postWithMeta.b.getKey()).build());
194+
final PagedResponse<Post> response = client.search(aSearchRequest(Post.class).withFilter("meta_key", postWithMeta.v2.getKey()).build());
194195

195196
assertThat(response.getList()).isNotEmpty().hasSize(1);
196197
}
@@ -274,10 +275,10 @@ public void tesGetMedia() throws WpApiParsedException {
274275

275276
final Tuple2<Post, Media> postWithMedia = newTestPostWithMedia();
276277

277-
final Media media = client.getMedia(postWithMedia.b.getId());
278+
final Media media = client.getMedia(postWithMedia.v2.getId());
278279

279280
assertThat(media).isNotNull();
280-
assertThat(media.getDescription()).isEqualTo(postWithMedia.b.getDescription());
281+
assertThat(media.getDescription()).isEqualTo(postWithMedia.v2.getDescription());
281282

282283
LOG.debug("Media: {}", media);
283284
}
@@ -323,7 +324,7 @@ public void testUpdateMedia() throws WpApiParsedException {
323324
public void testGetPostMedias() throws WpApiParsedException {
324325
final Tuple2<Post, Media> postMediaTwo = newTestPostWithMedia();
325326

326-
final List<Media> postMedias = client.getPostMedias(postMediaTwo.a.getId());
327+
final List<Media> postMedias = client.getPostMedias(postMediaTwo.v1.getId());
327328

328329
assertThat(postMedias).isNotNull().isNotEmpty();
329330
}
@@ -334,7 +335,7 @@ public void testGetPostMetas() throws PostCreateException {
334335
final Tuple2<Post, PostMeta> postWithMeta = newTestPostWithRandomDataWithMeta();
335336

336337
//when
337-
final List<PostMeta> postMetas = client.getPostMetas(postWithMeta.a.getId());
338+
final List<PostMeta> postMetas = client.getPostMetas(postWithMeta.v1.getId());
338339

339340
// then
340341
assertThat(postMetas).isNotNull();
@@ -346,7 +347,7 @@ public void testGetPostMeta() throws PostCreateException {
346347

347348
final Tuple2<Post, PostMeta> postWithMeta = newTestPostWithRandomDataWithMeta();
348349

349-
final PostMeta postMeta = client.getPostMeta(postWithMeta.a.getId(), postWithMeta.b.getId());
350+
final PostMeta postMeta = client.getPostMeta(postWithMeta.v1.getId(), postWithMeta.v2.getId());
350351

351352
assertThat(postMeta).isNotNull();
352353

@@ -879,7 +880,7 @@ private PostBuilder newTestPostBuilderWithRandomData() {
879880
private Tuple2<Post, PostMeta> newTestPostWithRandomDataWithMeta() throws PostCreateException {
880881
final Post post = client.createPost(newTestPostWithRandomData(), PostStatus.publish);
881882
final PostMeta meta = client.createMeta(post.getId(), randomAlphabetic(5), randomAlphabetic(10));
882-
return Tuple2.of(post, meta);
883+
return tuple(post, meta);
883884
}
884885

885886
private Media newRandomMedia(Post post) {
@@ -897,6 +898,6 @@ private Tuple2<Post, Media> newTestPostWithMedia() throws WpApiParsedException {
897898
Resource resource = new ClassPathResource("/bin/gradient_colormap.jpg");
898899
final Media mediaItem = client.createMedia(newRandomMedia(post), resource);
899900

900-
return Tuple2.of(post, mediaItem);
901+
return tuple(post, mediaItem);
901902
}
902903
}

0 commit comments

Comments
 (0)