Async API
Classes for handling OpenWeatherMap API requests asnychronously. These classes are designed to be used with the corresponding OpenWeatherMap API endpoints.
Example
If you want to use the One Call API, you can create an instance of the OneCallAPI class.
- class openweatherwrap.asyncapi.AirPollutionAPI(api_key, location, language='en', units='standard')
Bases:
OpenWeatherMapAPIAsynchronous class for handling OpenWeatherMap Air Pollution API requests. This class is designed to be used with the Air Pollution API endpoint.
- async get_air_pollution()
Asynchronously fetches air pollution data from the Air Pollution API.
- Returns:
An instance of AirPollutionResponse containing the air pollution data.
- Return type:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
- async get_air_pollution_forecast()
Asynchronously fetches air pollution forecast data from the Air Pollution API.
- Returns:
An instance of AirPollutionResponse containing the air pollution forecast data.
- Return type:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
- async get_air_pollution_history(start, end)
Asynchronously fetches historical air pollution data from the Air Pollution API.
- Parameters:
- Returns:
An instance of AirPollutionResponse containing the historical air pollution data.
- Return type:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
- class openweatherwrap.asyncapi.CurrentWeatherAPI(api_key, location, language='en', units='standard', mode='json')
Bases:
OpenWeatherMapAPIAsynchronous class for handling OpenWeatherMap Current Weather API requests. This class is designed to be used with the Current Weather API endpoint.
- async get_weather()
Asynchronously fetches current weather data from the Current Weather API.
- Returns:
An instance of CurrentWeatherResponse containing the current weather data.
- Return type:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
- class openweatherwrap.asyncapi.FiveDayForecast(api_key, location, count=-1, language='en', units='standard', mode='json')
Bases:
OpenWeatherMapAPIAsynchronous class for handling OpenWeatherMap 5-Day Forecast API requests. This class is designed to be used with the 5-Day Forecast API endpoint.
- async get_forecast()
Asynchronously fetches the 5-day weather forecast data from the 5-Day Forecast API.
- Returns:
An instance of FiveDayForecastResponse containing the forecast data.
- Return type:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
- class openweatherwrap.asyncapi.GeocodingAPI(api_key)
Bases:
OpenWeatherMapAPIAsynchronous class for handling OpenWeatherMap Geocoding API requests. This class is designed to be used with the Geocoding API endpoint.
- async get_by_city(city, country, state_code=None, limit=1)
Fetches geocoding data for a city and country.
- Parameters:
- Returns:
An instance of GeocodingResponse containing the geocoding data.
- Return type:
- async get_by_coordinates(latitude, longitude, limit=1)
Fetches the geocoding data for a set of coordinates from the OpenWeatherMap API and returns the response.
- Parameters:
- Returns:
Object containing the geocoding data.
- Return type:
- Raises:
ValueError – If limit > 5 or limit < 1.
ValueError – If latitude is not between -90 and 90, and longitude is not between -180 and 180.
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
- async get_by_zip(zip_code, country)
Fetches geocoding data for a zip code and country.
- Parameters:
- Returns:
An instance of GeocodingResponse containing the geocoding data.
- Return type:
- class openweatherwrap.asyncapi.OneCallAPI(api_key, location, language='en', units='standard')
Bases:
OpenWeatherMapAPIAsynchronous class for handling OpenWeatherMap One Call API requests. This class is designed to be used with the One Call API endpoint.
- async get_aggregation(date)
Asynchronously fetches the weather data for a specific date from the one call API and returns the response as a OneCallResponse object`.
- Parameters:
date (str) – Date in the ISO 8601 format ‘YYYY-MM-DD’ for which to fetch the weather data.
- Returns:
Object containing the weather data for the specified date.
- Return type:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
- async get_overview()
Asynchronously fetches a brief, human-readable overview of the weather data for the specified location.
- Returns:
A brief overview of the weather data.
- Return type:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
- async get_timed_weather(timestamp)
Asynchronously fetches weather data for a specific timestamp from the One Call API.
- Parameters:
timestamp (int) – The Unix timestamp for which to fetch the weather data.
- Returns:
An instance of OneCallResponse containing the weather data for the specified timestamp.
- Return type:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
ValueError – If the timestamp is before January 1, 1979.
ValueError – If the timestamp is not a valid UNIX timestamp.
- async get_weather(exclude=[])
Asynchronously fetches weather data from the One Call API.
- Parameters:
exclude (list[Literal['current', 'minutely', 'hourly', 'daily', 'alerts']], optional) – List of data types to exclude from the response. Defaults to an empty list, which means no data types are excluded.
- Returns:
An instance of OneCallResponse containing the weather data.
- Return type:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
- class openweatherwrap.asyncapi.WeatherMapsAPI(api_key)
Bases:
OpenWeatherMapAPIAsynchronous class for handling OpenWeatherMap Weather Map API requests. This class is designed to be used with the Weather Map API endpoint.
- async download_weathermap(layer, x, y, zoom, filename)
Downloads the weather map image for a specific layer and coordinates to a file.
The pixel coordinate can be calculated using the following formula:
\[pixel\_coordinate = world\_coordinate * 2^{zoom}\]You can read more here.
- Parameters:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
ValueError – If the layer is not one of the supported layers.
ValueError – if zoom is not between 0 and 9 (inclusive).
ValueError – if the x and y coordinates are not valid.
OSError – If there is an error writing to the file.
- Return type:
- async get_weathermap(layer, x, y, zoom)
Fetches the weather map image for a specific layer and coordinates.
The pixel coordinate can be calculated using the following formula:
\[pixel = world\_coordinate * 2^{zoom}\]You can read more here.
- Parameters:
- Returns:
The weather map image data.
- Return type:
- Raises:
SubscriptionLevelError – If the API key does not have access to the requested data.
InvalidAPIKeyError – If the API key is invalid.
NotFoundError – If the location is not found.
TooManyRequestsError – If the API rate limit is exceeded.
OpenWeatherMapException – For internal server errors (500, 502, 503, 504).
ValueError – If the layer is not one of the supported layers.
ValueError – if zoom is not between 0 and 9 (inclusive).
ValueError – if the x and y coordinates are not valid.