diff --git a/README.md b/README.md
index 58cfd24..79cbf36 100644
--- a/README.md
+++ b/README.md
@@ -5,25 +5,23 @@ See https://www.semantics3.com for more information.
## Installation
* The JAVA version used for this library is JDK 7.
-* To build and install from the latest source:
+* Add the following to your pom.xml
-```git clone git@github.com:Semantics3/semantics3-java.git```
-
-* Otherwise, each stable [release](https://github.com/Semantics3/semantics3-java/releases/download/v1.0.2/Sem3Java-1.0.2-SNAPSHOT-jar-with-dependencies.jar) is available as fat jar.
-
-
-## Requirements
-* [signpost-core-1.2.1.2.jar](https://oauth-signpost.googlecode.com/files/signpost-core-1.2.1.2.jar) - OAuth2 implementation
-
-* [commons-codec-1.7.jar](http://repo1.maven.org/maven2/commons-codec/commons-codec/1.7/commons-codec-1.7.jar) - Apache Commons Codec library for Base64 implementation
+```xml
+
+ com.semantics3
+ Sem3Java
+ 1.2.4
+
+```
-* JDK >= 6
+* For projects which don't use Maven each stable [release](https://github.com/Semantics3/semantics3-java/releases/download/Sem3Java-1.2.4/Sem3Java-1.2.4-jar-with-dependencies.jar) is also available as a fat jar.
## Getting Started
In order to use the client, you must have both an API key and an API secret. To obtain your key and secret, you need to first create an account at
https://www.semantics3.com/
-You can access your API access credentials from the user dashboard at https://www.semantics3.com/dashboard/applications
+You can access your API access credentials from the user dashboard at https://dashboard.semantics3.com.
### Setup Work
@@ -41,7 +39,7 @@ Products products = new Products(
### First Request aka 'Hello World':
-Let's make our first Request! We are going to run a simple search fo the word "iPhone" as follows:
+Let's make our first Request! We are going to run a simple search for the word "iPhone" as follows:
```java
/* Build the Request */
@@ -127,76 +125,76 @@ You can use webhooks to get near-real-time price updates from Semantics3.
You can register a webhook with Semantics3 by sending a POST request to ```"webhooks"``` endpoint. To verify that your URL is active, a GET request will be sent to your server with a ```verification_code``` parameter. Your server should respond with ```verification_code``` in the response body to complete the verification process.
-```
- String apiKey = "SEM3xxxxxxxxxxxxxxxxxxxxxx";
- String apiSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
- String endpoint = "webhooks";
- Semantics3Request request = new Semantics3Request(apiKey, apiSecret, endpoint);
- HashMap params = new HashMap();
- params.put("webhook_uri", "http://mydomain.com/webhooks-callback-url");
- JSONObject results = request.runQuery(endpoint, "POST", params);
- System.out.println(results.toString(4));
+```java
+String apiKey = "SEM3xxxxxxxxxxxxxxxxxxxxxx";
+String apiSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
+String endpoint = "webhooks";
+Semantics3Request request = new Semantics3Request(apiKey, apiSecret, endpoint);
+HashMap params = new HashMap();
+params.put("webhook_uri", "http://mydomain.com/webhooks-callback-url");
+JSONObject results = request.runQuery(endpoint, "POST", params);
+System.out.println(results.toString(4));
```
To fetch existing webhooks
-```
- String endpoint = "webhooks";
- Semantics3Request request = new Semantics3Request(apiKey, apiSecret, endpoint);
- HashMap params = new HashMap();
- JSONObject results = request.runQuery(endpoint, "GET", params);
- System.out.println(results.toString(4));
+```java
+String endpoint = "webhooks";
+Semantics3Request request = new Semantics3Request(apiKey, apiSecret, endpoint);
+HashMap params = new HashMap();
+JSONObject results = request.runQuery(endpoint, "GET", params);
+System.out.println(results.toString(4));
```
To remove a webhook
-```
- String webhookId = "7JcGN81u";
- String endpoint = "webhooks/" + webhookId;
- Semantics3Request request = new Semantics3Request(apiKey, apiSecret, endpoint);
-
- HashMap params = new HashMap();
+```java
+String webhookId = "7JcGN81u";
+String endpoint = "webhooks/" + webhookId;
+Semantics3Request request = new Semantics3Request(apiKey, apiSecret, endpoint);
+
+HashMap params = new HashMap();
- JSONObject results = request.runQuery(endpoint, "DELETE", params);
- System.out.println(results.toString(4));
+JSONObject results = request.runQuery(endpoint, "DELETE", params);
+System.out.println(results.toString(4));
```
### Registering events
Once you register a webhook, you can start adding events to it. Semantics3 server will send you notifications when these events occur. To register events for a specific webhook send a POST request to the ```"webhooks/{webhook_id}/events"``` endpoint
-```
- String webhookId = "7JcGN81u";
- String endpoint = "webhooks/" +webhookId+ "/events";
-
- Semantics3Request request = new Semantics3Request(apiKey, apiSecret, endpoint);
-
- HashMap params = new HashMap();
- HashMap product = new HashMap();
- HashMap constraints = new HashMap();
- params.put("type", "price.change");
- product.put("sem3_id", "1QZC8wchX62eCYS2CACmka");
- params.put("product", product);
- constraints.put("gte", 10);
- constraints.put("lte", 100);
- params.put("constraints", constraints);
- JSONObject results = request.runQuery(endpoint, "POST", params);
- System.out.println(results.toString(4));
+```java
+String webhookId = "7JcGN81u";
+String endpoint = "webhooks/" +webhookId+ "/events";
+
+Semantics3Request request = new Semantics3Request(apiKey, apiSecret, endpoint);
+
+HashMap params = new HashMap();
+HashMap product = new HashMap();
+HashMap constraints = new HashMap();
+params.put("type", "price.change");
+product.put("sem3_id", "1QZC8wchX62eCYS2CACmka");
+params.put("product", product);
+constraints.put("gte", 10);
+constraints.put("lte", 100);
+params.put("constraints", constraints);
+JSONObject results = request.runQuery(endpoint, "POST", params);
+System.out.println(results.toString(4));
```
To fetch all registered events for a give webhook
-```
- String webhookId = "7JcGN81u";
- String endpoint = "webhooks/" +webhookId+ "/events";
-
- Semantics3Request request = new Semantics3Request(apiKey, apiSecret, endpoint);
- HashMap params = new HashMap();
- JSONObject results = request.runQuery(endpoint, "GET", params);
+```java
+String webhookId = "7JcGN81u";
+String endpoint = "webhooks/" +webhookId+ "/events";
+
+Semantics3Request request = new Semantics3Request(apiKey, apiSecret, endpoint);
+HashMap params = new HashMap();
+JSONObject results = request.runQuery(endpoint, "GET", params);
```
## Webhook Notifications
Once you have created a webhook and registered events on it, notifications will be sent to your registered webhook URI via a POST request when the corresponding events occur. Make sure that your server can accept POST requests. Here is how a sample notification object looks like
-```
+```json
{
"type": "price.change",
"event_id": "XyZgOZ5q",
@@ -220,10 +218,10 @@ Use GitHub's standard fork/commit/pull-request cycle. If you have any questions
## Authors
-* Shawn Tan
-
* Asmit Kumar
+* Shawn Tan
+
## Copyright
Copyright (c) 2015 Semantics3 Inc.
diff --git a/pom.xml b/pom.xml
index bdfeee3..cea89bf 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,50 +4,124 @@
com.semantics3
Sem3Java
- 1.1.4-SNAPSHOT
+ 1.2.5-SNAPSHOT
Semantics3 Java Library
- http://maven.apache.org
+ https://github.com/Semantics3/semantics3-java
+ Java bindings for Semantics3 API
+
+ https://github.com/Semantics3/semantics3-java/issues
+ GitHub Issues
+
+
+
+ MIT License
+ http://www.opensource.org/licenses/mit-license.php
+ repo
+
+
+
+
+ abishek@semantics3.com
+ Abishek Bhat
+ https://github.com/abishekk92
+ abishekk92
+
+
+ asmit@semantics3.com
+ Asmit Kumar
+ https://github.com/asmitkumar
+ asmitkumar
+
+
+ Shawn Tan
+ https://github.com/shawntan
+ shawntan
+
+
org.apache.maven.plugins
- maven-assembly-plugin
- 2.2.1
+ maven-release-plugin
+ 2.5
-
- jar-with-dependencies
-
-
+ true
+ false
+ release
+ deploy -Dmaven.test.skip=true
+
+
+ org.sonatype.plugins
+ nexus-staging-maven-plugin
+ 1.6.3
+ true
+
+ ossrh
+ https://oss.sonatype.org/
+ true
+
+
+
+ org.apache.maven.plugins
+ maven-source-plugin
- assemble-all
- package
+ attach-sources
- single
+ jar
org.apache.maven.plugins
- maven-release-plugin
- 2.2.2
+ maven-javadoc-plugin
+
+
+ attach-javadocs
+
+ jar
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-gpg-plugin
+ 1.4
- deploy -Dmaven.test.skip=true
- -Dgpg.passphrase=${gpg.passphrase}
+ ${gpg.passphrase}
+
+
+ sign-artifacts
+ verify
+
+ sign
+
+
+
- org.sonatype.plugins
- nexus-staging-maven-plugin
- 1.6.3
- true
+ org.apache.maven.plugins
+ maven-assembly-plugin
+ 2.2.1
- ossrh
- https://oss.sonatype.org/
- true
+
+ jar-with-dependencies
+
+
+
+
+ assemble-all
+ package
+
+ single
+
+
+
@@ -69,42 +143,11 @@
1.10
-
-
- release-sign-artifacts
-
-
- performRelease
- true
-
-
-
-
-
- org.apache.maven.plugins
- maven-gpg-plugin
- 1.4
-
- ${gpg.passphrase}
-
-
-
- sign-artifacts
- verify
-
- sign
-
-
-
-
-
-
-
-
scm:git:git://github.com/Semantics3/semantics3-java.git
scm:git:git@github.com:Semantics3/semantics3-java.git
https://github.com/Semantics3/semantics3-java
+ HEAD
diff --git a/src/main/java/com/semantics3/api/Categories.java b/src/main/java/com/semantics3/api/Categories.java
index 22ad6aa..3c35149 100644
--- a/src/main/java/com/semantics3/api/Categories.java
+++ b/src/main/java/com/semantics3/api/Categories.java
@@ -1,13 +1,13 @@
package com.semantics3.api;
-import java.io.IOException;
-
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
-
import org.json.JSONObject;
+import java.io.IOException;
+import java.net.URISyntaxException;
+
public class Categories extends Semantics3Request {
public Categories(String apiKey, String apiSecret) {
@@ -19,7 +19,7 @@ public Categories field(Object... fields) {
return this;
}
- public JSONObject getCategories() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException {
+ public JSONObject getCategories() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException, URISyntaxException {
return this.get();
}
diff --git a/src/main/java/com/semantics3/api/Offers.java b/src/main/java/com/semantics3/api/Offers.java
index f592351..ba22a5d 100644
--- a/src/main/java/com/semantics3/api/Offers.java
+++ b/src/main/java/com/semantics3/api/Offers.java
@@ -1,13 +1,13 @@
package com.semantics3.api;
-import java.io.IOException;
-
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
-
import org.json.JSONObject;
+import java.io.IOException;
+import java.net.URISyntaxException;
+
public class Offers extends Semantics3Request {
public Offers(String apiKey, String apiSecret) {
@@ -19,7 +19,7 @@ public Offers field(Object... fields) {
return this;
}
- public JSONObject getOffers() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException {
+ public JSONObject getOffers() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException, URISyntaxException {
return this.get();
}
diff --git a/src/main/java/com/semantics3/api/Products.java b/src/main/java/com/semantics3/api/Products.java
index a52199e..711c834 100644
--- a/src/main/java/com/semantics3/api/Products.java
+++ b/src/main/java/com/semantics3/api/Products.java
@@ -1,14 +1,13 @@
package com.semantics3.api;
-import java.io.IOException;
-
+import com.semantics3.Util;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
-
import org.json.JSONObject;
-import com.semantics3.Util;
+import java.io.IOException;
+import java.net.URISyntaxException;
public class Products extends Semantics3Request {
@@ -17,13 +16,13 @@ public Products(String apiKey, String apiSecret) {
// TODO Auto-generated constructor stub
}
- public JSONObject getProducts() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException {
+ public JSONObject getProducts() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException, URISyntaxException {
return this.get();
}
- public JSONObject getOffers() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException {
+ public JSONObject getOffers() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException, URISyntaxException {
return this.get("offers");
}
- public JSONObject getCategories() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException {
+ public JSONObject getCategories() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException, URISyntaxException {
return this.get("categories");
}
diff --git a/src/main/java/com/semantics3/api/Semantics3Request.java b/src/main/java/com/semantics3/api/Semantics3Request.java
index 6e1d268..f3c3b87 100644
--- a/src/main/java/com/semantics3/api/Semantics3Request.java
+++ b/src/main/java/com/semantics3/api/Semantics3Request.java
@@ -13,6 +13,7 @@
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
+import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
@@ -72,10 +73,10 @@ public Semantics3Request(String apiKey, String apiSecret) {
}
protected JSONObject fetch(String endpoint, String params) throws
- OAuthMessageSignerException,
- OAuthExpectationFailedException,
- OAuthCommunicationException,
- IOException {
+ OAuthMessageSignerException,
+ OAuthExpectationFailedException,
+ OAuthCommunicationException,
+ IOException, URISyntaxException {
String req = new StringBuffer()
.append(API_BASE)
.append(endpoint)
@@ -83,27 +84,43 @@ protected JSONObject fetch(String endpoint, String params) throws
.append(URLEncoder.encode(params, "UTF-8"))
.toString();
URL url = new URL(req);
-
+ url = url.toURI().normalize().toURL();
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setRequestProperty("User-Agent", "Semantics3 Java Library");
consumer.sign(request);
request.connect();
- JSONObject json = new JSONObject(new JSONTokener(request.getInputStream()));
-
- return json;
+ try {
+ JSONObject json = new JSONObject(new JSONTokener(request.getInputStream()));
+ if (!json.has("code")){
+ json.put("code", "OK");
+ }
+ return json;
+ }
+ catch (IOException e) {
+ InputStream error = ((HttpURLConnection) request).getErrorStream();
+ JSONObject json = new JSONObject(new JSONTokener(error));
+ if (!json.has("code")) {
+ json.put("code", "Error");
+ }
+ else {
+ Object value = json.get("code");
+ json.put("code", value.toString());
+ }
+ return json;
+ }
}
protected JSONObject fetch(String endpoint, String method, HashMap params) throws
OAuthMessageSignerException,
OAuthExpectationFailedException,
OAuthCommunicationException,
- IOException {
+ IOException, URISyntaxException {
String req = new StringBuffer()
.append(API_BASE)
.append(endpoint)
.toString();
URL url = new URL(req);
-
+ url = url.toURI().normalize().toURL();
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setRequestProperty("User-Agent", "Semantics3 Java Library");
if(method == "POST") {
@@ -134,7 +151,13 @@ protected JSONObject fetch(String endpoint, String method, HashMap params) throws
+ public JSONObject runQuery(String endpoint, String method, HashMap params) throws
OAuthMessageSignerException,
OAuthExpectationFailedException,
OAuthCommunicationException,
- IOException {
+ IOException, URISyntaxException {
+ if(method == "GET"){
+ JSONObject jsonObject = new JSONObject(params);
+ this.query.put(endpoint, jsonObject);
+ this.runQuery(endpoint);
+
+ }
+ else {
this.queryResult = fetch(endpoint, method, params);
+ }
if (!this.queryResult.getString("code").equals("OK")) {
throw new Semantics3Exception(
this.queryResult.getString("code"),
@@ -233,15 +264,15 @@ protected JSONObject runQuery(String endpoint, String method, HashMap 0, is(true)); } @Test
+ public void TestSKUQuery() throws OAuthExpectationFailedException, OAuthCommunicationException, OAuthMessageSignerException, IOException, URISyntaxException {
+ Properties property = TestUtils.getConfig("api.config");
+ Semantics3Request semantics3Request = new Semantics3Request(property.get("API_KEY").toString(), property.get("API_SECRET").toString());
+ HashMap params = new HashMap();
+ params.put("site", "abercrombie.com");
+ JSONObject jsonObject = semantics3Request.runQuery("skus", "GET", params);
+ JSONArray resultsArray = (JSONArray) jsonObject.get("results");
assertThat(resultsArray.length() > 0, is(true));
}
+
+ @Test(expected = Semantics3Exception.class)
+ public void TestSiteQuery() throws IOException, OAuthExpectationFailedException, OAuthCommunicationException, OAuthMessageSignerException, URISyntaxException {
+ Properties property = TestUtils.getConfig("api.config");
+ Products products = new Products(property.get("API_KEY").toString(), property.get("API_SECRET").toString());
+ products .productsField("url", "zsdfseobn.com/sdgeafg");
+ JSONObject results = products.getProducts();
+ }
+
+ @Ignore
+ public void TestPagination() throws IOException, OAuthCommunicationException, OAuthExpectationFailedException, OAuthMessageSignerException, URISyntaxException {
+ Properties property = TestUtils.getConfig("api.config");
+ Semantics3Request semantics3Request = new Semantics3Request(property.get("API_KEY").toString(), property.get("API_SECRET").toString());
+ HashMap params = new HashMap();
+ params.put("site", "macys.com");
+ JSONObject jsonObject = semantics3Request.runQuery("skus", "GET", params);
+ while(jsonObject.has("next")){
+ JSONObject next = (JSONObject)jsonObject.get("next");
+ String page = next.getString("page");
+ System.out.println(page);
+ params.remove("site");
+ params.put("page", page);
+ jsonObject = semantics3Request.runQuery("skus", "GET", params);
+ System.out.println(jsonObject.toString());
+ }
+ }
}
diff --git a/src/test/java/com/semantics3/api/Semantics3RequestTest.java b/src/test/java/com/semantics3/api/Semantics3RequestTest.java
index 6888c70..135dc1f 100644
--- a/src/test/java/com/semantics3/api/Semantics3RequestTest.java
+++ b/src/test/java/com/semantics3/api/Semantics3RequestTest.java
@@ -4,12 +4,12 @@
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
-import org.hamcrest.CoreMatchers;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import java.io.IOException;
+import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Properties;
@@ -22,11 +22,11 @@
*/
public class Semantics3RequestTest {
@Test
- public void testRunQueryPost() throws IOException, OAuthCommunicationException, OAuthExpectationFailedException, OAuthMessageSignerException {
+ public void testRunQueryPost() throws IOException, OAuthCommunicationException, OAuthExpectationFailedException, OAuthMessageSignerException, URISyntaxException {
Properties property = TestUtils.getConfig("api.config");
Semantics3Request sem3 = new Semantics3Request(property.get("API_KEY").toString(), property.get("API_SECRET").toString());
HashMap params = new HashMap();
- params.put("webhook_uri", "http://148.251.44.168:5000");
+ params.put("webhook_uri", " http://sem3-webhooks-verification.ngrok.com");
try {
JSONObject result = sem3.runQuery("webhooks", "POST", params);
assertThat(result.getString("code"), equalTo("OK"));
@@ -37,17 +37,17 @@ public void testRunQueryPost() throws IOException, OAuthCommunicationException,
}
@Test
- public void testRunQueryGet() throws IOException, OAuthCommunicationException, OAuthExpectationFailedException, OAuthMessageSignerException {
+ public void testRunQueryGet() throws IOException, OAuthCommunicationException, OAuthExpectationFailedException, OAuthMessageSignerException, URISyntaxException {
Properties property = TestUtils.getConfig("api.config");
Semantics3Request sem3 = new Semantics3Request(property.get("API_KEY").toString(), property.get("API_SECRET").toString());
HashMap params = new HashMap();
JSONObject result = sem3.runQuery("webhooks", "GET", params);
- assertThat(result.getJSONArray("data").length(), CoreMatchers.anyOf(equalTo(0), equalTo(1)));
+ assertThat(result.getString("code"), equalTo("OK"));
}
@Test
- public void testRunQueryDelete() throws IOException, OAuthCommunicationException, OAuthExpectationFailedException, OAuthMessageSignerException {
+ public void testRunQueryDelete() throws IOException, OAuthCommunicationException, OAuthExpectationFailedException, OAuthMessageSignerException, URISyntaxException {
Properties property = TestUtils.getConfig("api.config");
Semantics3Request sem3 = new Semantics3Request(property.get("API_KEY").toString(), property.get("API_SECRET").toString());
HashMap params = new HashMap();
@@ -61,4 +61,6 @@ public void testRunQueryDelete() throws IOException, OAuthCommunicationException
assertThat(response.getString("code"), equalTo("OK"));
}
}
+
+
}
diff --git a/src/test/resources/api.config.sample b/src/test/resources/api.config.sample
index 2f16042..a0a986b 100644
--- a/src/test/resources/api.config.sample
+++ b/src/test/resources/api.config.sample
@@ -1,2 +1,4 @@
API_KEY=
API_SECRET=
+SKU_API_KEY=
+SKU_API_SECRET=