Skip to content

Commit

Permalink
Merge pull request #386 from bounswe/financial_data_endpoints
Browse files Browse the repository at this point in the history
Implement YahooFinance endpoints.
  • Loading branch information
TheRealLowyer authored Nov 23, 2024
2 parents 93abd1b + d8808d7 commit dc585f8
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll()
.requestMatchers("/api/auth/validate-token").permitAll()
.requestMatchers("/api/user/**").permitAll()
.requestMatchers("/api/asset/**").permitAll()
.anyRequest().authenticated()
);
return http.build();
Expand Down
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);
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}

0 comments on commit dc585f8

Please sign in to comment.