Lab10 convert currency real
Use the Real Currency Conversion Service¶
Now you will implement the CurrencyService
so it will call out to the remote API.
The Weather API Example
This is the code and JavaBean from the Weather example:
Learning from examples is great, but don't just blindly copy-n-paste...
// instantiate a RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// create a Template URL for the API
String weatherUrl = "https://basic-weather.herokuapp.com/api/weather/{zip}";
// put the variables in a Map
Map<String, String> uriVariables = new HashMap<>();
uriVariables.put("zip", "94404");
// call out to the remote API and get back a JavaBean
WeatherResponse response =
restTemplate.getForObject(weatherUrl,
WeatherResponse.class,
uriVariables);
And here's the JavaBean that's used above:
public class WeatherResponse {
private String location;
private String updated;
private Float temp;
private String condition;
// getters and setters would go here
}
A. JavaBean for Response¶
-
Create a JavaBean
ConvertedCurrency
that represents the returned JSON calledConvertedCurrency
. The properties for your JavaBean must match the JSON names, i.e., it will have 2 properties for the currency and the converted result.The JSON returned from this API looks like this:
{ "currency": "GBP", "converted": 71.00 }
B. Real Currency Conversion Service¶
You'll call out to the remote API like this:
http://jitterted-currency-conversion.herokuapp.com/convert?from=USD&to=GBP&amount=100
-
Create a new class,
HttpCurrencyConversionService
that implements theCurrencyConversionService
interface, but uses the remote API to do the conversion. -
In the
convertToBritishPound
method, create a URI Template string for the remote API:- The base part of the URI is
http://jitterted-currency-conversion.herokuapp.com/convert
- There are three query parameters:
from
- the source currency, e.g.,USD
to
- the converted currency, e.g.,GBP
amount
- the amount to convert, e.g., 10
Example
To convert 100 in USD to GBP, the URI would look like this:
http://jitterted-currency-conversion.herokuapp.com/convert?from=USD&to=GBP&amount=100
Think about what parts of the string need to become template variables. A URI Template Variable looks like
{amount}
. - The base part of the URI is
-
Instantiate a
RestTemplate
and use thegetForObject()
method, passing in these 3 parameters:- The URI template string from above
ConvertedCurrency.class
- The query parameters in the map
-
Calling
getForObject()
returns an instance ofConvertedCurrency
, so you will take theconverted
property (which is adouble
) and return it as anint
(you can just cast it or use theintValue()
method). -
Now try using
curl
or Postman to get an order and see that the GBP converted value is correct (it should be approximately 0.76 times the original amount).
Once you've completed the above steps, do not move on until you have checked in with the instructor.
Bonus #1¶
The server at http://jitterted-currency-conversion.herokuapp.com also supports converting to Bitcoin.
- The
to
currency query parameter for Bitcoin isBTC
(instead ofGBP
).
What would you need to do to have the price be converted to Bitcoin in addition to USD and GBP?
Think about what new method you want in your CurrencyConversionService
interface and then implement it in both the stub and the real service.
Bonus #2¶
Modify the web page view of an order to also convert the displayed price to Pounds.
For example, this would return the page, but with the price in Pounds:
http://localhost:8080/coffee-order/1?currency=gbp
You'll want to dig into the CoffeeOrderWebController
class to make this modification.
Once you've completed the above steps,
check in with the instructor to review your code.