From 4cbd6cd196d01347fe5e92a8ee58138f04e67877 Mon Sep 17 00:00:00 2001 From: Marc0Franc0 Date: Mon, 4 Dec 2023 14:26:48 -0300 Subject: [PATCH] Update -Se implementaron clases para consumir una nueva api --- .../ApiDolar/OtherCurrenciesController.java | 4 +++ .../FinnHubApi/QuoteController.java | 22 +++++++++++++++ .../mapper/FinnHubApi/QuoteMapper.java | 15 ++++++++++ .../ExchangeRates/model/FinnHub/Quote.java | 28 +++++++++++++++++++ .../service/DolarApi/DolarServiceImpl.java | 17 +++++------ .../DolarApi/OtherCurrenciesServiceImpl.java | 9 +++--- .../service/FinnHubApi/QuoteService.java | 9 ++++++ .../service/FinnHubApi/QuoteServiceImpl.java | 21 ++++++++++++++ .../ExchangeRates/service/util/ApiUtil.java | 24 ++++++++++++---- src/main/resources/application.properties | 2 ++ 10 files changed, 131 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/app/ExchangeRates/controller/FinnHubApi/QuoteController.java create mode 100644 src/main/java/com/app/ExchangeRates/mapper/FinnHubApi/QuoteMapper.java create mode 100644 src/main/java/com/app/ExchangeRates/model/FinnHub/Quote.java create mode 100644 src/main/java/com/app/ExchangeRates/service/FinnHubApi/QuoteService.java create mode 100644 src/main/java/com/app/ExchangeRates/service/FinnHubApi/QuoteServiceImpl.java diff --git a/src/main/java/com/app/ExchangeRates/controller/ApiDolar/OtherCurrenciesController.java b/src/main/java/com/app/ExchangeRates/controller/ApiDolar/OtherCurrenciesController.java index 5ab0d79..3bfc33c 100644 --- a/src/main/java/com/app/ExchangeRates/controller/ApiDolar/OtherCurrenciesController.java +++ b/src/main/java/com/app/ExchangeRates/controller/ApiDolar/OtherCurrenciesController.java @@ -1,8 +1,10 @@ package com.app.ExchangeRates.controller.ApiDolar; import com.app.ExchangeRates.model.DolarApi.Money; +import com.app.ExchangeRates.model.FinnHub.Quote; import com.app.ExchangeRates.service.DolarApi.DolarService; import com.app.ExchangeRates.service.DolarApi.OtherCurrenciesService; +import com.app.ExchangeRates.service.FinnHubApi.QuoteService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -15,6 +17,8 @@ public class OtherCurrenciesController { @Autowired private OtherCurrenciesService otherCurrenciesService; + @Autowired + private QuoteService quoteService; @GetMapping("/euro") ResponseEntity getEuro(){ return ResponseEntity.status(HttpStatus.OK).body(otherCurrenciesService.getEuro()); diff --git a/src/main/java/com/app/ExchangeRates/controller/FinnHubApi/QuoteController.java b/src/main/java/com/app/ExchangeRates/controller/FinnHubApi/QuoteController.java new file mode 100644 index 0000000..84b2602 --- /dev/null +++ b/src/main/java/com/app/ExchangeRates/controller/FinnHubApi/QuoteController.java @@ -0,0 +1,22 @@ +package com.app.ExchangeRates.controller.FinnHubApi; + +import com.app.ExchangeRates.model.FinnHub.Quote; +import com.app.ExchangeRates.service.FinnHubApi.QuoteService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/exchanges/stock-price") +public class QuoteController { + @Autowired + private QuoteService quoteService; + @GetMapping("/{symbol}") + ResponseEntity getQuote(@PathVariable String symbol){ + return ResponseEntity.status(HttpStatus.OK).body(quoteService.getQuote(symbol)); + } +} diff --git a/src/main/java/com/app/ExchangeRates/mapper/FinnHubApi/QuoteMapper.java b/src/main/java/com/app/ExchangeRates/mapper/FinnHubApi/QuoteMapper.java new file mode 100644 index 0000000..090f2c0 --- /dev/null +++ b/src/main/java/com/app/ExchangeRates/mapper/FinnHubApi/QuoteMapper.java @@ -0,0 +1,15 @@ +package com.app.ExchangeRates.mapper.FinnHubApi; + +import com.app.ExchangeRates.model.FinnHub.Quote; +import lombok.experimental.UtilityClass; + +@UtilityClass +public class QuoteMapper { + public Quote buildQuoteDto(Quote quote){ + return Quote.builder() + .openPriceOfTheDay(quote.getOpenPriceOfTheDay()) + .percentChange(quote.getPercentChange()) + .currentPrice(quote.getCurrentPrice()) + .build(); + } +} diff --git a/src/main/java/com/app/ExchangeRates/model/FinnHub/Quote.java b/src/main/java/com/app/ExchangeRates/model/FinnHub/Quote.java new file mode 100644 index 0000000..df246bf --- /dev/null +++ b/src/main/java/com/app/ExchangeRates/model/FinnHub/Quote.java @@ -0,0 +1,28 @@ +package com.app.ExchangeRates.model.FinnHub; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.*; + +@Builder +@AllArgsConstructor +@NoArgsConstructor +@Data +public class Quote { + @JsonProperty("c") + private Double currentPrice; + @JsonProperty("dp") + private Double percentChange; + @JsonProperty("o") + private Double openPriceOfTheDay; + +/* { + "c": 191.24, + "d": 1.29, + "dp": 0.6791, + "h": 191.555, + "l": 189.23, + "o": 190.33, + "pc": 189.95, + "t": 1701464401 + }*/ +} diff --git a/src/main/java/com/app/ExchangeRates/service/DolarApi/DolarServiceImpl.java b/src/main/java/com/app/ExchangeRates/service/DolarApi/DolarServiceImpl.java index ee452ae..6bc460f 100644 --- a/src/main/java/com/app/ExchangeRates/service/DolarApi/DolarServiceImpl.java +++ b/src/main/java/com/app/ExchangeRates/service/DolarApi/DolarServiceImpl.java @@ -1,15 +1,12 @@ package com.app.ExchangeRates.service.DolarApi; -import com.app.ExchangeRates.mapper.DolarApi.MoneyMapper; import com.app.ExchangeRates.model.DolarApi.Money; -import com.app.ExchangeRates.service.DolarApi.DolarService; import com.app.ExchangeRates.service.util.ApiUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; @Service @Slf4j @@ -25,7 +22,7 @@ public Money getOfficialDollar() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } @Override @@ -34,7 +31,7 @@ public Money getBlueDollar() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } @Override @@ -43,7 +40,7 @@ public Money getCCLDollar() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } @Override @@ -52,7 +49,7 @@ public Money getDollarCard() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } @Override @@ -61,7 +58,7 @@ public Money getStockMarketDollar() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } @Override @@ -70,7 +67,7 @@ public Money getSolidarityDollar() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } @Override @@ -79,6 +76,6 @@ public Money getWholesaleDollar() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } } diff --git a/src/main/java/com/app/ExchangeRates/service/DolarApi/OtherCurrenciesServiceImpl.java b/src/main/java/com/app/ExchangeRates/service/DolarApi/OtherCurrenciesServiceImpl.java index fb7baff..aeec692 100644 --- a/src/main/java/com/app/ExchangeRates/service/DolarApi/OtherCurrenciesServiceImpl.java +++ b/src/main/java/com/app/ExchangeRates/service/DolarApi/OtherCurrenciesServiceImpl.java @@ -7,7 +7,6 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; @Service @@ -23,7 +22,7 @@ public Money getEuro() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } @Override @@ -32,7 +31,7 @@ public Money getBrazilianReal() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } @Override @@ -41,7 +40,7 @@ public Money getChileanPeso() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } @Override @@ -50,7 +49,7 @@ public Money getUruguayanPeso() { HttpHeaders headers = new HttpHeaders(); return apiUtil .buildApiDolarDTO( - apiUtil.consultExternalApi(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); + apiUtil.buildExchange(uri,HttpMethod.GET,new HttpEntity<>(headers),Money.class)); } diff --git a/src/main/java/com/app/ExchangeRates/service/FinnHubApi/QuoteService.java b/src/main/java/com/app/ExchangeRates/service/FinnHubApi/QuoteService.java new file mode 100644 index 0000000..b44c23a --- /dev/null +++ b/src/main/java/com/app/ExchangeRates/service/FinnHubApi/QuoteService.java @@ -0,0 +1,9 @@ +package com.app.ExchangeRates.service.FinnHubApi; + +import com.app.ExchangeRates.model.FinnHub.Quote; +import org.springframework.stereotype.Service; + +@Service +public interface QuoteService { + Quote getQuote(String symbol); +} diff --git a/src/main/java/com/app/ExchangeRates/service/FinnHubApi/QuoteServiceImpl.java b/src/main/java/com/app/ExchangeRates/service/FinnHubApi/QuoteServiceImpl.java new file mode 100644 index 0000000..759ec73 --- /dev/null +++ b/src/main/java/com/app/ExchangeRates/service/FinnHubApi/QuoteServiceImpl.java @@ -0,0 +1,21 @@ +package com.app.ExchangeRates.service.FinnHubApi; + +import com.app.ExchangeRates.model.FinnHub.Quote; +import com.app.ExchangeRates.service.util.ApiUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +@Service +public class QuoteServiceImpl implements QuoteService { + @Autowired + private ApiUtil apiUtil; + @Value("${finnhub.base-url}") + String baseUrl; + @Value("${finnhub.token}") + String token; + @Override + public Quote getQuote(String symbol) { + String uri = baseUrl+"/quote?symbol="+symbol+"&token="+token; + return apiUtil.buildQuoteDTO(apiUtil.getForEntity(uri,Quote.class)); + } +} diff --git a/src/main/java/com/app/ExchangeRates/service/util/ApiUtil.java b/src/main/java/com/app/ExchangeRates/service/util/ApiUtil.java index af15e24..6c3bb82 100644 --- a/src/main/java/com/app/ExchangeRates/service/util/ApiUtil.java +++ b/src/main/java/com/app/ExchangeRates/service/util/ApiUtil.java @@ -1,7 +1,9 @@ package com.app.ExchangeRates.service.util; import com.app.ExchangeRates.mapper.DolarApi.MoneyMapper; +import com.app.ExchangeRates.mapper.FinnHubApi.QuoteMapper; import com.app.ExchangeRates.model.DolarApi.Money; +import com.app.ExchangeRates.model.FinnHub.Quote; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; @@ -13,17 +15,29 @@ public class ApiUtil { @Autowired private RestTemplate restTemplate; - + private RuntimeException throwError(ResponseEntity response){ + log.error("Error when making request to external api - httpStatus was: {}", response.getStatusCode()); + throw new RuntimeException("Error"); + } public Money buildApiDolarDTO(ResponseEntity response){ if(response.getStatusCode().value()==200){ log.info("Request to external api correct: {}", response.getStatusCode()); return MoneyMapper.buildMoneyDto((Money) response.getBody()); } - log.error("Error when making request to external api - httpStatus was: {}", response.getStatusCode()); - throw new RuntimeException("Error"); + throw throwError(response); } - public ResponseEntity consultExternalApi(String uri, HttpMethod httpMethod, - HttpEntity httpEntity, Class returnType){ + public ResponseEntity buildExchange(String uri, HttpMethod httpMethod, + HttpEntity httpEntity, Class returnType){ return restTemplate.exchange(uri,httpMethod,httpEntity,returnType); } + public ResponseEntity getForEntity(String uri, Class returnType){ + return restTemplate.getForEntity(uri,returnType); + } + public Quote buildQuoteDTO(ResponseEntity response){ + if(response.getStatusCode().value()==200){ + log.info("Request to external api correct: {}", response.getStatusCode()); + return QuoteMapper.buildQuoteDto((Quote) response.getBody()); + } + throw throwError(response); + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f283a57..9da40f7 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,4 @@ apidolar.base-url= https://dolarapi.com/v1 +finnhub.base-url=https://finnhub.io/api/v1 +finnhub.token=cljvmhhr01ql1cbg8bf0cljvmhhr01ql1cbg8bfg