Skip to content

Commit 3606c77

Browse files
authored
Merge pull request #84 from onesteinbv/16.0-upd-currency_rate_update_coingecko
[UPD] currency_rate_update_coingecko : Removed pycgapi python lib dependency
2 parents 9212629 + 93cfa12 commit 3606c77

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

currency_rate_update_coingecko/__manifest__.py

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@
1313
"application": False,
1414
"depends": ["currency_rate_update_mapping", "crypto_currency"],
1515
"data": [],
16-
"external_dependencies": {"python": ["pycgapi"]},
1716
}

currency_rate_update_coingecko/models/res_currency_rate_provider_CoinGecko.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import logging
44
from datetime import timedelta
55

6-
from pycgapi import CoinGeckoAPI
6+
import requests
7+
from requests.adapters import HTTPAdapter
8+
from urllib3.util import Retry
79

810
from odoo import _, fields, models
911

1012
_logger = logging.getLogger(__name__)
13+
API_URL = "https://api.coingecko.com/api/v3/coins/%s/history"
1114

1215

1316
class ResCurrencyRateProviderCoinGecko(models.Model):
@@ -40,7 +43,6 @@ def _obtain_rates(self, base_currency, currencies, date_from, date_to):
4043

4144
def _get_historical_rate_from_coingecko(self, date_from, date_to, base_currency):
4245
"""Get all the exchange rates from 'date_from' to 'date_to'"""
43-
api = CoinGeckoAPI()
4446
content = {}
4547
current_date = date_from
4648
while current_date <= date_to:
@@ -51,8 +53,8 @@ def _get_historical_rate_from_coingecko(self, date_from, date_to, base_currency)
5153
lambda rpm: rpm.provider_service == self.service
5254
):
5355
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
5658
)
5759
except Exception as e:
5860
_logger.warning(
@@ -87,3 +89,19 @@ def _get_historical_rate_from_coingecko(self, date_from, date_to, base_currency)
8789
)
8890
current_date += timedelta(days=1)
8991
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()

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ xlrd
2828
python-slugify
2929
vcrpy
3030
dnspython==2.6.1
31-
pycgapi
3231
bokeh==3.1.1

0 commit comments

Comments
 (0)