Skip to content

Commit

Permalink
Add the ability to initialise a client with a different base context.
Browse files Browse the repository at this point in the history
This solves #41
  • Loading branch information
johanmynhardt committed Jul 18, 2017
1 parent 5a7ca1f commit ffb0489
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
36 changes: 27 additions & 9 deletions src/main/java/com/afrozaar/wordpress/wpapi/v2/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import java.util.stream.Stream;

public class Client implements Wordpress {
private static final String DEFAULT_CONTEXT = "/wp-json/wp/v2";
private static final Logger LOG = LoggerFactory.getLogger(Client.class);
private static final String META_KEY = "key";
private static final String META_VALUE = "value";
Expand All @@ -106,15 +107,16 @@ public class Client implements Wordpress {
private static final String VERSION = "version";
private static final String ARTIFACT_ID = "artifactId";

private final RestTemplate restTemplate;
protected final RestTemplate restTemplate;
private final Predicate<Link> next = link -> Strings.NEXT.equals(link.getRel());
private final Predicate<Link> previous = link -> Strings.PREV.equals(link.getRel());
private final Tuple2<String, String> userAgentTuple;

public final String context;
public final String baseUrl;
private final String username;
private final String password;
private final boolean debug;
public final String username;
public final String password;
public final boolean debug;
public final boolean permalinkEndpoint;
private Boolean canDeleteMetaViaPost = null;

Expand All @@ -124,10 +126,20 @@ public class Client implements Wordpress {
}

public Client(String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug) {
this(baseUrl, username, password, usePermalinkEndpoint, debug, null);
this(null, baseUrl, username, password, usePermalinkEndpoint, debug, null);
}

public Client(String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug, ClientHttpRequestFactory requestFactory) {
this(null, baseUrl, username, password, usePermalinkEndpoint, debug, requestFactory);
}

public Client(String context, String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug) {
this(context, baseUrl, username, password, usePermalinkEndpoint, debug, null);
}

public Client(String context, String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug,
ClientHttpRequestFactory requestFactory) {
this.context = context;
this.baseUrl = baseUrl;
this.username = username;
this.password = password;
Expand All @@ -145,13 +157,18 @@ public Client(String baseUrl, String username, String password, boolean usePerma
messageConverters.add(new MappingJackson2HttpMessageConverter(emptyArrayAsNullObjectMapper));
//messageConverters.add(new MappingJackson2HttpMessageConverter());
restTemplate = new RestTemplate(messageConverters);
if(requestFactory != null){;
restTemplate.setRequestFactory(requestFactory);

if (requestFactory != null) {
restTemplate.setRequestFactory(requestFactory);
}

}

@Override
public String getContext() {
return ofNullable(this.context).orElse(DEFAULT_CONTEXT);
}

@Override
public Post createPost(final Map<String, Object> postFields, PostStatus status) throws PostCreateException {
final ImmutableMap<String, Object> post = new ImmutableMap.Builder<String, Object>().putAll(postFields).put("status", status.value).build();
Expand Down Expand Up @@ -859,6 +876,7 @@ private Map<String, Object> fieldsFrom(Post post) {
biConsumer.accept("sticky", post.getSticky());
biConsumer.accept("featured_media", post.getFeaturedMedia());
biConsumer.accept("categories", post.getCategoryIds());
//biConsumer.accept("type", post.getType());

return builder.build();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/afrozaar/wordpress/wpapi/v2/Wordpress.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import java.util.function.Function;

public interface Wordpress extends Posts, PostMetas, Taxonomies, Terms, Medias, Pages, Users, Tags, Categories, CustomCalls {
String CONTEXT = "/wp-json/wp/v2";

String getContext();

<T> PagedResponse<T> getPagedResponse(String context, Class<T> typeRef, String... expandParams);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static Request of(String uri, Map<String, List<String>> params) {
}

public UriComponentsBuilder usingClient(Client client) {
return forHost(client, Client.CONTEXT);
return forHost(client, client.getContext());
}

public UriComponentsBuilder forHost(Client client, String context0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testTraverse() throws PostCreateException {
client.createPost(newTestPostWithRandomData(), PostStatus.publish);
}

final String EXPECTED = expectedUrlForContext(Client.CONTEXT, "/posts", clientConfig.getWordpress());
final String EXPECTED = expectedUrlForContext(client.getContext(), "/posts", clientConfig.getWordpress());

final PagedResponse<Post> postPagedResponse = client.search(Posts.list());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void createUpdateRequest() throws URISyntaxException {
// when
final URI result = UpdatePostRequest
.forPost(post)
.forHost(client, Client.CONTEXT).buildAndExpand(post.getId())
.forHost(client, client.getContext()).buildAndExpand(post.getId())
.toUri();

System.out.println("result = " + result);
Expand All @@ -62,7 +62,7 @@ public void createUpdateRequestWithPermalinkOff() throws URISyntaxException {
// when
final URI result = UpdatePostRequest
.forPost(post)
.forHost(client, Client.CONTEXT).buildAndExpand(post.getId())
.forHost(client, client.getContext()).buildAndExpand(post.getId())
.toUri();

System.out.println("result = " + result);
Expand Down

0 comments on commit ffb0489

Please sign in to comment.