11package com .afrozaar .wordpress .wpapi .v2 ;
22
33import static com .afrozaar .wordpress .wpapi .v2 .util .FieldExtractor .extractField ;
4-
4+ import static com . afrozaar . wordpress . wpapi . v2 . util . Tuples . tuple ;
55import static java .lang .String .format ;
66import static java .net .URLDecoder .decode ;
77import static java .util .Objects .isNull ;
3737import com .afrozaar .wordpress .wpapi .v2 .response .PagedResponse ;
3838import com .afrozaar .wordpress .wpapi .v2 .util .AuthUtil ;
3939import 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
4242import com .google .common .base .Preconditions ;
4343import com .google .common .collect .ImmutableMap ;
7070import com .fasterxml .jackson .databind .DeserializationFeature ;
7171import com .fasterxml .jackson .databind .JsonMappingException ;
7272import com .fasterxml .jackson .databind .ObjectMapper ;
73-
7473import org .apache .commons .beanutils .BeanUtils ;
7574import org .assertj .core .util .VisibleForTesting ;
7675import 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
0 commit comments