Skip to content

Commit d39e64b

Browse files
committed
Refactor URL construction and error messages in currency and SGS modules for improved readability
1 parent 18b17ae commit d39e64b

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

bcb/currency.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
def _currency_url(currency_id: str, start_date: DateInput, end_date: DateInput) -> str:
2020
start_date = Date(start_date)
2121
end_date = Date(end_date)
22-
url = (
23-
"https://ptax.bcb.gov.br/ptax_internet/consultaBoletim.do?"
24-
"method=gerarCSVFechamentoMoedaNoPeriodo&"
25-
"ChkMoeda={}&DATAINI={:%d/%m/%Y}&DATAFIM={:%d/%m/%Y}"
22+
return (
23+
f"https://ptax.bcb.gov.br/ptax_internet/consultaBoletim.do?"
24+
f"method=gerarCSVFechamentoMoedaNoPeriodo&"
25+
f"ChkMoeda={currency_id}&DATAINI={start_date.date:%d/%m/%Y}&DATAFIM={end_date.date:%d/%m/%Y}"
2626
)
27-
return url.format(currency_id, start_date.date, end_date.date)
2827

2928

3029
CACHE = dict()
@@ -115,7 +114,7 @@ def _get_symbol(symbol: str, start_date: DateInput, end_date: DateInput) -> pd.D
115114
x = elm.text
116115
x = re.sub(r"^\W+", "", x)
117116
x = re.sub(r"\W+$", "", x)
118-
msg = "BCB API returned error: {} - {}".format(x, symbol)
117+
msg = f"BCB API returned error: {x} - {symbol}"
119118
warnings.warn(msg)
120119
return None
121120

bcb/sgs/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ def _get_url_and_payload(code: int, start_date: DateInput, end_date: DateInput,
6161
payload["dataInicial"] = Date(start_date).date.strftime("%d/%m/%Y")
6262
end_date = end_date if end_date else "today"
6363
payload["dataFinal"] = Date(end_date).date.strftime("%d/%m/%Y")
64-
url = "https://api.bcb.gov.br/dados/serie/bcdata.sgs.{}/dados".format(code)
64+
url = f"https://api.bcb.gov.br/dados/serie/bcdata.sgs.{code}/dados"
6565
else:
66-
url = ("https://api.bcb.gov.br/dados/serie/bcdata.sgs.{}/dados" "/ultimos/{}").format(code, last)
66+
url = f"https://api.bcb.gov.br/dados/serie/bcdata.sgs.{code}/dados/ultimos/{last}"
6767

6868
return {"payload": payload, "url": url}
6969

@@ -181,8 +181,8 @@ def get_json(code: int, start: Optional[DateInput] = None, end: Optional[DateInp
181181
except Exception:
182182
res_json = {}
183183
if "error" in res_json:
184-
raise Exception("BCB error: {}".format(res_json["error"]))
184+
raise Exception(f"BCB error: {res_json['error']}")
185185
elif "erro" in res_json:
186-
raise Exception("BCB error: {}".format(res_json["erro"]["detail"]))
187-
raise Exception("Download error: code = {}".format(code))
186+
raise Exception(f"BCB error: {res_json['erro']['detail']}")
187+
raise Exception(f"Download error: code = {code}")
188188
return res.text

0 commit comments

Comments
 (0)