Skip to content

Lab 10 - 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

  1. Create a JavaBean ConvertedCurrency that represents the returned JSON called ConvertedCurrency. 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
  1. Create a new class, HttpCurrencyConversionService that implements the CurrencyConversionService interface, but uses the remote API to do the conversion.

  2. 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:
      1. from - the source currency, e.g., USD
      2. to - the converted currency, e.g., GBP
      3. 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}.

  3. Instantiate a RestTemplate and use the getForObject() method, passing in these 3 parameters:

    1. The URI template string from above
    2. ConvertedCurrency.class
    3. The query parameters in the map
  4. Calling getForObject() returns an instance of ConvertedCurrency, so you will take the converted property (which is a double) and return it as an int (you can just cast it or use the intValue() method).

  5. 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 is BTC (instead of GBP).

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.