Overview
The Currency Converter integrates with ExchangeRate-API to retrieve real-time currency conversion rates. The integration is implemented in theConsultaMoneda service class using Java’s modern HTTP client.
ConsultaMoneda Service Class
Class Definition
Package:lad.com.alura.conversormoneda.modelos
obtenerTasa() Method
The core method that performs currency conversion by calling the ExchangeRate-API.The source currency code in ISO 4217 format (e.g., “USD”, “EUR”, “ARS”)
The target currency code in ISO 4217 format (e.g., “ARS”, “BRL”, “COP”)
The amount to convert from the base currency
The converted amount in the target currency, or
0 if an error occursComplete Implementation
API Endpoint Structure
Endpoint Format
Your ExchangeRate-API key. The application uses a hardcoded key:
9ba310889b4d07769a662fc0Three-letter ISO 4217 currency code for the base currency
Three-letter ISO 4217 currency code for the target currency
The amount in the base currency to convert
Example API Calls
- USD to ARS
- BRL to USD
- COP to USD
HttpClient Usage
The application uses Java 11’s modernHttpClient API for HTTP communication.
HTTP Client Creation
HttpClient.newHttpClient() creates a new client with default configuration. For production applications, consider reusing a single client instance for better performance.Building HTTP Requests
Builder Pattern
The
HttpRequest.Builder uses the builder pattern for fluent, readable request configuration.Sending Requests
The HTTP request to send
Specifies how to handle the response body.
ofString() converts the response to a StringJSON Parsing with Gson
The application uses the Gson library to parse JSON responses from the API.API Response Format
Parsing Implementation
Key JSON Fields
result
Status of the API call:
"success" or "error"base_code
The base currency code requested
target_code
The target currency code requested
conversion_result
The final converted amount - this is what the application returns
Error Handling
The implementation includes comprehensive error handling for network and parsing issues.Try-Catch Block
Common Error Scenarios
Network Connection Failure
Network Connection Failure
Exception Type:
IOExceptionOccurs when:- No internet connection
- DNS resolution fails
- Firewall blocks the request
0 and prints error messageInvalid JSON Response
Invalid JSON Response
Exception Type:
JsonParseExceptionOccurs when:- API returns malformed JSON
- Response format changes
- Server error response
0 and prints error messageMissing JSON Field
Missing JSON Field
Exception Type:
NullPointerExceptionOccurs when:conversion_resultfield is missing- API returns error response
0 and prints error messageHTTP Request Failure
HTTP Request Failure
Exception Type:
InterruptedExceptionOccurs when:- Request thread is interrupted
- Request times out
0 and prints error messageDependencies
The API integration requires the following Java dependencies:The
java.net.http.HttpClient is part of the Java 11+ standard library and requires no external dependencies.API Rate Limits
ExchangeRate-API has the following rate limits:Free Plan
1,500 requests per month
Response Time
Typically under 100ms
Best Practices
Externalize API Keys
Externalize API Keys
The API key is currently hardcoded. For production:Or use a properties file:
Reuse HttpClient
Reuse HttpClient
Create a single
HttpClient instance and reuse it:Add Request Timeout
Add Request Timeout
Prevent hanging requests:
Validate Currency Codes
Validate Currency Codes
Validate currency codes before making API calls: