Package net.thauvin.erik.frankfurter
Class Frankfurter
java.lang.Object
net.thauvin.erik.frankfurter.Frankfurter
Client for the Frankfurter.dev exchange rates API.
Provides methods to fetch currency metadata, providers, and exchange rates. All calls are blocking. Instances are thread-safe and reusable.
Timeouts: 5s connect timeout, 10s request timeout by default. Both must be positive.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new client using the default API endpoint, defaultHttpClientwith 5s connect timeout, and 10s request timeout.Frankfurter(HttpClient client, URI baseUri) Creates a new client using the given HTTP client and base URI with default 10s request timeout.Frankfurter(HttpClient client, URI baseUri, Duration requestTimeout) Creates a new client using the given HTTP client, base URI, and request timeout.Frankfurter(URI baseUri) Creates a new client using the given base URI, defaultHttpClientwith 5s connect timeout, and 10s request timeout.Frankfurter(Duration requestTimeout) Creates a new client using the default API endpoint and connect timeout, with a custom request timeout.Frankfurter(Duration connectTimeout, Duration requestTimeout) Creates a new client using the default API endpoint with custom connect and request timeouts. -
Method Summary
Modifier and TypeMethodDescriptionReturns the base API URI used by this client.Returns the underlyingHttpClient.Returns the connect timeout configured on the underlyingHttpClient.Retrieves the list of supported currencies.getCurrency(String code) Retrieves metadata for a single currency.getCurrency(CurrencyCode code) Retrieves metadata for a single currency.Retrieves the list of available rate providers.Retrieves a single exchange rate for the given currency pair.getRate(RateConfig config) Retrieves a single exchange rate using the/rate/{base}/{quote}endpoint.getRate(CurrencyCode base, CurrencyCode quote) Retrieves a single exchange rate for the given currency pair.getRates()Retrieves exchange rates using default configuration.getRates(RatesConfig config) Retrieves exchange rates using the/ratesendpoint.Returns the request timeout configured for this client.
-
Constructor Details
-
Frankfurter
public Frankfurter()Creates a new client using the default API endpoint, defaultHttpClientwith 5s connect timeout, and 10s request timeout. -
Frankfurter
Creates a new client using the default API endpoint and connect timeout, with a custom request timeout.- Parameters:
requestTimeout- the request timeout (must be positive, notnull)- Throws:
IllegalArgumentException- ifrequestTimeoutis zero or negative
-
Frankfurter
Creates a new client using the default API endpoint with custom connect and request timeouts.- Parameters:
connectTimeout- the connect timeout (must be positive, notnull)requestTimeout- the request timeout (must be positive, notnull)- Throws:
IllegalArgumentException- if either timeout is zero or negative
-
Frankfurter
Creates a new client using the given base URI, defaultHttpClientwith 5s connect timeout, and 10s request timeout.- Parameters:
baseUri- the base API URI (must not benull)
-
Frankfurter
Creates a new client using the given HTTP client and base URI with default 10s request timeout.Note: The connect timeout is determined by the provided
HttpClient. If the client was built without an explicit connect timeout,getConnectTimeout()will return an emptyOptional.- Parameters:
client- the HTTP client to use (must not benull)baseUri- the base API URI (must not benull)
-
Frankfurter
public Frankfurter(@NonNull HttpClient client, @NonNull URI baseUri, @NonNull Duration requestTimeout) Creates a new client using the given HTTP client, base URI, and request timeout.Note: The connect timeout is determined by the provided
HttpClient. If the client was built without an explicit connect timeout,getConnectTimeout()will return an emptyOptional.- Parameters:
client- the HTTP client to use (must not benull)baseUri- the base API URI (must not benull)requestTimeout- the request timeout (must be positive, notnull)- Throws:
IllegalArgumentException- ifrequestTimeoutis zero or negative
-
-
Method Details
-
getBaseUri
Returns the base API URI used by this client.- Returns:
- the base URI (never null)
-
getClient
Returns the underlyingHttpClient.- Returns:
- the HTTP client (never null)
-
getConnectTimeout
Returns the connect timeout configured on the underlyingHttpClient.Returns an empty
Optionalif theHttpClientwas built without an explicit connect timeout, which means connections may block indefinitely.- Returns:
- the connect timeout, or empty if not configured
-
getCurrencies
Retrieves the list of supported currencies.- Returns:
- the currencies result
- Throws:
IOException- if a network error occurs or the request times outInterruptedException- if the request is interrupted
-
getCurrency
@NonNull public CurrencyResult getCurrency(@NonNull String code) throws IOException, InterruptedException Retrieves metadata for a single currency.- Parameters:
code- the ISO currency code (must not benullor blank)- Returns:
- the currency result
- Throws:
IOException- if a network error occurs or the request times outInterruptedException- if the request is interrupted
-
getCurrency
@NonNull public CurrencyResult getCurrency(@NonNull CurrencyCode code) throws IOException, InterruptedException Retrieves metadata for a single currency.- Parameters:
code- the currency code (must not benull)- Returns:
- the currency result
- Throws:
IOException- if a network error occurs or the request times outInterruptedException- if the request is interrupted
-
getProviders
Retrieves the list of available rate providers.- Returns:
- the providers result
- Throws:
IOException- if a network error occurs or the request times outInterruptedException- if the request is interrupted
-
getRate
@NonNull public RateResult getRate(@NonNull String base, @NonNull String quote) throws IOException, InterruptedException Retrieves a single exchange rate for the given currency pair.- Parameters:
base- the base ISO currency code (must not benullor blank)quote- the quote ISO currency code (must not benullor blank)- Returns:
- the rate result
- Throws:
IOException- if a network error occurs or the request times outInterruptedException- if the request is interrupted
-
getRate
@NonNull public RateResult getRate(@NonNull CurrencyCode base, @NonNull CurrencyCode quote) throws IOException, InterruptedException Retrieves a single exchange rate for the given currency pair.- Parameters:
base- the base currency (must not benull)quote- the quote currency (must not benull)- Returns:
- the rate result
- Throws:
IOException- if a network error occurs or the request times outInterruptedException- if the request is interrupted
-
getRate
@NonNull public RateResult getRate(@NonNull RateConfig config) throws IOException, InterruptedException Retrieves a single exchange rate using the/rate/{base}/{quote}endpoint.- Parameters:
config- the rate configuration (must not benull)- Returns:
- the rate result
- Throws:
IOException- if a network error occurs or the request times outInterruptedException- if the request is interrupted
-
getRates
Retrieves exchange rates using default configuration.- Returns:
- the rates result
- Throws:
IOException- if a network error occurs or the request times outInterruptedException- if the request is interrupted
-
getRates
@NonNull public RatesResult getRates(@NonNull RatesConfig config) throws IOException, InterruptedException Retrieves exchange rates using the/ratesendpoint.- Parameters:
config- the rates configuration (must not benull)- Returns:
- the rates result
- Throws:
IOException- if a network error occurs or the request times outInterruptedException- if the request is interrupted
-
getRequestTimeout
Returns the request timeout configured for this client.- Returns:
- the request timeout (never null)
-