Skip to content
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
119 changes: 67 additions & 52 deletions src/main/java/org/patriques/AlphaVantageConnector.java
Original file line number Diff line number Diff line change
@@ -1,72 +1,87 @@
package org.patriques;

import org.patriques.input.ApiParameter;
import org.patriques.input.ApiParameterBuilder;
import org.patriques.output.AlphaVantageException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;

import org.patriques.input.ApiParameter;
import org.patriques.input.ApiParameterBuilder;
import org.patriques.output.AlphaVantageException;

/**
* Connection to Alpha Vantage API.
*
* @see ApiConnector
*/
public class AlphaVantageConnector implements ApiConnector {
private static final String BASE_URL = "https://www.alphavantage.co/query?";
private final String apiKey;
private final int timeOut;
private static final String BASE_URL = "https://www.alphavantage.co/query?";
private final String apiKey;
private final int timeOut;
private final Proxy proxy;

/**
* Creates an AlphaVantageConnector.
*
* @param apiKey the secret key to access the api.
* @param timeOut the timeout for when reading the connection should give up.
*/
public AlphaVantageConnector(String apiKey, int timeOut) {
this(apiKey, timeOut, null);
}

/**
* Creates an AlphaVantageConnector.
*
* @param apiKey the secret key to access the api.
* @param timeOut the timeout for when reading the connection should give up.
*/
public AlphaVantageConnector(String apiKey, int timeOut) {
this.apiKey = apiKey;
this.timeOut = timeOut;
}
/**
* Creates an AlphaVantageConnector.
*
* @param apiKey the secret key to access the api.
* @param timeOut the timeout for when reading the connection should give up.
* @param proxy the proxy to connect via. may be null.
*/
public AlphaVantageConnector(String apiKey, int timeOut, Proxy proxy) {
this.apiKey = apiKey;
this.timeOut = timeOut;
this.proxy = proxy;
}

@Override
public String getRequest(ApiParameter... apiParameters) {
String params = getParameters(apiParameters);
try {
URL request = new URL(BASE_URL + params);
URLConnection connection = request.openConnection();
connection.setConnectTimeout(timeOut);
connection.setReadTimeout(timeOut);
@Override
public String getRequest(ApiParameter... apiParameters) {
String params = getParameters(apiParameters);
try {
URL request = new URL(BASE_URL + params);
URLConnection connection = proxy == null ? request.openConnection() : request.openConnection(proxy);
connection.setConnectTimeout(timeOut);
connection.setReadTimeout(timeOut);

InputStreamReader inputStream = new InputStreamReader(connection.getInputStream(), "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStream);
StringBuilder responseBuilder = new StringBuilder();
InputStreamReader inputStream = new InputStreamReader(connection.getInputStream(), "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStream);
StringBuilder responseBuilder = new StringBuilder();

String line;
while ((line = bufferedReader.readLine()) != null) {
responseBuilder.append(line);
}
bufferedReader.close();
return responseBuilder.toString();
} catch (IOException e) {
throw new AlphaVantageException("failure sending request", e);
}
}
String line;
while ((line = bufferedReader.readLine()) != null) {
responseBuilder.append(line);
}
bufferedReader.close();
return responseBuilder.toString();
} catch (IOException e) {
throw new AlphaVantageException("failure sending request", e);
}
}

/**
* Builds up the url query from the api parameters used to append to the base url.
*
* @param apiParameters the api parameters used in the query
* @return the query string to use in the url
*/
private String getParameters(ApiParameter... apiParameters) {
ApiParameterBuilder urlBuilder = new ApiParameterBuilder();
for (ApiParameter parameter : apiParameters) {
urlBuilder.append(parameter);
}
urlBuilder.append("apikey", apiKey);
return urlBuilder.getUrl();
}
/**
* Builds up the url query from the api parameters used to append to the base
* url.
*
* @param apiParameters the api parameters used in the query
* @return the query string to use in the url
*/
private String getParameters(ApiParameter... apiParameters) {
ApiParameterBuilder urlBuilder = new ApiParameterBuilder();
for (ApiParameter parameter : apiParameters) {
urlBuilder.append(parameter);
}
urlBuilder.append("apikey", apiKey);
return urlBuilder.getUrl();
}
}
33 changes: 33 additions & 0 deletions src/test/java/org/patriques/AlphaVantageConnectorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.patriques;

import static org.junit.Assert.assertEquals;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;

import org.junit.Test;
import org.patriques.input.timeseries.OutputSize;
import org.patriques.output.exchange.Daily;

/**
* @author Kilian
*
*/
public class AlphaVantageConnectorTest {

@Test
public void testProxy() {
String apiKey = "50M3AP1K3Y";
int timeout = 3000;
AlphaVantageConnector apiConnector = new AlphaVantageConnector(apiKey, timeout);

Proxy proxy = new Proxy(Type.HTTP,new InetSocketAddress("178.128.153.253",3128));
AlphaVantageConnector proxyapiConnector = new AlphaVantageConnector(apiKey, timeout,proxy);

Daily x = new ForeignExchange(apiConnector).daily("EUR","USD",OutputSize.COMPACT);
Daily y = new ForeignExchange(proxyapiConnector).daily("EUR","USD",OutputSize.COMPACT);
assertEquals(x.getMetaData(),y.getMetaData());
}

}