Skip to content

Manage pagination #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,10 @@
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<configuration>
<useAgent>true</useAgent>
</configuration>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.simplify4u.plugins</groupId>
<artifactId>sign-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -154,8 +141,17 @@
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>

</plugins>

<pluginManagement>
<plugins>
<plugin>
<groupId>org.simplify4u.plugins</groupId>
<artifactId>sign-maven-plugin</artifactId>
<version>0.3.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
52 changes: 43 additions & 9 deletions src/main/java/com/icoderman/woocommerce/DefaultHttpClient.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.icoderman.woocommerce;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
Expand All @@ -18,16 +23,15 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class DefaultHttpClient implements HttpClient {

private static final String CONTENT_TYPE = "Content-Type";
private static final String APPLICATION_JSON = "application/json";
private static final String WC_TOTAL_HEADER = "X-WP-Total";
private static final String WC_TOTAL_PAGES_HEADER = "X-WP-TotalPages";

private CloseableHttpClient httpClient;
private ObjectMapper mapper;
Expand All @@ -44,9 +48,14 @@ public Map get(String url) {
}

@Override
public List getAll(String url) {
public Page getAll(String url) {
HttpGet httpGet = new HttpGet(url);
return getEntityAndReleaseConnection(httpGet, List.class);
Map result = getEntityWithHeaderAndReleaseConnection(httpGet, List.class);
List content = (List) result.get("content");
int total = Integer.parseInt(result.get("total").toString());
int totalPages = Integer.parseInt(result.get("totalPages").toString());
Page page = new Page<>(content, total, totalPages, content.size());
return page;
}

@Override
Expand Down Expand Up @@ -131,4 +140,29 @@ private <T> T getEntityAndReleaseConnection(HttpRequestBase httpRequest, Class<T
httpRequest.releaseConnection();
}
}

private <T> Map getEntityWithHeaderAndReleaseConnection(HttpRequestBase httpRequest, Class<T> objectClass) {
try {
HttpResponse httpResponse = httpClient.execute(httpRequest);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity == null) {
throw new RuntimeException("Error retrieving results from http request");
}
Object result = mapper.readValue(httpEntity.getContent(), Object.class);
if (objectClass.isInstance(result)) {
Map<String, Object> map = new HashMap<>();
if (httpResponse.containsHeader(WC_TOTAL_HEADER)) {
map.put("total", httpResponse.getFirstHeader(WC_TOTAL_HEADER).getValue());
map.put("totalPages", httpResponse.getFirstHeader(WC_TOTAL_PAGES_HEADER).getValue());
}
map.put("content", objectClass.cast(result));
return map;
}
throw new RuntimeException("Can't parse retrieved object: " + result.toString());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
httpRequest.releaseConnection();
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/icoderman/woocommerce/HttpClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.icoderman.woocommerce;

import java.util.List;
import java.util.Map;

/**
Expand All @@ -22,7 +21,7 @@ public interface HttpClient {
* @param url url to request
* @return retrieved result
*/
List getAll(String url);
Page getAll(String url);

/**
* Requests url with HTTP POST and retrieves result object as Map
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/icoderman/woocommerce/Page.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.icoderman.woocommerce;

import java.util.List;

public class Page<T> {

/**
* List of element.
*/
private List<T> content;
/**
* Total number of element.
*/
private int total;
/**
* Total number of pages.
*/
private int totalPages;
/**
* Size of the current page.
*/
private int size;

/**
* Create new page of element.
* @param content Content list
* @param total total number of element.
* @param totalPages total number of pages.
* @param size size of the current result.
*/
public Page(List<T> content, int total, int totalPages, int size) {
this.content = content;
this.total = total;
this.totalPages = totalPages;
this.size = size;
}

public List<T> getContent() {
return content;
}

public int getTotal() {
return total;
}

public int getTotalPages() {
return totalPages;
}

public int getSize() {
return size;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/icoderman/woocommerce/WooCommerce.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public interface WooCommerce {
* @param params additional request params
* @return List of retrieved entities
*/
List getAll(String endpointBase, Map<String, String> params);
Page getAll(String endpointBase, Map<String, String> params);

/**
* Retrieves all WooCommerce entities
*
* @param endpointBase API endpoint base @see EndpointBaseType
* @return List of retrieved entities
*/
default List getAll(String endpointBase) {
default Page getAll(String endpointBase) {
return getAll(endpointBase, Collections.emptyMap());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Map get(String endpointBase, int id) {
}

@Override
public List getAll(String endpointBase, Map<String, String> params) {
public Page getAll(String endpointBase, Map<String, String> params) {
String url = String.format(API_URL_FORMAT, config.getUrl(), apiVersion, endpointBase);
String signature = OAuthSignature.getAsQueryString(config, url, HttpMethod.GET, params);
String securedUrl = String.format(URL_SECURED_FORMAT, url, signature);
Expand Down