SDK for accessing to OpenWeatherAPI
Project description
OpenWeatherSDK
Introduction
SDK for accessing to OpenWeatherAPI and retrieving information about current weather conditions in a specified city. The SDK can operate in two modes: on-demand and polling. In on-demand mode (the default mode), API requests are made on user demand, while in the polling mode, there is regular polling of the API for weather updates in the saved (previously requested) cities.
Contents
Description of response formats
Installation
pip install msklv-openweather-sdk
Prerequisites
To work with the SDK, you need to obtain an access token for OpenWeatherAPI. More information can be found on FAQ page ("Get started" section -> "How to get an API key").
Client initialization
The only required argument is the access token.
By default, the SDK operates in on-demand mode, returns information in English, uses the metric system of measurements, has a cache size of 10 locations, and the information remains valid for 10 minutes. You can modify this mode by passing additional arguments during client initialization.
Additionally, if you need to modify the behavior of the SDK, you can pass additional arguments.
Additional arguments
mode
- determines the operating mode of the SDK. In on-demand mode, the SDK
makes requests to the API only upon client requests. In polling mode, the SDK
regularly polls the API. Defaults: on-demand. Available options: on-demand, polling.
language
- determines the language for the output. Defaults: en. Available
options and more info see here.
units
- determines the units of measurements for the output. Defaults:
metric. Available options and more info see
here.
cache_size
- determines the number of stored locations in cache. Defaults: 10.
ttl
- determines the Time-To-Live of information in cache (in secs). Defaults:
600.
Cache
Each client has its own cache, defined by the number of stored locations and the Time-To-Live (TTL) of the information. In polling mode, the TTL determines the API polling interval.
Weather request
Currently, handling requests for current weather by location name is implemented. By default, the response is returned in a compact format. You can change this behavior by passing an additional argument.
Additional arguments
compact_mode
- determines whether to return the response in a compact format.
Defaults: True.
Description of response formats
Compact format (used by default)
{
"weather": {
"main": "Clear",
"description": "clear sky"
},
"temperature": {
"temp": 8.19,
"feels_like": 7.03
},
"visibility": 10000,
"wind": {
"speed": 2.06
},
"datatime": 1710099501,
"sys": {
"sunrise": 1710051241,
"sunset": 1710092882
},
"timezone": 3600,
"name": "Palais-Royal"
}
weather.main
- group of weather parameters (Rain, Snow, Clouds etc.).
weather.description
- weather condition within the group. More info see
here.
temperature.temp
- temperature. Unit Standart: Kelvin, Metric: Celsius,
Imperial: Fahrenheit.
temperature.feels_like
- temperature. This temperature parameter accounts for
the human perception of weather. Unit Standart: Kelvin, Metric: Celsius,
Imperial: Fahrenheit.
visibility
- visibility, meter. The maximum value of the visibility is 10 km.
wind.speed
- wind speed. Unit Standart: meter/sec, Metric: meter/sec,
Imperial: miles/hour.
datatime
- time of data calculation, unix, UTC.
sys.sunrise
- sunrise time, unix, UTC.
sys.sunset
- sunset time, unix, UTC.
timezone
- shift in seconds from UTC.
name
- city name.
Full format
{
"coord": {
"lon": 2.32,
"lat": 48.858
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 8.19,
"feels_like": 7.03,
"temp_min": 6.07,
"temp_max": 9.42,
"pressure": 998,
"humidity": 86
},
"visibility": 10000,
"wind": {
"speed": 2.06,
"deg": 220
},
"clouds": {
"all": 0
},
"dt": 1710099501,
"sys": {
"type": 2,
"id": 2012208,
"country": "FR",
"sunrise": 1710051241,
"sunset": 1710092882
},
"timezone": 3600,
"id": 6545270,
"name": "Palais-Royal",
"cod": 200
}
Description of full format see here
Usage example
>>> from openweather_sdk import Client
>>> c = Client(token=<YOUR_TOKEN>)
>>> c.health_check()
200
>>> c.get_location_weather("Paris")
{
"weather": {
"main": "Clear",
"description": "clear sky"
},
"temperature": {
"temp": 8.19,
"feels_like": 7.03
},
"visibility": 10000,
"wind": {
"speed": 2.06
},
"datatime": 1710099501,
"sys": {
"sunrise": 1710051241,
"sunset": 1710092882
},
"timezone": 3600,
"name": "Palais-Royal"
}
>>> c.get_location_weather("Paris", compact_mode=False)
{
"coord": {
"lon": 2.32,
"lat": 48.858
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 8.19,
"feels_like": 7.03,
"temp_min": 6.07,
"temp_max": 9.42,
"pressure": 998,
"humidity": 86
},
"visibility": 10000,
"wind": {
"speed": 2.06,
"deg": 220
},
"clouds": {
"all": 0
},
"dt": 1710099501,
"sys": {
"type": 2,
"id": 2012208,
"country": "FR",
"sunrise": 1710051241,
"sunset": 1710092882},
"timezone": 3600,
"id": 6545270,
"name": "Palais-Royal",
"cod": 200
}
>>> c.remove()
Errors
Description of possible errors can be found on FAQ page ("API errors" section).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for msklv_openweather_sdk-0.2.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c60f6cdd9a43a5b30d6408df1e137dae689f7783f734cd4649f1a0a17b57d39c |
|
MD5 | 38d76e28e247f6f812197fdc9f183993 |
|
BLAKE2b-256 | 5e20080599d13a30f3aa258ca49de5534b05d9400f5956bc34c7f78b4eb53169 |
Hashes for msklv_openweather_sdk-0.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0573a95342f41f43b3b19249c680aec1e1829ff61c01cff537c295a8c029adba |
|
MD5 | d27b5b2de407a05774b0f2af68b880e5 |
|
BLAKE2b-256 | f2e5bc366dfbe1aa245289a842527592f350ddaf55fb6df247ec53c4bc71f5e0 |