Skip to content

Commit

Permalink
refactor exchange rate internals, set up unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crazzyghost committed May 3, 2020
1 parent 901b804 commit d8227c9
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 38 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[![CircleCI](https://circleci.com/gh/crazzyghost/alphavantage-java/tree/master.svg?style=shield)](https://circleci.com/gh/crazzyghost/alphavantage-java/tree/master)
[![codecov](https://codecov.io/gh/crazzyghost/alphavantage-java/branch/master/graph/badge.svg)](https://codecov.io/gh/crazzyghost/alphavantage-java)
[![](https://jitpack.io/v/crazzyghost/alphavantage-java.svg)](https://jitpack.io/#crazzyghost/alphavantage-java)


I created this wrapper to make accessing the [AlphaVantage API](https://www.alphavantage.co/) with Java fairly simple and fun. Make sure to get an [API key](https://www.alphavantage.co/support/#api-key) from Alphavantage's website before continuing.

Expand Down Expand Up @@ -51,6 +53,7 @@ The available API categories to select from currently are: Stock Time Series, Fo
| Crypto Currency Data | `.crypto()` |
| Technical Indicators | `.indicator()` |

For example
```java
//selecting stock time series category
AlphaVantage.api()
Expand Down Expand Up @@ -104,7 +107,7 @@ AlphaVantage.api()
.fetch();
...
void handleSuccess(TimeSeriesResponse e){
plotGraph(e.getStockUnits(), e.getMetaData())
plotGraph(e.getStockUnits(), e.getMetaData());
}

```
Expand All @@ -127,7 +130,7 @@ AlphaVantage.api()
.exchangeRate()
.fromCurrency("USD")
.toCurrency("GHS")
.onSuccess((ExchangeRateResponse e) -> handleSucess(e))
.onSuccess((ExchangeRateResponse e) -> handleSuccess(e))
.fetch();
```

Expand All @@ -138,7 +141,7 @@ AlphaVantage.api()
.daily()
.symbol("BTC")
.market("CNY")
.onSuccess(response -> handleSucess(response.getCryptoUnits()))
.onSuccess(response -> handleSuccess(response.getCryptoUnits()))
.fetch();
```
#### Fetching Technical Indicator Data
Expand All @@ -150,6 +153,6 @@ AlphaVantage.api()
.interval(Interval.THIRTY_MIN)
.seriesType(SeriesType.HIGH)
.timePeriod(200)
.onSuccess(e -> handleSucess(e))
.onSuccess(e -> handleSuccess(e))
.fetch();
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.crazzyghost.alphavantage.AlphaVantageException;
import com.crazzyghost.alphavantage.Config;
Expand All @@ -14,7 +13,6 @@
import com.squareup.moshi.Types;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

Expand All @@ -25,15 +23,11 @@ public class ExchangeRate implements Fetcher {
private ExchangeRateRequest.Builder builder;
private Fetcher.SuccessCallback<ExchangeRateResponse> successCallback;
private Fetcher.FailureCallback failureCallback;
private OkHttpClient client;

public ExchangeRate(Config config){
this.config = config;
this.request = null;
this.builder = ExchangeRateRequest.builder();
client = new OkHttpClient.Builder()
.connectTimeout(config.getTimeOut(), TimeUnit.SECONDS)
.build();
}

public ExchangeRate toCurrency(String toCurrency){
Expand Down Expand Up @@ -67,7 +61,7 @@ public ExchangeRate onFailure(FailureCallback callback){
@Override
public void fetch() {

if(config.getKey() == null){
if(config == null || config.getKey() == null){
throw new AlphaVantageException("Config not set");
}
this.request = this.builder.build();
Expand All @@ -76,7 +70,7 @@ public void fetch() {
.url(Config.BASE_URL + UrlExtractor.extract(this.request) + config.getKey())
.build();

client.newCall(request).enqueue(new okhttp3.Callback() {
config.getOkHttpClient().newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
if(failureCallback != null){
Expand All @@ -96,7 +90,6 @@ public void onResponse(Call call, Response response) throws IOException {
if(failureCallback != null){
failureCallback.onFailure(new AlphaVantageException(exchangeResponse.getErrorMessage()));
}
return;
}
if(successCallback != null)
successCallback.onSuccess(exchangeResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,24 @@

public class ExchangeRateRequest {


private Function function;
private String from_currency;
private String to_currency;


private ExchangeRateRequest(Builder builder){
this.function = builder.function;
this.from_currency = builder.fromCurrency;
this.to_currency = builder.toCurrency;
}

public Function getFunction(){
return this.function;
}

public String getFromCurrency(){
return this.from_currency;
}

public String getToCurrency(){
return this.to_currency;
}

public static Builder builder(){
return new Builder();
}

public static class Builder{
Function function;
String fromCurrency;
String toCurrency;
private Function function;
private String fromCurrency;
private String toCurrency;

public Builder(){
this.function = Function.CURRENCY_EXCHANGE_RATE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ public class ExchangeRateResponse {
private String fromCurrencyName;
private String toCurrencyCode;
private String toCurrencyName;
private Double exchangeRate;
private double exchangeRate;
private String lastRefreshed;
private String timeZone;
private double bidPrice;
private double askPrice;

private String errorMessage;

Expand All @@ -23,14 +25,19 @@ private ExchangeRateResponse(
String toCurrencyName,
Double exchangeRate,
String lastRefreshed,
String timeZone) {
String timeZone,
double bidPrice,
double askPrice
) {
this.fromCurrencyCode = fromCurrencyCode;
this.fromCurrencyName = fromCurrencyName;
this.toCurrencyCode = toCurrencyCode;
this.toCurrencyName = toCurrencyName;
this.exchangeRate = exchangeRate;
this.lastRefreshed = lastRefreshed;
this.timeZone = timeZone;
this.bidPrice = bidPrice;
this.askPrice = askPrice;
this.errorMessage = null;
}

Expand All @@ -49,30 +56,28 @@ public String getErrorMessage() {

public static class Parser {


@SuppressWarnings("unchecked")
ExchangeRateResponse parse(Map<String, Object> stringObjectMap) {

//get the keys
List<String> keys = new ArrayList<>(stringObjectMap.keySet());

Map<String, String> md;

try{
md = (Map<String, String>) stringObjectMap.get(keys.get(0));

}catch (ClassCastException e){
return new ExchangeRateResponse((String)stringObjectMap.get(keys.get(0)));
}


return new ExchangeRateResponse(
md.get("1. From_Currency Code"),
md.get("2. From_Currency Name"),
md.get("3. To_Currency Code"),
md.get("4. To_Currency Name"),
Double.parseDouble(md.get("5. Exchange Rate")),
md.get("6. Last Refreshed"),
md.get("7. Time Zone")
md.get("7. Time Zone"),
Double.parseDouble(md.get("8. Bid Price")),
Double.parseDouble(md.get("9. Ask Price"))
);

}
Expand All @@ -88,6 +93,8 @@ public String toString() {
", exchangeRate=" + exchangeRate +
", lastRefreshed='" + lastRefreshed + '\'' +
", timeZone='" + timeZone + '\'' +
", bidPrice='" + bidPrice + '\'' +
", askPrice='" + askPrice+ '\'' +
", errorMessage='" + errorMessage + '\'' +
'}';
}
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/exchangerate/ExchangeRateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package exchangerate;
import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class ExchangeRateTest {

@Before
public void setUp(){

}

@Test
public void testConfigNotSet(){
assertEquals(5, 4);
}

@Test
public void testConfigKeyNotSet(){
assertEquals(5, 4);
}

@Test
public void testResponseFailure(){
assertEquals(5, 4);
}


@Test
public void testRequest(){
assertEquals(5, 4);
}

@Test
public void testResponse(){
assertEquals(5, 4);
}

@Test
public void testExchangeRate(){
assertEquals(5, 4);
}

@Test
public void testExchangeRateError(){
assertEquals(5, 4);
}

@Test
public void testExchangeRateErrorWithoutFailureCallback(){
assertEquals(5, 4);
}

}
13 changes: 13 additions & 0 deletions src/test/java/exchangerate/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Realtime Currency Exchange Rate": {
"1. From_Currency Code": "BTC",
"2. From_Currency Name": "Bitcoin",
"3. To_Currency Code": "CNY",
"4. To_Currency Name": "Chinese Yuan",
"5. Exchange Rate": "62423.68125000",
"6. Last Refreshed": "2020-05-03 20:32:20",
"7. Time Zone": "UTC",
"8. Bid Price": "62423.68125000",
"9. Ask Price": "62423.75188500"
}
}

0 comments on commit d8227c9

Please sign in to comment.