Skip to content

Commit 50397fd

Browse files
author
Oleksandr Mandryk
committed
Added the possibility to provide additional request parameters for getAll method
1 parent 5d69b67 commit 50397fd

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

src/main/java/com/icoderman/woocommerce/WooCommerce.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.icoderman.woocommerce;
22

3+
import java.util.Collections;
34
import java.util.List;
45
import java.util.Map;
56

@@ -26,13 +27,24 @@ public interface WooCommerce {
2627
*/
2728
Map get(String endpointBase, int id);
2829

30+
/**
31+
* Retrieves all WooCommerce entities with request parameters
32+
*
33+
* @param endpointBase API endpoint base @see EndpointBaseType
34+
* @param params additional request params
35+
* @return List of retrieved entities
36+
*/
37+
List getAll(String endpointBase, Map<String, String> params);
38+
2939
/**
3040
* Retrieves all WooCommerce entities
3141
*
3242
* @param endpointBase API endpoint base @see EndpointBaseType
3343
* @return List of retrieved entities
3444
*/
35-
List getAll(String endpointBase);
45+
default List getAll(String endpointBase) {
46+
return getAll(endpointBase, Collections.emptyMap());
47+
}
3648

3749
/**
3850
* Updates WooCommerce entity

src/main/java/com/icoderman/woocommerce/WooCommerceAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public Map get(String endpointBase, int id) {
3535
}
3636

3737
@Override
38-
public List getAll(String endpointBase) {
38+
public List getAll(String endpointBase, Map<String, String> params) {
3939
String url = String.format(API_URL_FORMAT, config.getUrl(), endpointBase);
40-
String signature = OAuthSignature.getAsQueryString(config, url, HttpMethod.GET);
40+
String signature = OAuthSignature.getAsQueryString(config, url, HttpMethod.GET, params);
4141
String securedUrl = String.format(URL_SECURED_FORMAT, url, signature);
4242
return client.getAll(securedUrl);
4343
}

src/main/java/com/icoderman/woocommerce/oauth/OAuthSignature.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,44 @@ public class OAuthSignature {
2929

3030
private OAuthSignature() {}
3131

32-
public static Map<String, String> getAsMap(OAuthConfig config, String endpoint, HttpMethod httpMethod) {
32+
public static Map<String, String> getAsMap(OAuthConfig config, String endpoint, HttpMethod httpMethod, Map<String, String> params) {
3333
if (config == null || endpoint == null || httpMethod == null) {
3434
return Collections.emptyMap();
3535
}
36-
Map<String,String> params = new HashMap<>();
37-
params.put(OAuthHeader.OAUTH_CONSUMER_KEY.getValue(), config.getConsumerKey());
38-
params.put(OAuthHeader.OAUTH_TIMESTAMP.getValue(), String.valueOf(System.currentTimeMillis() / 1000L));
39-
params.put(OAuthHeader.OAUTH_NONCE.getValue(), UUID.randomUUID().toString());
40-
params.put(OAuthHeader.OAUTH_SIGNATURE_METHOD.getValue(), SIGNATURE_METHOD_HMAC_SHA256);
36+
Map<String,String> authParams = new HashMap<>();
37+
authParams.put(OAuthHeader.OAUTH_CONSUMER_KEY.getValue(), config.getConsumerKey());
38+
authParams.put(OAuthHeader.OAUTH_TIMESTAMP.getValue(), String.valueOf(System.currentTimeMillis() / 1000L));
39+
authParams.put(OAuthHeader.OAUTH_NONCE.getValue(), UUID.randomUUID().toString());
40+
authParams.put(OAuthHeader.OAUTH_SIGNATURE_METHOD.getValue(), SIGNATURE_METHOD_HMAC_SHA256);
41+
authParams.putAll(params);
4142

4243
// WooCommerce specified param
4344
if (HttpMethod.DELETE.equals(httpMethod)) {
44-
params.put(DELETE_PARAM_FORCE, Boolean.TRUE.toString());
45+
authParams.put(DELETE_PARAM_FORCE, Boolean.TRUE.toString());
4546
}
46-
String oAuthSignature = generateOAuthSignature(config.getConsumerSecret(), endpoint, httpMethod, params);
47-
params.put(OAuthHeader.OAUTH_SIGNATURE.getValue(), oAuthSignature);
48-
return params;
47+
String oAuthSignature = generateOAuthSignature(config.getConsumerSecret(), endpoint, httpMethod, authParams);
48+
authParams.put(OAuthHeader.OAUTH_SIGNATURE.getValue(), oAuthSignature);
49+
return authParams;
4950
}
5051

51-
public static String getAsQueryString(OAuthConfig config, String endpoint, HttpMethod httpMethod) {
52+
public static Map<String, String> getAsMap(OAuthConfig config, String endpoint, HttpMethod httpMethod) {
53+
return getAsMap(config, endpoint, httpMethod, Collections.emptyMap());
54+
}
55+
56+
public static String getAsQueryString(OAuthConfig config, String endpoint, HttpMethod httpMethod, Map<String, String> params) {
5257
if (config == null || endpoint == null || httpMethod == null) {
5358
return "";
5459
}
55-
Map<String, String> oauthParameters = getAsMap(config, endpoint, httpMethod);
60+
Map<String, String> oauthParameters = getAsMap(config, endpoint, httpMethod, params);
5661
String encodedSignature = oauthParameters.get(OAuthHeader.OAUTH_SIGNATURE.getValue())
5762
.replace(SpecialSymbol.PLUS.getPlain(), SpecialSymbol.PLUS.getEncoded());
5863
oauthParameters.put(OAuthHeader.OAUTH_SIGNATURE.getValue(), encodedSignature);
5964
return mapToString(oauthParameters, SpecialSymbol.EQUAL.getPlain(), SpecialSymbol.AMP.getPlain());
6065
}
6166

67+
public static String getAsQueryString(OAuthConfig config, String endpoint, HttpMethod httpMethod) {
68+
return getAsQueryString(config, endpoint, httpMethod, Collections.emptyMap());
69+
}
6270

6371
private static String generateOAuthSignature(String customerSecret, String endpoint, HttpMethod httpMethod, Map<String, String> parameters) {
6472
String signatureBaseString = getSignatureBaseString(endpoint, httpMethod.name(), parameters);

src/test/java/com/icoderman/woocommerce/integration/WooCommerceClientTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
public class WooCommerceClientTest {
1717

18-
private static final String CONSUMER_KEY = "ck_700a11f6fa9fb498a58dd3c252d8e52e93f3f073";
19-
private static final String CONSUMER_SECRET = "cs_a2117031fd9283f995e9822f6a739e65486a0528";
20-
private static final String WC_URL = "http://localhost:32789";
18+
private static final String CONSUMER_KEY = "ck_d35e7be7cc695d87f23490729dd80e173f88c8f5";
19+
private static final String CONSUMER_SECRET = "cs_53a835760712ebf0c8bcf2a21197af4b2323a052";
20+
private static final String WC_URL = "http://localhost/index.php";
2121

2222
private WooCommerce wooCommerce;
2323

@@ -41,14 +41,17 @@ public void apiCreateProductTest() {
4141
@Ignore
4242
@Test
4343
public void apiGetAllProductsTest() {
44-
Object products = wooCommerce.getAll(EndpointBaseType.PRODUCTS.getValue());
44+
Map<String, String> params = new HashMap<>();
45+
params.put("per_page","100");
46+
params.put("offset","0");
47+
Object products = wooCommerce.getAll(EndpointBaseType.PRODUCTS.getValue(), params);
4548
Assert.assertNotNull(products);
4649
}
4750

4851
@Ignore
4952
@Test
5053
public void apiGetProductTest() {
51-
Map product = wooCommerce.get(EndpointBaseType.PRODUCTS.getValue(), 10);
54+
Map product = wooCommerce.get(EndpointBaseType.PRODUCTS.getValue(), 79);
5255
Assert.assertNotNull(product);
5356
}
5457

0 commit comments

Comments
 (0)