-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathWooCommerceAPI.java
More file actions
67 lines (54 loc) · 2.79 KB
/
WooCommerceAPI.java
File metadata and controls
67 lines (54 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.icoderman.woocommerce;
import com.icoderman.woocommerce.oauth.OAuthSignature;
import com.icoderman.woocommerce.oauth.OAuthConfig;
import java.util.List;
import java.util.Map;
public class WooCommerceAPI implements WooCommerce {
private static final String API_URL_FORMAT = "%s/wp-json/wc/%s/%s";
private static final String API_URL_BATCH_FORMAT = "%s/wp-json/wc/%s/%s/batch";
private static final String API_URL_ONE_ENTITY_FORMAT = "%s/wp-json/wc/%s/%s/%d";
private static final String URL_SECURED_FORMAT = "%s?%s";
private HttpClient client;
private OAuthConfig config;
private String apiVersion;
public WooCommerceAPI(OAuthConfig config, ApiVersionType apiVersion) {
this.config = config;
this.client = new DefaultHttpClient();
this.apiVersion = apiVersion.getValue();
}
@Override
public Map create(String endpointBase, Map<String, Object> object) {
String url = String.format(API_URL_FORMAT, config.getUrl(), apiVersion, endpointBase);
return client.post(url, OAuthSignature.getAsMap(config, url, HttpMethod.POST), object);
}
@Override
public Map get(String endpointBase, int id) {
String url = String.format(API_URL_ONE_ENTITY_FORMAT, config.getUrl(), apiVersion, endpointBase, id);
String signature = OAuthSignature.getAsQueryString(config, url, HttpMethod.GET);
String securedUrl = String.format(URL_SECURED_FORMAT, url, signature);
return client.get(securedUrl);
}
@Override
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);
return client.getAll(securedUrl);
}
@Override
public Map update(String endpointBase, int id, Map<String, Object> object) {
String url = String.format(API_URL_ONE_ENTITY_FORMAT, config.getUrl(), apiVersion, endpointBase, id);
return client.put(url, OAuthSignature.getAsMap(config, url, HttpMethod.PUT), object);
}
@Override
public Map delete(String endpointBase, int id) {
String url = String.format(API_URL_ONE_ENTITY_FORMAT, config.getUrl(), apiVersion, endpointBase, id);
Map<String, String> params = OAuthSignature.getAsMap(config, url, HttpMethod.DELETE);
return client.delete(url, params);
}
@Override
public Map batch(String endpointBase, Map<String, Object> object) {
String url = String.format(API_URL_BATCH_FORMAT, config.getUrl(), apiVersion, endpointBase);
return client.post(url, OAuthSignature.getAsMap(config, url, HttpMethod.POST), object);
}
}