3
3
import logging
4
4
from datetime import timedelta
5
5
6
- from pycgapi import CoinGeckoAPI
6
+ import requests
7
+ from requests .adapters import HTTPAdapter
8
+ from urllib3 .util import Retry
7
9
8
10
from odoo import _ , fields , models
9
11
10
12
_logger = logging .getLogger (__name__ )
13
+ API_URL = "https://api.coingecko.com/api/v3/coins/%s/history"
11
14
12
15
13
16
class ResCurrencyRateProviderCoinGecko (models .Model ):
@@ -40,7 +43,6 @@ def _obtain_rates(self, base_currency, currencies, date_from, date_to):
40
43
41
44
def _get_historical_rate_from_coingecko (self , date_from , date_to , base_currency ):
42
45
"""Get all the exchange rates from 'date_from' to 'date_to'"""
43
- api = CoinGeckoAPI ()
44
46
content = {}
45
47
current_date = date_from
46
48
while current_date <= date_to :
@@ -51,8 +53,8 @@ def _get_historical_rate_from_coingecko(self, date_from, date_to, base_currency)
51
53
lambda rpm : rpm .provider_service == self .service
52
54
):
53
55
try :
54
- coin_data = api . coin_historical_on_date (
55
- currency .provider_reference , current_date . strftime ( "%m-%d-%Y" )
56
+ coin_data = self . _get_coin_data_for_date (
57
+ currency .provider_reference , current_date
56
58
)
57
59
except Exception as e :
58
60
_logger .warning (
@@ -87,3 +89,19 @@ def _get_historical_rate_from_coingecko(self, date_from, date_to, base_currency)
87
89
)
88
90
current_date += timedelta (days = 1 )
89
91
return content
92
+
93
+ def _get_coin_data_for_date (self , provider_reference , current_date ):
94
+ """Get the exchange rate for a coin on the given date"""
95
+ retries = Retry (
96
+ total = 5 ,
97
+ backoff_factor = 1 ,
98
+ status_forcelist = [429 , 500 , 502 , 503 , 504 ],
99
+ allowed_methods = ["HEAD" , "GET" , "OPTIONS" ],
100
+ )
101
+ adapter = HTTPAdapter (max_retries = retries )
102
+ session = requests .Session ()
103
+ session .mount ("https://" , adapter )
104
+ params = {"date" : current_date .strftime ("%d-%m-%Y" ), "localization" : "en" }
105
+ response = session .get (API_URL % provider_reference , params = params )
106
+ response .raise_for_status ()
107
+ return response .json ()
0 commit comments