Skip to content

Commit 8bbbb91

Browse files
committed
Create a Tuples class to contain the different required arity tuple factories.
1 parent 3e31ea8 commit 8bbbb91

File tree

5 files changed

+64
-34
lines changed

5 files changed

+64
-34
lines changed

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

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

33
import static com.afrozaar.wordpress.wpapi.v2.util.FieldExtractor.extractField;
4+
import static com.afrozaar.wordpress.wpapi.v2.util.Tuples.tuple;
45

56
import static java.lang.String.format;
67
import static java.net.URLDecoder.decode;
@@ -37,7 +38,7 @@
3738
import com.afrozaar.wordpress.wpapi.v2.response.PagedResponse;
3839
import com.afrozaar.wordpress.wpapi.v2.util.AuthUtil;
3940
import com.afrozaar.wordpress.wpapi.v2.util.MavenProperties;
40-
import com.afrozaar.wordpress.wpapi.v2.util.Tuple2;
41+
import com.afrozaar.wordpress.wpapi.v2.util.Tuples.Tuple2;
4142

4243
import com.google.common.base.Preconditions;
4344
import com.google.common.collect.ImmutableMap;
@@ -125,7 +126,7 @@ public class Client implements Wordpress {
125126

126127
{
127128
Properties properties = MavenProperties.getProperties();
128-
userAgentTuple = Tuple2.of("User-Agent", format("%s/%s", properties.getProperty(ARTIFACT_ID), properties.getProperty(VERSION)));
129+
userAgentTuple = tuple("User-Agent", format("%s/%s", properties.getProperty(ARTIFACT_ID), properties.getProperty(VERSION)));
129130
}
130131

131132
public Client(String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug) {
@@ -893,20 +894,20 @@ protected Map<String, Object> fieldsFrom(Post post) {
893894

894895
Arrays.stream(post.getClass().getDeclaredFields())
895896
.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()))
897+
.map(field -> tuple(field, field.getAnnotationsByType(JsonProperty.class)[0]))
898+
.filter(fieldTuple -> processableFields.contains(fieldTuple.v2.value()))
898899
.forEach(field -> {
899900
try {
900-
ReflectionUtils.makeAccessible(field.a);
901-
Object theField = field.a.get(post);
901+
ReflectionUtils.makeAccessible(field.v1);
902+
Object theField = field.v1.get(post);
902903
if (nonNull(theField)) {
903904
final Object value;
904905
if (theField instanceof RenderableField) {
905906
value = ((RenderableField) theField).getRendered();
906907
} else {
907908
value = theField;
908909
}
909-
biConsumer.accept(field.b.value(), value);
910+
biConsumer.accept(field.v2.value(), value);
910911
}
911912
} catch (IllegalAccessException e) {
912913
LOG.error("Error populating post fields builder.", e);
@@ -918,7 +919,9 @@ protected Map<String, Object> fieldsFrom(Post post) {
918919

919920
private <T, B> ResponseEntity<T> doExchange0(HttpMethod method, URI uri, Class<T> typeRef, B body, @Nullable MediaType mediaType) {
920921
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);
922+
final RequestEntity.BodyBuilder builder = RequestEntity.method(method, uri)
923+
.header(authTuple.v1, authTuple.v2)
924+
.header(userAgentTuple.v1, userAgentTuple.v2);
922925

923926
ofNullable(mediaType).ifPresent(builder::contentType);
924927

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)