1
|
|
|
# !/usr/bin/env python3 |
2
|
|
|
|
3
|
|
|
"""Implements the client for Series.""" |
4
|
|
|
|
5
|
|
|
# pylint: skip-file |
6
|
|
|
# pydocstyle: add-ignore=D105,D107,D401 |
7
|
|
|
|
8
|
1 |
|
from typing import Dict, Optional |
9
|
|
|
|
10
|
1 |
|
import requests |
11
|
1 |
|
import requests.auth |
12
|
|
|
|
13
|
1 |
|
from .. import __version__ |
14
|
1 |
|
from ..exceptions import ResponseError |
15
|
|
|
|
16
|
|
|
|
17
|
1 |
|
class SieAPIRest: |
18
|
|
|
""" |
19
|
|
|
SIE-BANXICO API |
20
|
|
|
API de consulta de series de tiempo del Banco de México |
21
|
|
|
version: 1.4.3-hotfix1 |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
def __init__( |
25
|
|
|
self, |
26
|
|
|
bmx_token: str, |
27
|
|
|
url_prefix: str = "https://www.banxico.org.mx") -> None: |
28
|
1 |
|
self.headers = { |
29
|
|
|
'Bmx-Token': bmx_token, |
30
|
|
|
'Accept': 'application/json', |
31
|
|
|
"User-Agent": __version__.__user_agent__ |
32
|
|
|
} |
33
|
1 |
|
self.url_prefix = url_prefix |
34
|
|
|
|
35
|
1 |
|
def _request(self, method, path, params=None): |
36
|
1 |
|
print(f"{self.url_prefix}/{path}") |
37
|
|
|
|
38
|
1 |
|
resp = requests.request( |
39
|
|
|
method=method, |
40
|
|
|
url=f"{self.url_prefix}/{path}", |
41
|
|
|
headers=self.headers, |
42
|
|
|
params=params |
43
|
|
|
) |
44
|
|
|
|
45
|
1 |
|
if resp.status_code == 200: |
46
|
1 |
|
return resp.json() |
47
|
|
|
|
48
|
|
|
raise ResponseError(resp.text) |
49
|
|
|
|
50
|
1 |
|
def metadatos_series_using_get( |
51
|
|
|
self, |
52
|
|
|
id_series: str, |
53
|
|
|
) -> str: |
54
|
|
|
""" |
55
|
|
|
Series metadata |
56
|
|
|
Send a get request to SieAPIRest/service/v1/series/{idSeries}. |
57
|
|
|
|
58
|
|
|
:param id_series: idSeries |
59
|
|
|
:return: OK |
60
|
|
|
""" |
61
|
|
|
return self._request( |
62
|
|
|
method="get", |
63
|
|
|
path=f"SieAPIRest/service/v1/series/{id_series}" |
64
|
|
|
) |
65
|
|
|
|
66
|
1 |
|
def datos_series_using_get( |
67
|
|
|
self, |
68
|
|
|
id_series: str, |
69
|
|
|
decimales: Optional[str] = None, |
70
|
|
|
incremento: Optional[str] = None, |
71
|
|
|
) -> str: |
72
|
|
|
""" |
73
|
|
|
Series data |
74
|
|
|
Send a get request to SieAPIRest/service/v1/series/{idSeries}/datos. |
75
|
|
|
|
76
|
|
|
:param id_series: idSeries |
77
|
|
|
:param decimales: 'sinCeros' to remove trailing zeros to the right of the decimal point. |
78
|
|
|
:param incremento: 'PorcObsAnt', 'PorcAnual', 'PorcAcumAnual'. |
79
|
|
|
:return: OK |
80
|
|
|
""" |
81
|
|
|
params = {} # type: Dict[str, str] |
82
|
|
|
if decimales is not None: |
83
|
|
|
params['decimales'] = decimales |
84
|
|
|
if incremento is not None: |
85
|
|
|
params['incremento'] = incremento |
86
|
|
|
|
87
|
|
|
return self._request( |
88
|
|
|
method='get', |
89
|
|
|
path=f"SieAPIRest/service/v1/series/{id_series}/datos", |
90
|
|
|
params=params |
91
|
|
|
) |
92
|
|
|
|
93
|
1 |
|
def datos_oportuno_series_using_get( |
94
|
|
|
self, |
95
|
|
|
id_series: str, |
96
|
|
|
decimales: Optional[str] = None, |
97
|
|
|
incremento: Optional[str] = None, |
98
|
|
|
) -> str: |
99
|
|
|
""" |
100
|
|
|
Last data |
101
|
|
|
Send a get request to SieAPIRest/service/v1/series/{idSeries}/datos/oportuno. |
102
|
|
|
|
103
|
|
|
:param id_series: idSeries |
104
|
|
|
:param decimales: 'sinCeros' to remove trailing zeros to the right of the decimal point. |
105
|
|
|
:param incremento: 'PorcObsAnt', 'PorcAnual', 'PorcAcumAnual'. |
106
|
|
|
:return: OK |
107
|
|
|
""" |
108
|
1 |
|
params = {} # type: Dict[str, str] |
109
|
1 |
|
if decimales is not None: |
110
|
|
|
params['decimales'] = decimales |
111
|
1 |
|
if incremento is not None: |
112
|
|
|
params['incremento'] = incremento |
113
|
|
|
|
114
|
1 |
|
return self._request( |
115
|
|
|
method='get', |
116
|
|
|
path=f"SieAPIRest/service/v1/series/{id_series}/datos/oportuno", |
117
|
|
|
params=params, |
118
|
|
|
) |
119
|
|
|
|
120
|
1 |
|
def obten_rango_datos_series_using_get( |
121
|
|
|
self, |
122
|
|
|
id_series: str, |
123
|
|
|
fecha_inicial: str, |
124
|
|
|
fecha_final: str, |
125
|
|
|
decimales: Optional[str] = None, |
126
|
|
|
incremento: Optional[str] = None, |
127
|
|
|
) -> str: |
128
|
|
|
""" |
129
|
|
|
Data range |
130
|
|
|
Send a get request to SieAPIRest/service/v1/series/{idSeries}/datos/{fechaInicial}/{fechaFinal}. |
131
|
|
|
|
132
|
|
|
:param id_series: idSeries |
133
|
|
|
:param fecha_inicial: fechaInicial |
134
|
|
|
:param fecha_final: fechaFinal |
135
|
|
|
:param decimales: 'sinCeros' to remove trailing zeros to the right of the decimal point. |
136
|
|
|
:param incremento: 'PorcObsAnt', 'PorcAnual', 'PorcAcumAnual'. |
137
|
|
|
:return: OK |
138
|
|
|
""" |
139
|
|
|
params = {} # type: Dict[str, str] |
140
|
|
|
if decimales is not None: |
141
|
|
|
params['decimales'] = decimales |
142
|
|
|
if incremento is not None: |
143
|
|
|
params['incremento'] = incremento |
144
|
|
|
|
145
|
|
|
return self._request( |
146
|
|
|
method='get', |
147
|
|
|
path=f"SieAPIRest/service/v1/series/{id_series}/datos/{fecha_inicial}/{fecha_final}", |
148
|
|
|
params=params, |
149
|
|
|
) |
150
|
|
|
|
151
|
1 |
|
def metadatos_series_versionadas_using_get( |
152
|
|
|
self, |
153
|
|
|
id_series: str, |
154
|
|
|
) -> str: |
155
|
|
|
""" |
156
|
|
|
Series versions metadata |
157
|
|
|
Send a get request to SieAPIRest/service/v1/series/{idSeries}/versiones. |
158
|
|
|
|
159
|
|
|
:param id_series: idSeries |
160
|
|
|
:return: OK |
161
|
|
|
""" |
162
|
|
|
return self._request( |
163
|
|
|
method='get', |
164
|
|
|
path=f"SieAPIRest/service/v1/series/{id_series}/versiones", |
165
|
|
|
) |
166
|
|
|
|
167
|
1 |
|
def datos_series_versionadas_using_get( |
168
|
|
|
self, |
169
|
|
|
id_series: str, |
170
|
|
|
id_version: str, |
171
|
|
|
decimales: Optional[str] = None, |
172
|
|
|
incremento: Optional[str] = None, |
173
|
|
|
) -> str: |
174
|
|
|
""" |
175
|
|
|
Series data |
176
|
|
|
Send a get request to SieAPIRest/service/v1/series/{idSeries}/versiones/{idVersion}/datos. |
177
|
|
|
|
178
|
|
|
:param id_series: idSeries |
179
|
|
|
:param id_version: idVersion |
180
|
|
|
:param decimales: 'sinCeros' to remove trailing zeros to the right of the decimal point. |
181
|
|
|
:param incremento: 'PorcObsAnt', 'PorcAnual', 'PorcAcumAnual'. |
182
|
|
|
:return: OK |
183
|
|
|
""" |
184
|
|
|
params = {} # type: Dict[str, str] |
185
|
|
|
if decimales is not None: |
186
|
|
|
params['decimales'] = decimales |
187
|
|
|
if incremento is not None: |
188
|
|
|
params['incremento'] = incremento |
189
|
|
|
|
190
|
|
|
return self._request( |
191
|
|
|
method='get', |
192
|
|
|
path=f"SieAPIRest/service/v1/series/{id_series}/versiones/{id_version}/datos", |
193
|
|
|
params=params, |
194
|
|
|
) |
195
|
|
|
|