Skip to content

Commit

Permalink
Refactored AbstractApi to moved crypto methods to the CryptoApi.
Browse files Browse the repository at this point in the history
  • Loading branch information
gracg committed Apr 13, 2022
1 parent 2c7fc4e commit f26608e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 35 deletions.
34 changes: 0 additions & 34 deletions src/main/java/nl/capite/tiingo4j/abstracts/AbstractApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,38 +92,4 @@ protected String csvString(List<String> strings) {
return sb.toString();
}



protected List<CryptoTopOfTheBook> getCryptoTopOfTheBook(List<String> tickers, CryptoTopOfTheBookParameters parameters) throws IOException, ApiException {
final String url = "https://api.tiingo.com/tiingo/crypto/top";



if(tickers==null) {
throw new NullPointerException("tickers parameters cannot be null.");
}
if(tickers.size()==0) {
throw new NullPointerException("tickers list must include at least one ticker.");
}

HashMap<String,String> params = new HashMap<>();
if(parameters!=null) {
params = parameters.getMap();
}

String tickerStringList = csvString(tickers);
params.put("tickers",tickerStringList);

Request request = createRequestFromMap(url,params);

Response response = client.newCall(request).execute();
String body = response.body().string();
if(!isStatus2XX(response.code())) {
throw parseError(body,null);
}

CryptoTopOfTheBook[] data = mapper.readValue(body,CryptoTopOfTheBook[].class);
return data==null ? new ArrayList<>():Arrays.asList(data);
}

}
53 changes: 53 additions & 0 deletions src/main/java/nl/capite/tiingo4j/apis/CryptoApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package nl.capite.tiingo4j.apis;

import nl.capite.tiingo4j.abstracts.AbstractApi;
import nl.capite.tiingo4j.exceptions.ApiException;
import nl.capite.tiingo4j.models.CryptoTopOfTheBook;
import nl.capite.tiingo4j.requestParameters.CryptoTopOfTheBookParameters;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class CryptoApi extends AbstractApi {

public CryptoApi(String apiKey) {
super(apiKey);
}

public List<CryptoTopOfTheBook> getCryptoTopOfTheBook(List<String> tickers, CryptoTopOfTheBookParameters parameters) throws IOException, ApiException {
final String url = "https://api.tiingo.com/tiingo/crypto/top";



if(tickers==null) {
throw new NullPointerException("tickers parameters cannot be null.");
}
if(tickers.size()==0) {
throw new NullPointerException("tickers list must include at least one ticker.");
}

HashMap<String,String> params = new HashMap<>();
if(parameters!=null) {
params = parameters.getMap();
}

String tickerStringList = csvString(tickers);
params.put("tickers",tickerStringList);

Request request = createRequestFromMap(url,params);

Response response = client.newCall(request).execute();
String body = response.body().string();
if(!isStatus2XX(response.code())) {
throw parseError(body,null);
}

CryptoTopOfTheBook[] data = mapper.readValue(body,CryptoTopOfTheBook[].class);
return data==null ? new ArrayList<>(): Arrays.asList(data);
}
}
4 changes: 3 additions & 1 deletion src/main/java/nl/capite/tiingo4j/apis/TiingoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ public class TiingoApi extends AbstractApi {

private final StockApi stockApi;
private final NewsApi newsApi;
private final CryptoApi cryptoApi;

public TiingoApi(String apiKey) {
super(apiKey);
stockApi = new StockApi(apiKey);
newsApi = new NewsApi(apiKey);
cryptoApi = new CryptoApi(apiKey);
}

public Optional<Meta> getMeta(String ticker) throws IOException, ApiException {
Expand All @@ -35,7 +37,7 @@ public List<Article> getNews(NewsParameters parameters) throws IOException, ApiE
}

public List<CryptoTopOfTheBook> getCryptoTopOfTheBook(List<String> tickers, CryptoTopOfTheBookParameters parameters) throws IOException, ApiException {
return super.getCryptoTopOfTheBook(tickers,parameters);
return cryptoApi.getCryptoTopOfTheBook(tickers,parameters);
}

}

0 comments on commit f26608e

Please sign in to comment.