-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d76b3d9
commit f4a038f
Showing
5 changed files
with
41 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
"""Contains the Coinbase class, which is responsible for fetching the ETH/USD""" | ||
"""Helper to fetch the ETH/USD""" | ||
|
||
from prometheus_client import Gauge | ||
from cachetools import func | ||
from pydantic import parse_obj_as | ||
from requests import Session | ||
|
||
from .models import CoinbaseTrade | ||
|
||
|
||
URL = "https://api.pro.coinbase.com/products/ETH-USD/trades" | ||
metric_eth_usd_gauge = Gauge("eth_usd", "ETH/USD conversion rate") | ||
|
||
|
||
class Coinbase: | ||
"""Coinbase abstraction.""" | ||
|
||
def __init__(self) -> None: | ||
"""Coinbase""" | ||
self.__http = Session() | ||
|
||
def emit_eth_usd_conversion_rate(self) -> None: | ||
"""Emit the ETH/USD conversion rate to Prometheus Gauge. | ||
If any error, fails silently. | ||
""" | ||
try: | ||
response = self.__http.get(URL, params=dict(limit=1)) | ||
trades_dict = response.json() | ||
trades = parse_obj_as(list[CoinbaseTrade], trades_dict) | ||
trade, *_ = trades | ||
metric_eth_usd_gauge.set(trade.price) | ||
except: | ||
# This feature is totally optional, so if it fails, we just return 0 | ||
pass | ||
|
||
|
||
@func.ttl_cache(ttl=600) | ||
def get_current_eth_price() -> float: | ||
"""Get the current ETH price in USD. | ||
Returns: | ||
-------- | ||
float | ||
""" | ||
try: | ||
response = Session().get(URL, params=dict(limit=1)) | ||
trades_dict = response.json() | ||
trades = parse_obj_as(list[CoinbaseTrade], trades_dict) | ||
trade, *_ = trades | ||
except: | ||
# This feature is totally optional, so if it fails, we just | ||
# return 0.0. | ||
return 0.0 | ||
|
||
return trade.price |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters