meteocatpy 0.0.14 → 0.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/meteocatpy/__init__.py +3 -1
- package/meteocatpy/const.py +1 -0
- package/meteocatpy/uvi.py +84 -0
- package/meteocatpy/version.py +1 -1
- package/package.json +1 -1
- package/pyproject.toml +1 -1
- package/tests/integration_test_uvi.py +34 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
## [0.0.15](https://github.com/figorr/meteocatpy/compare/v0.0.14...v0.0.15) (2024-12-13)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* 0.0.15 ([e4362bf](https://github.com/figorr/meteocatpy/commit/e4362bf9830c8eb5f9aa6fd803d06bf6bb64c2c8))
|
|
7
|
+
* add MeteocatUviData ([49807fe](https://github.com/figorr/meteocatpy/commit/49807fe195098db59a654d015942352cb3705b93))
|
|
8
|
+
* add MeteocatUviData class ([16e5eaf](https://github.com/figorr/meteocatpy/commit/16e5eaf7c918c673ab4648d1c3194144605d3249))
|
|
9
|
+
* add uvi url ([b6a42c5](https://github.com/figorr/meteocatpy/commit/b6a42c5f2bc405359c49c7c5f106d576f5c70d9e))
|
|
10
|
+
* ignore uvi test json ([4df4501](https://github.com/figorr/meteocatpy/commit/4df450166c4d01b67e096a754506d6d939ba09a5))
|
|
11
|
+
|
|
1
12
|
## [0.0.14](https://github.com/figorr/meteocatpy/compare/v0.0.13...v0.0.14) (2024-12-12)
|
|
2
13
|
|
|
3
14
|
|
package/meteocatpy/__init__.py
CHANGED
|
@@ -15,6 +15,7 @@ from .stations import MeteocatStations
|
|
|
15
15
|
from .townstations import MeteocatTownStations
|
|
16
16
|
from .data import MeteocatStationData
|
|
17
17
|
from .variables import MeteocatVariables
|
|
18
|
+
from .uvi import MeteocatUviData
|
|
18
19
|
|
|
19
20
|
__all__ = [
|
|
20
21
|
"MeteocatTown",
|
|
@@ -23,5 +24,6 @@ __all__ = [
|
|
|
23
24
|
"MeteocatStations",
|
|
24
25
|
"MeteocatTownStations",
|
|
25
26
|
"MeteocatStationData",
|
|
26
|
-
"MeteocatVariables"
|
|
27
|
+
"MeteocatVariables",
|
|
28
|
+
"MeteocatUviData"
|
|
27
29
|
]
|
package/meteocatpy/const.py
CHANGED
|
@@ -8,3 +8,4 @@ STATIONS_LIST_URL = "/xema/v1/estacions/metadades"
|
|
|
8
8
|
STATIONS_MUNICIPI_URL = "/xema/v1/representatives/metadades/municipis/{codi_municipi}/variables/{codi_variable}"
|
|
9
9
|
VARIABLES_URL = "/xema/v1/variables/mesurades/metadades"
|
|
10
10
|
STATION_DATA_URL = "/xema/v1/estacions/mesurades/{codiEstacio}/{any}/{mes}/{dia}"
|
|
11
|
+
UVI_DATA_URL = "/pronostic/v1/uvi/{codi_municipi}"
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import aiohttp
|
|
2
|
+
import logging
|
|
3
|
+
from .const import (
|
|
4
|
+
BASE_URL,
|
|
5
|
+
UVI_DATA_URL
|
|
6
|
+
)
|
|
7
|
+
from .exceptions import (
|
|
8
|
+
BadRequestError,
|
|
9
|
+
ForbiddenError,
|
|
10
|
+
TooManyRequestsError,
|
|
11
|
+
InternalServerError,
|
|
12
|
+
UnknownAPIError,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
_LOGGER = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
class MeteocatUviData:
|
|
18
|
+
"""Clase para interactuar con los datos del índice UVI de la API de Meteocat."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, api_key: str):
|
|
21
|
+
"""
|
|
22
|
+
Inicializa la clase MeteocatUviData.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
api_key (str): Clave de API para autenticar las solicitudes.
|
|
26
|
+
"""
|
|
27
|
+
self.api_key = api_key
|
|
28
|
+
self.headers = {
|
|
29
|
+
"Content-Type": "application/json",
|
|
30
|
+
"X-Api-Key": self.api_key,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async def get_uvi_index(self, town_id: str):
|
|
34
|
+
"""
|
|
35
|
+
Obtiene los datos del índice UVI para un municipio.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
town_id (str): Código del municipio.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
dict: Datos del índice UVI del municipio.
|
|
42
|
+
"""
|
|
43
|
+
if not town_id:
|
|
44
|
+
raise ValueError("El parámetro 'town_id' no puede estar vacío.")
|
|
45
|
+
|
|
46
|
+
url = f"{BASE_URL}{UVI_DATA_URL}".format(
|
|
47
|
+
codi_municipi=town_id
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
async with aiohttp.ClientSession() as session:
|
|
51
|
+
try:
|
|
52
|
+
async with session.get(url, headers=self.headers) as response:
|
|
53
|
+
if response.status == 200:
|
|
54
|
+
return await response.json()
|
|
55
|
+
|
|
56
|
+
# Gestionar errores según el código de estado
|
|
57
|
+
if response.status == 400:
|
|
58
|
+
raise BadRequestError(await response.json())
|
|
59
|
+
elif response.status == 403:
|
|
60
|
+
error_data = await response.json()
|
|
61
|
+
if error_data.get("message") == "Forbidden":
|
|
62
|
+
raise ForbiddenError(error_data)
|
|
63
|
+
elif error_data.get("message") == "Missing Authentication Token":
|
|
64
|
+
raise ForbiddenError(error_data)
|
|
65
|
+
elif response.status == 429:
|
|
66
|
+
raise TooManyRequestsError(await response.json())
|
|
67
|
+
elif response.status == 500:
|
|
68
|
+
raise InternalServerError(await response.json())
|
|
69
|
+
else:
|
|
70
|
+
raise UnknownAPIError(
|
|
71
|
+
f"Unexpected error {response.status}: {await response.text()}"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
except aiohttp.ClientError as e:
|
|
75
|
+
raise UnknownAPIError(
|
|
76
|
+
message=f"Error al conectar con la API de Meteocat: {str(e)}",
|
|
77
|
+
status_code=0,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
except Exception as ex:
|
|
81
|
+
raise UnknownAPIError(
|
|
82
|
+
message=f"Error inesperado: {str(ex)}",
|
|
83
|
+
status_code=0,
|
|
84
|
+
)
|
package/meteocatpy/version.py
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
# version.py
|
|
2
|
-
__version__ = "0.0.
|
|
2
|
+
__version__ = "0.0.15"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "meteocatpy",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.15",
|
|
4
4
|
"description": "[](https://opensource.org/licenses/Apache-2.0)\r [](https://pypi.org/project/meteocatpy)\r [](https://gitlab.com/figorr/meteocatpy/commits/master)",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
package/pyproject.toml
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import pytest
|
|
3
|
+
import json
|
|
4
|
+
from dotenv import load_dotenv
|
|
5
|
+
from meteocatpy.uvi import MeteocatUviData
|
|
6
|
+
|
|
7
|
+
# Cargar variables desde el archivo .env
|
|
8
|
+
load_dotenv()
|
|
9
|
+
|
|
10
|
+
# Obtener los valores del archivo .env
|
|
11
|
+
API_KEY = os.getenv("METEOCAT_API_KEY")
|
|
12
|
+
MUNICIPI_CODI_TEST = os.getenv("MUNICIPI_CODI_TEST")
|
|
13
|
+
|
|
14
|
+
# Asegúrate de que las variables estén definidas
|
|
15
|
+
assert API_KEY, "API Key is required"
|
|
16
|
+
assert MUNICIPI_CODI_TEST, "Station codi test is required"
|
|
17
|
+
|
|
18
|
+
@pytest.mark.asyncio
|
|
19
|
+
async def test_municipis():
|
|
20
|
+
# Crear una instancia de MeteocatUviData con la API Key
|
|
21
|
+
uvi_client = MeteocatUviData(API_KEY)
|
|
22
|
+
|
|
23
|
+
# Obtener los datos del índice UVI
|
|
24
|
+
uvi_data = await uvi_client.get_uvi_index(MUNICIPI_CODI_TEST)
|
|
25
|
+
|
|
26
|
+
# Crear la carpeta si no existe
|
|
27
|
+
os.makedirs('tests/files', exist_ok=True)
|
|
28
|
+
|
|
29
|
+
# Guardar los datos del índice UVI en un archivo JSON
|
|
30
|
+
with open(f'tests/files/uvi_index_{MUNICIPI_CODI_TEST}.json', 'w', encoding='utf-8') as f:
|
|
31
|
+
json.dump(uvi_data, f, ensure_ascii=False, indent=4)
|
|
32
|
+
|
|
33
|
+
# Verificar que los datos del índice UVI no estén vacíos
|
|
34
|
+
assert uvi_data, "UVI data is empty"
|