Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.
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
25 changes: 25 additions & 0 deletions src/main/java/com/semantics3/api/ConnectionBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.semantics3.api;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;

public class ConnectionBuilder {
private static final String USER_AGENT_KEY = "User-Agent";
private static final String USER_AGENT_VALUE = "Semantics3 Java Library";

public HttpURLConnection buildConnection(String req, ConnectionProperties connectionProperties)
throws IOException, URISyntaxException {
URL url = new URL(req);
url = url.toURI().normalize().toURL();

HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.setRequestProperty(USER_AGENT_KEY, USER_AGENT_VALUE);

request.setConnectTimeout(connectionProperties.getConnectionTimeout());
request.setReadTimeout(connectionProperties.getReadTimeout());

return request;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/semantics3/api/ConnectionProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.semantics3.api;

public class ConnectionProperties {
private static final int DEFAULT_NO_TIMEOUT = 0;

private int readTimeout = DEFAULT_NO_TIMEOUT;
private int connectionTimeout = DEFAULT_NO_TIMEOUT;


public int getReadTimeout() {
return readTimeout;
}

public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}

public int getConnectionTimeout() {
return connectionTimeout;
}

public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
}
63 changes: 29 additions & 34 deletions src/main/java/com/semantics3/api/Semantics3Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

Expand All @@ -30,46 +30,41 @@ public class Semantics3Request{
private HashMap<String,JSONObject> query = new HashMap<String, JSONObject>();
private JSONObject queryResult;
private StringBuffer urlBuilder;

public Semantics3Request(String apiKey, String apiSecret, String endpoint) {
if (apiKey == null) {
private ConnectionProperties connectionProperties;

public Semantics3Request(String apiKey, String apiSecret, String endpoint, ConnectionProperties connectionProperties) {
if (apiKey == null) {
throw new Semantics3Exception(
"API Credentials Missing",
"You did not supply an apiKey. Please sign up at https://semantics3.com/ to obtain your api_key."
);
);
}
if (apiSecret == null) {
if (apiSecret == null) {
throw new Semantics3Exception(
"API Credentials Missing",
"You did not supply an apiSecret. Please sign up at https://semantics3.com/ to obtain your api_key."
);
);
}

this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.endpoint = endpoint;
this.consumer = new DefaultOAuthConsumer(apiKey, apiSecret);
consumer.setTokenWithSecret("", "");

this.endpoint = endpoint;

this.connectionProperties = connectionProperties;
}

public Semantics3Request(String apiKey, String apiSecret) {
if (apiKey == null) {
throw new Semantics3Exception(
"API Credentials Missing",
"You did not supply an apiKey. Please sign up at https://semantics3.com/ to obtain your api_key."
);
}
if (apiSecret == null) {
throw new Semantics3Exception(
"API Credentials Missing",
"You did not supply an apiSecret. Please sign up at https://semantics3.com/ to obtain your api_key."
);
}
public Semantics3Request(String apiKey, String apiSecret, ConnectionProperties connectionProperties) {
this(apiKey, apiSecret, null, connectionProperties);
}

public Semantics3Request(String apiKey, String apiSecret, String endpoint) {
this(apiKey, apiSecret, endpoint, new ConnectionProperties());
}

this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.consumer = new DefaultOAuthConsumer(apiKey, apiSecret);
consumer.setTokenWithSecret("", "");
public Semantics3Request(String apiKey, String apiSecret) {
this(apiKey, apiSecret, null, new ConnectionProperties());
}

protected JSONObject fetch(String endpoint, String params) throws
Expand All @@ -83,10 +78,7 @@ protected JSONObject fetch(String endpoint, String params) throws
.append("?q=")
.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");
HttpURLConnection request = new ConnectionBuilder().buildConnection(req, this.connectionProperties);
consumer.sign(request);
request.connect();
try {
Expand All @@ -96,6 +88,9 @@ protected JSONObject fetch(String endpoint, String params) throws
}
return json;
}
catch (SocketTimeoutException e) {
throw e;
}
catch (IOException e) {
InputStream error = ((HttpURLConnection) request).getErrorStream();
JSONObject json = new JSONObject(new JSONTokener(error));
Expand All @@ -119,10 +114,7 @@ protected JSONObject fetch(String endpoint, String method, HashMap<String, Objec
.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");
HttpURLConnection request = new ConnectionBuilder().buildConnection(req, this.connectionProperties);
if(method == "POST") {
request.setDoInput(true);
request.setDoOutput(true);
Expand All @@ -148,6 +140,9 @@ protected JSONObject fetch(String endpoint, String method, HashMap<String, Objec
}
return json;
}
catch (SocketTimeoutException e) {
throw e;
}
catch (IOException e) {
InputStream error = ((HttpURLConnection) request).getErrorStream();
JSONObject json = new JSONObject(new JSONTokener(error));
Expand Down