Skip to content

Commit

Permalink
Added changes to Currency Conversion
Browse files Browse the repository at this point in the history
Added changes for API call using Feign Client
  • Loading branch information
vijay-2510 committed Apr 19, 2023
1 parent 08e1d4c commit 133eaa6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions currency-conversion-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class CurrencyConversionServiceApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.vijay.microservices.currencyconversionservice.controller;

import com.vijay.microservices.currencyconversionservice.bean.CurrencyConversion;
import com.vijay.microservices.currencyconversionservice.proxy.CurrencyExchangeProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -13,15 +15,27 @@
@RestController
public class CurrencyConversionController {

@Autowired
private CurrencyExchangeProxy currencyExchangeProxy;

@GetMapping("currency-conversion/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversion calculateCurrencyConversion(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity){

HashMap<String,String> uriVariables = new HashMap<>();
uriVariables.put("from",from);
uriVariables.put("to",to);
ResponseEntity<CurrencyConversion> responseEntity = new RestTemplate().getForEntity("http://localhost:8000/currency-exchange/from/{from}/to/{to}", CurrencyConversion.class, uriVariables);
CurrencyConversion currenncyConversion = responseEntity.getBody();
return new CurrencyConversion(currenncyConversion.getId(),from,to, currenncyConversion.getConversionMultiple(),quantity
,quantity.multiply(currenncyConversion.getConversionMultiple()),currenncyConversion.getEnvironment());
CurrencyConversion currencyConversion = responseEntity.getBody();
return new CurrencyConversion(currencyConversion.getId(),from,to, currencyConversion.getConversionMultiple(),quantity
,quantity.multiply(currencyConversion.getConversionMultiple()),currencyConversion.getEnvironment());
}

@GetMapping("currency-conversion-feign/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversion calculateCurrencyConversionFeign(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity){

CurrencyConversion currencyConversion = currencyExchangeProxy.retrieveExchangeValue(from, to);

return new CurrencyConversion(currencyConversion.getId(),from,to, currencyConversion.getConversionMultiple(),quantity
,quantity.multiply(currencyConversion.getConversionMultiple()),currencyConversion.getEnvironment());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.vijay.microservices.currencyconversionservice.proxy;

import com.vijay.microservices.currencyconversionservice.bean.CurrencyConversion;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;

@FeignClient(name="currency-exchange", url="localhost:8000")
public interface CurrencyExchangeProxy {

@GetMapping("currency-exchange/from/{from}/to/{to}")
public CurrencyConversion retrieveExchangeValue(@PathVariable String from, @PathVariable String to);
}

0 comments on commit 133eaa6

Please sign in to comment.