Skip to content

Commit

Permalink
Refactored AbstractApi to moved news methods to the NewsApi.
Browse files Browse the repository at this point in the history
  • Loading branch information
gracg committed Apr 13, 2022
1 parent 4a1cad8 commit 2c7fc4e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
14 changes: 0 additions & 14 deletions src/main/java/nl/capite/tiingo4j/abstracts/AbstractApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,6 @@ protected String csvString(List<String> strings) {
}


protected List<Article> getNews(NewsParameters parameters) throws IOException, ApiException {
final String url = "https://api.tiingo.com/tiingo/news";
Request request = createRequest(url,parameters);

Response response = client.newCall(request).execute();
String body = response.body().string();

if(!isStatus2XX(response.code())) {
throw parseError(body,null);
}

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

protected List<CryptoTopOfTheBook> getCryptoTopOfTheBook(List<String> tickers, CryptoTopOfTheBookParameters parameters) throws IOException, ApiException {
final String url = "https://api.tiingo.com/tiingo/crypto/top";
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/nl/capite/tiingo4j/apis/NewsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import nl.capite.tiingo4j.exceptions.ApiException;
import nl.capite.tiingo4j.models.Article;
import nl.capite.tiingo4j.requestParameters.NewsParameters;
import okhttp3.Request;
import okhttp3.Response;

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

public class NewsApi extends AbstractApi {
Expand All @@ -15,6 +19,17 @@ public NewsApi(String apiKey) {
}

public List<Article> getNews(NewsParameters parameters) throws IOException, ApiException {
return super.getNews(parameters);
final String url = "https://api.tiingo.com/tiingo/news";
Request request = createRequest(url,parameters);

Response response = client.newCall(request).execute();
String body = response.body().string();

if(!isStatus2XX(response.code())) {
throw parseError(body,null);
}

var x = mapper.readValue(body,Article[].class);
return x==null?new ArrayList<>(): Arrays.asList(x);
}
}
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 @@ -14,10 +14,12 @@
public class TiingoApi extends AbstractApi {

private final StockApi stockApi;
private final NewsApi newsApi;

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

public Optional<Meta> getMeta(String ticker) throws IOException, ApiException {
Expand All @@ -29,7 +31,7 @@ public List<Price> getPrices(String ticker, PriceParameters parameters) throws I
}

public List<Article> getNews(NewsParameters parameters) throws IOException, ApiException {
return super.getNews(parameters);
return newsApi.getNews(parameters);
}

public List<CryptoTopOfTheBook> getCryptoTopOfTheBook(List<String> tickers, CryptoTopOfTheBookParameters parameters) throws IOException, ApiException {
Expand Down

0 comments on commit 2c7fc4e

Please sign in to comment.