Skip to content

Commit

Permalink
chore: add requests exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chilango74 committed Sep 1, 2021
1 parent 3d3a8d3 commit 2085a60
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
6 changes: 1 addition & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import okama as ok

ccy = 'RUB'
ls = ['RGBITR.INDX', 'MCFTR.INDX', 'SPY.US', 'PGJ.US', 'GLD.US']
cons_weights=[0.65, 0.06, 0.19, 0.05, 0.05]
port_cons_no_infl = ok.Portfolio(ls, ccy=ccy, weights=cons_weights, inflation=False)
print(port_cons_no_infl.percentile_from_history(years=8))
print(ok.Asset('SPY.US'))
14 changes: 9 additions & 5 deletions okama/api/api_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ def connect(
params = {"first_date": first_date, "last_date": last_date, "period": period}
session.mount("https://", adapter)
session.mount("http://", adapter)
r = session.get(request_url, params=params, verify=False, timeout=cls.default_timeout)
if r.status_code != requests.codes.ok:
raise Exception(
f"Error fetching data for {symbol}:",
try:
r = session.get(request_url, params=params, verify=False, timeout=cls.default_timeout)
r.raise_for_status()
except requests.exceptions.HTTPError as errh:
if r.status_code == 404:
raise requests.exceptions.HTTPError(f"{symbol} is not found in the database.") from errh
raise requests.exceptions.HTTPError(
f"HTTP error fetching data for {symbol}:",
r.status_code,
r.reason,
request_url,
)
) from errh
return r.text

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/test_asset_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ def test_get_cumulative_return(self, input_data, expected1, expected2, expected3

cumulative_testdata2 = [
("YTD", -0.0614, -0.0674),
(1, -0.1383, 0.1863),
(None, -0.1912, 0.2530),
(1, -0.1383, 0.1745),
(None, -0.1912, 0.2406),
]

@mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def test_get_cagr_real_no_inflation_exception(portfolio_no_inflation):

@mark.parametrize(
"period, real, expected",
[("YTD", False, 0.01505), (1, False, 0.12269), (2, True, 0.1381)],
[("YTD", False, 0.01505), (1, False, 0.12269), (2, True, 0.1268)],
)
def test_cumulative_return(portfolio_rebalanced_month, period, real, expected):
assert portfolio_rebalanced_month.get_cumulative_return(
Expand Down

0 comments on commit 2085a60

Please sign in to comment.