Skip to content
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

Relating BUG #35 #38

Open
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>

<build>
Expand Down
49 changes: 34 additions & 15 deletions src/main/java/com/icoderman/woocommerce/DefaultHttpClient.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package com.icoderman.woocommerce;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -94,13 +92,11 @@ public Map delete(String url, Map<String, String> params) {
}

private Map postEntity(Map<String, Object> objectForJson, HttpEntityEnclosingRequestBase httpPost) {
try {
HttpEntity entity = new ByteArrayEntity(this.mapper.writeValueAsBytes(objectForJson), ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
return getEntityAndReleaseConnection(httpPost, Map.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Gson gson = new Gson();
System.out.println(gson.toJson(objectForJson));
HttpEntity entity = new StringEntity(gson.toJson(objectForJson), ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
return getEntityAndReleaseConnection(httpPost, Map.class);
}

private List<NameValuePair> getParametersAsList(Map<String, String> params) {
Expand All @@ -117,6 +113,7 @@ private <T> T getEntityAndReleaseConnection(HttpRequestBase httpRequest, Class<T
try {
HttpResponse httpResponse = httpClient.execute(httpRequest);
HttpEntity httpEntity = httpResponse.getEntity();

if (httpEntity == null) {
throw new RuntimeException("Error retrieving results from http request");
}
Expand All @@ -131,4 +128,26 @@ private <T> T getEntityAndReleaseConnection(HttpRequestBase httpRequest, Class<T
httpRequest.releaseConnection();
}
}

public String InputToString(InputStream in) throws IOException {
try {
StringBuilder stringBuilder = new StringBuilder();
Reader reader = new InputStreamReader(in);
int data = reader.read();


while (data != -1) {
stringBuilder.append((char) data);
data = reader.read();
}
return stringBuilder.toString();
} catch (IOException ex) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
throw ex;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class WooCommerceClientTest {

@Before
public void setUp() {
wooCommerce = new WooCommerceAPI(new OAuthConfig(WC_URL, CONSUMER_KEY, CONSUMER_SECRET), ApiVersionType.V2);
OAuthConfig config = new OAuthConfig(WC_URL, CONSUMER_KEY, CONSUMER_SECRET);
wooCommerce = new WooCommerceAPI(config, ApiVersionType.V2);
}

@Ignore
Expand All @@ -35,8 +36,12 @@ public void apiCreateProductTest() {
Map<String, Object> productInfo = new HashMap<>();
productInfo.put("name", "Premium Quality");
productInfo.put("type", "simple");
productInfo.put("regular_price", "21.99");
productInfo.put("regular_price", "168.06");
productInfo.put("downloadable", true);
productInfo.put("download_expiry", "30");
productInfo.put("virtual", true);
productInfo.put("description", "Pellentesque habitant morbi tristique senectus et netus");

Map product = wooCommerce.create(EndpointBaseType.PRODUCTS.getValue(), productInfo);
Assert.assertNotNull(product);
}
Expand Down