Created
December 4, 2015 06:58
-
-
Save javaeeeee/0ddae73b77d71c547985 to your computer and use it in GitHub Desktop.
A resource class to perform currency conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Path("/converter") | |
| public class ConverterResource { | |
| /** | |
| * The name of the path parameter for the API key. | |
| */ | |
| public static final String APP_ID = "app_id"; | |
| /** | |
| * A part of an error message. | |
| */ | |
| public static final String UNKNOWN_CURRENCY = "Unknown currency "; | |
| /** | |
| * Error message when no amount to convert was presented. | |
| */ | |
| public static final String AMOUNT_IS_NECESSARY = "Amount is necessary."; | |
| /** | |
| * A Jersey client to connect to external API to get exchange rate. | |
| */ | |
| private final Client client; | |
| /** | |
| * The URL to access exchange rate API. | |
| */ | |
| private final String apiURL; | |
| /** | |
| * The key to access exchange rate API. | |
| */ | |
| private final String apiKey; | |
| /** | |
| * A constructor. | |
| * | |
| * @param client Jersey client. | |
| * @param apiURL the URL to access exchange rate API. | |
| * @param apiKey he key to access exchange rate API. | |
| */ | |
| public ConverterResource(Client client, String apiURL, String apiKey) { | |
| this.client = client; | |
| this.apiURL = apiURL; | |
| this.apiKey = apiKey; | |
| } | |
| /** | |
| * A subresource method used to convert currencies. | |
| * | |
| * The URI to access it looks like | |
| * <em>localhost:8080/converter/1.5?from=EUR&to=CAD</em> where a double | |
| * number is provided as a path parameter and initial and final currencies | |
| * are provided as query parameters. | |
| * | |
| * @param amount a whole number, an amount to convert. | |
| * @param from the currency to be converted. | |
| * @param to the currency to which to convert | |
| * @return the result of conversion rounded to Cents. | |
| */ | |
| @GET | |
| @Path("/{amount}") | |
| @Produces(MediaType.TEXT_PLAIN) | |
| @Consumes(MediaType.TEXT_PLAIN) | |
| //localhost:8080/converter/1.5?from=EUR&to=CAD | |
| public double convert( | |
| @PathParam("amount") Optional<Double> amount, | |
| @DefaultValue("USD") @QueryParam("from") String from, | |
| @DefaultValue("EUR") @QueryParam("to") String to | |
| ) { | |
| //A variable to store convertion rate of initial currency to US Dollar. | |
| Double fromRate; | |
| //A variable to store convertion rate of the desired currency | |
| //to US Dollar. | |
| Double toRate; | |
| //A string to store an error message if an incorrect currency | |
| //was provided. | |
| String errorMessage; | |
| //Check that amount to convert is present. | |
| if (!amount.isPresent()) { | |
| throw new BadRequestException(AMOUNT_IS_NECESSARY); | |
| } | |
| //Obtain data from external API. | |
| CurrencyData currencyData = client | |
| // returns WebTarget | |
| .target(apiURL) | |
| // returns WebTarget | |
| .queryParam(APP_ID, apiKey) | |
| //Can be done only in Enterprise and unlimited versions. | |
| //.queryParam("base", to) | |
| //returns Invocation.Builder | |
| .request(MediaType.APPLICATION_JSON) | |
| //returns CurrencyData | |
| .get(CurrencyData.class); | |
| //Obtain rates. | |
| fromRate = currencyData.getRates().get(from); | |
| toRate = currencyData.getRates().get(to); | |
| //Check that valid currency symbols were provided. | |
| if (fromRate == null || toRate == null) { | |
| errorMessage = UNKNOWN_CURRENCY | |
| + (fromRate == null ? from : to); | |
| throw new BadRequestException(errorMessage); | |
| } | |
| //Convert and return the result. | |
| return convert(fromRate, toRate, amount.get()); | |
| } | |
| ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment