Package com.github.trelawnm.weathersdk
Class WeatherSDK
java.lang.Object
com.github.trelawnm.weathersdk.WeatherSDK
Weather SDK - lightweight Java library for OpenWeatherMap API.
Provides a simple interface to retrieve weather data in JSON format.
Operation Modes:
- ON_DEMAND - Lazy polling: data is fetched on request with TTL-based caching
- POLLING - True polling: background agent periodically updates all cached cities
Usage Example (ON_DEMAND mode):
WeatherSDK sdk = new WeatherSDK.Builder("your-api-key")
.endpoint("https://api.openweathermap.org/data/2.5/")
.mode(WeatherSDKMode.ON_DEMAND)
.build();
String weatherJson = sdk.getWeather("London");
System.out.println(weatherJson);
// Don't forget to cleanup when done
sdk.shutdown();
Usage Example (POLLING mode):
// SDK automatically starts background polling agent
WeatherSDK sdk = new WeatherSDK.Builder("your-api-key")
.mode(WeatherSDKMode.POLLING)
.pollingInterval(Duration.ofMinutes(10))
.maxSize(15)
.build();
// Immediate response from cache (zero latency)
String weatherJson = sdk.getWeather("London");
System.out.println(weatherJson);
// Don't forget to cleanup when done
sdk.shutdown();
Resource Management:
For POLLING mode, always call shutdown() to stop background threads
and release resources when the SDK is no longer needed.
- Version:
- 1.0
- Author:
- trelawnm
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for configuring WeatherSDK instances with fluent interface. -
Method Summary
Modifier and TypeMethodDescriptionvoidClears all cached weather data.Returns the endpoint URL for OpenWeatherMap API requestsgetKey()Returns the API key used for authentication with OpenWeatherMap servicegetMode()Returns the operation mode of the SDK (ON_DEMAND or POLLING)Returns the polling interval used in POLLING modegetWeather(String cityName) Gets current weather data for specified city in JSON format.getWeatherObj(String cityName) Gets current weather data for specified cityvoidshutdown()Gracefully shuts down background polling agent (if enabled) and releases resources.
-
Method Details
-
getKey
Returns the API key used for authentication with OpenWeatherMap service- Returns:
- the API key as String
-
getEndpoint
Returns the endpoint URL for OpenWeatherMap API requests- Returns:
- the endpoint URL as String
-
getMode
Returns the operation mode of the SDK (ON_DEMAND or POLLING)- Returns:
- the current operation mode
-
getPollingInterval
Returns the polling interval used in POLLING mode- Returns:
- the polling interval as Duration
-
getWeatherObj
Gets current weather data for specified city- Parameters:
cityName- the name of the city- Returns:
- weather data in JSON format
- Throws:
IllegalArgumentException- if request fails or city not found
-
getWeather
Gets current weather data for specified city in JSON format. Implements different caching strategies based on operation mode: - ON_DEMAND: Lazy polling with TTL-based cache validation - POLLING: Immediate response from pre-refreshed cache- Parameters:
cityName- the name of the city to query- Returns:
- weather data as JSON string
- Throws:
RuntimeException- if city not found or API request fails
-
shutdown
public void shutdown()Gracefully shuts down background polling agent (if enabled) and releases resources. Should be called when SDK is no longer needed, especially in POLLING mode. No-op if scheduler is not initialized (ON_DEMAND mode). -
clearCache
public void clearCache()Clears all cached weather data. Useful for freeing memory or resetting SDK state.
-