Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
-Se implementaron clases para consumir una nueva api
  • Loading branch information
Marc0Franc0 committed Dec 4, 2023
1 parent 4abcab1 commit 4cbd6cd
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +17,8 @@
public class OtherCurrenciesController {
@Autowired
private OtherCurrenciesService otherCurrenciesService;
@Autowired
private QuoteService quoteService;
@GetMapping("/euro")
ResponseEntity<Money> getEuro(){
return ResponseEntity.status(HttpStatus.OK).body(otherCurrenciesService.getEuro());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Quote> getQuote(@PathVariable String symbol){
return ResponseEntity.status(HttpStatus.OK).body(quoteService.getQuote(symbol));
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/app/ExchangeRates/model/FinnHub/Quote.java
Original file line number Diff line number Diff line change
@@ -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
}*/
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
24 changes: 19 additions & 5 deletions src/main/java/com/app/ExchangeRates/service/util/ApiUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
apidolar.base-url= https://dolarapi.com/v1
finnhub.base-url=https://finnhub.io/api/v1
finnhub.token=cljvmhhr01ql1cbg8bf0cljvmhhr01ql1cbg8bfg

0 comments on commit 4cbd6cd

Please sign in to comment.