-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #386 from bounswe/financial_data_endpoints
Implement YahooFinance endpoints.
- Loading branch information
Showing
9 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/com/bounswe2024group10/Tradeverse/controller/AssetController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.bounswe2024group10.Tradeverse.controller; | ||
|
||
import com.bounswe2024group10.Tradeverse.dto.asset.*; | ||
import com.bounswe2024group10.Tradeverse.service.AssetService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/api/asset") | ||
public class AssetController { | ||
@Autowired | ||
private AssetService assetService; | ||
|
||
@CrossOrigin(origins = "*", allowedHeaders = "*") | ||
@GetMapping("/details") | ||
public ResponseEntity<GetAssetDetailsResponse> getAssetDetails(@RequestBody GetAssetDetailsRequest request) { | ||
GetAssetDetailsResponse response = assetService.getAssetDetails(request); | ||
return ResponseEntity.ok(response); | ||
} | ||
|
||
@CrossOrigin(origins = "*", allowedHeaders = "*") | ||
@GetMapping("/chart") | ||
public ResponseEntity<GetAssetChartResponse> getAssetChart(@RequestBody GetAssetChartRequest request) { | ||
GetAssetChartResponse response = assetService.getAssetChart(request); | ||
return ResponseEntity.ok(response); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
backend/src/main/java/com/bounswe2024group10/Tradeverse/dto/asset/ChartData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.bounswe2024group10.Tradeverse.dto.asset; | ||
|
||
public class ChartData { | ||
private Long timestamp; | ||
private float high; | ||
private float low; | ||
private float close; | ||
private float open; | ||
|
||
public Long getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public void setTimestamp(Long timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public float getHigh() { | ||
return high; | ||
} | ||
|
||
public void setHigh(float high) { | ||
this.high = high; | ||
} | ||
|
||
public float getLow() { | ||
return low; | ||
} | ||
|
||
public void setLow(float low) { | ||
this.low = low; | ||
} | ||
|
||
public float getClose() { | ||
return close; | ||
} | ||
|
||
public void setClose(float close) { | ||
this.close = close; | ||
} | ||
|
||
public float getOpen() { | ||
return open; | ||
} | ||
|
||
public void setOpen(float open) { | ||
this.open = open; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
backend/src/main/java/com/bounswe2024group10/Tradeverse/dto/asset/GetAssetChartRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.bounswe2024group10.Tradeverse.dto.asset; | ||
|
||
public class GetAssetChartRequest { | ||
private String symbol; | ||
|
||
public String getSymbol() { | ||
return symbol; | ||
} | ||
|
||
public void setSymbol(String symbol) { | ||
this.symbol = symbol; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/com/bounswe2024group10/Tradeverse/dto/asset/GetAssetChartResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.bounswe2024group10.Tradeverse.dto.asset; | ||
|
||
import java.util.List; | ||
|
||
public class GetAssetChartResponse { | ||
private String symbol; | ||
private List<ChartData> chart; | ||
|
||
public String getSymbol() { | ||
return symbol; | ||
} | ||
|
||
public void setSymbol(String symbol) { | ||
this.symbol = symbol; | ||
} | ||
|
||
public List<ChartData> getChart() { | ||
return chart; | ||
} | ||
|
||
public void setChart(List<ChartData> chart) { | ||
this.chart = chart; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...end/src/main/java/com/bounswe2024group10/Tradeverse/dto/asset/GetAssetDetailsRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.bounswe2024group10.Tradeverse.dto.asset; | ||
|
||
public class GetAssetDetailsRequest { | ||
private String symbol; | ||
|
||
public String getSymbol() { | ||
return symbol; | ||
} | ||
|
||
public void setSymbol(String symbol) { | ||
this.symbol = symbol; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...nd/src/main/java/com/bounswe2024group10/Tradeverse/dto/asset/GetAssetDetailsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.bounswe2024group10.Tradeverse.dto.asset; | ||
|
||
public class GetAssetDetailsResponse { | ||
private String symbol; | ||
private String exchangeName; | ||
private float lastPrice; | ||
private float todayVolume; | ||
private String longName; | ||
|
||
public String getSymbol() { | ||
return symbol; | ||
} | ||
|
||
public void setSymbol(String symbol) { | ||
this.symbol = symbol; | ||
} | ||
|
||
public String getExchangeName() { | ||
return exchangeName; | ||
} | ||
|
||
public void setExchangeName(String exchangeName) { | ||
this.exchangeName = exchangeName; | ||
} | ||
|
||
public float getLastPrice() { | ||
return lastPrice; | ||
} | ||
|
||
public void setLastPrice(float lastPrice) { | ||
this.lastPrice = lastPrice; | ||
} | ||
|
||
public float getTodayVolume() { | ||
return todayVolume; | ||
} | ||
|
||
public void setTodayVolume(float todayVolume) { | ||
this.todayVolume = todayVolume; | ||
} | ||
|
||
public String getLongName() { | ||
return longName; | ||
} | ||
|
||
public void setLongName(String longName) { | ||
this.longName = longName; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
.../src/main/java/com/bounswe2024group10/Tradeverse/dto/asset/YahooFinanceChartResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.bounswe2024group10.Tradeverse.dto.asset; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class YahooFinanceChartResponse { | ||
private Chart chart; | ||
|
||
public static class Chart { | ||
private Result[] result; | ||
private String error; | ||
|
||
public Result[] getResult() { | ||
return result; | ||
} | ||
} | ||
|
||
public static class Result { | ||
private Meta meta; | ||
private long[] timestamp; | ||
private Indicators indicators; | ||
|
||
public Meta getMeta() { | ||
return meta; | ||
} | ||
|
||
public long[] getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public Indicators getIndicators() { | ||
return indicators; | ||
} | ||
} | ||
|
||
public static class Indicators { | ||
private Quote[] quote; | ||
|
||
public Quote[] getQuote() { | ||
return quote; | ||
} | ||
} | ||
|
||
public static class Quote { | ||
private Float[] high; | ||
private Float[] low; | ||
private Float[] close; | ||
private Float[] open; | ||
private Long[] volume; | ||
|
||
public Float[] getHigh() { return high; } | ||
public Float[] getLow() { return low; } | ||
public Float[] getClose() { return close; } | ||
public Float[] getOpen() { return open; } | ||
public Long[] getVolume() { return volume; } | ||
} | ||
|
||
public static class Meta { | ||
private String symbol; | ||
@JsonProperty("fullExchangeName") | ||
private String exchangeName; | ||
@JsonProperty("regularMarketPrice") | ||
private float lastPrice; | ||
@JsonProperty("regularMarketVolume") | ||
private float todayVolume; | ||
private String longName; | ||
|
||
// Getters | ||
public String getSymbol() { return symbol; } | ||
public String getExchangeName() { return exchangeName; } | ||
public float getLastPrice() { return lastPrice; } | ||
public float getTodayVolume() { return todayVolume; } | ||
public String getLongName() { return longName; } | ||
} | ||
|
||
public Chart getChart() { | ||
return chart; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
backend/src/main/java/com/bounswe2024group10/Tradeverse/service/AssetService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.bounswe2024group10.Tradeverse.service; | ||
|
||
import com.bounswe2024group10.Tradeverse.dto.asset.*; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.ResponseEntity; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
public class AssetService { | ||
public GetAssetDetailsResponse getAssetDetails(GetAssetDetailsRequest request) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
String url = "https://query1.finance.yahoo.com/v8/finance/chart/" + request.getSymbol(); | ||
ResponseEntity<YahooFinanceChartResponse> financialData = restTemplate.getForEntity(url, YahooFinanceChartResponse.class); | ||
YahooFinanceChartResponse yahooResponse = financialData.getBody(); | ||
if (yahooResponse == null || yahooResponse.getChart() == null | ||
|| yahooResponse.getChart().getResult() == null | ||
|| yahooResponse.getChart().getResult().length == 0 | ||
|| yahooResponse.getChart().getResult()[0].getMeta() == null) { | ||
throw new RuntimeException("Failed to get asset details"); | ||
} | ||
YahooFinanceChartResponse.Meta meta = yahooResponse.getChart().getResult()[0].getMeta(); | ||
String symbol = meta.getSymbol(); | ||
String exchangeName = meta.getExchangeName(); | ||
float lastPrice = meta.getLastPrice(); | ||
float todayVolume = meta.getTodayVolume(); | ||
String longName = meta.getLongName(); | ||
GetAssetDetailsResponse response = new GetAssetDetailsResponse(); | ||
response.setSymbol(symbol); | ||
response.setExchangeName(exchangeName); | ||
response.setLastPrice(lastPrice); | ||
response.setTodayVolume(todayVolume); | ||
response.setLongName(longName); | ||
return response; | ||
} | ||
|
||
public GetAssetChartResponse getAssetChart(GetAssetChartRequest request) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
String url = "https://query2.finance.yahoo.com/v8/finance/chart/" + request.getSymbol(); | ||
ResponseEntity<YahooFinanceChartResponse> financialData = restTemplate.getForEntity(url, YahooFinanceChartResponse.class); | ||
YahooFinanceChartResponse yahooResponse = financialData.getBody(); | ||
|
||
if (yahooResponse == null || yahooResponse.getChart() == null | ||
|| yahooResponse.getChart().getResult() == null | ||
|| yahooResponse.getChart().getResult().length == 0) { | ||
throw new RuntimeException("Failed to get asset chart data"); | ||
} | ||
|
||
YahooFinanceChartResponse.Result result = yahooResponse.getChart().getResult()[0]; | ||
List<ChartData> chartDataList = new ArrayList<>(); | ||
|
||
long[] timestamps = result.getTimestamp(); | ||
YahooFinanceChartResponse.Quote quote = result.getIndicators().getQuote()[0]; | ||
|
||
for (int i = 0; i < timestamps.length; i++) { | ||
if (quote.getClose()[i] == null) continue; | ||
ChartData data = new ChartData(); | ||
data.setTimestamp(timestamps[i]); | ||
data.setHigh(quote.getHigh()[i]); | ||
data.setLow(quote.getLow()[i]); | ||
data.setClose(quote.getClose()[i]); | ||
data.setOpen(quote.getOpen()[i]); | ||
chartDataList.add(data); | ||
} | ||
|
||
GetAssetChartResponse response = new GetAssetChartResponse(); | ||
response.setSymbol(result.getMeta().getSymbol()); | ||
response.setChart(chartDataList); | ||
return response; | ||
} | ||
} |