Skip to content

Commit

Permalink
feat(BybitMain): implement session with retry strategy for API requests
Browse files Browse the repository at this point in the history
- Enhanced the BybitMain class to use a requests.Session with a retry strategy, improving resilience against transient network issues.
- Updated API calls to utilize the session, allowing for automatic retries on specified HTTP status codes.
- Added timeout settings to API requests to prevent hanging connections.
  • Loading branch information
saleh-mir committed Dec 28, 2024
1 parent 54ee3f2 commit 551e424
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions jesse/modes/import_candles_mode/drivers/Bybit/BybitMain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import jesse.helpers as jh
from jesse.modes.import_candles_mode.drivers.interface import CandleExchange
from typing import Union
Expand All @@ -14,6 +16,16 @@ def __init__(self, name: str, rest_endpoint: str, category: str) -> None:
self.name = name
self.endpoint = rest_endpoint
self.category = category

# Setup session with retry strategy
self.session = requests.Session()
retries = Retry(
total=3,
backoff_factor=1,
status_forcelist=[408, 429, 500, 502, 503, 504],
allowed_methods=["HEAD", "GET", "POST"]
)
self.session.mount('https://', HTTPAdapter(max_retries=retries, pool_maxsize=100))

def get_starting_time(self, symbol: str) -> int:
dashless_symbol = jh.dashless_symbol(symbol)
Expand All @@ -25,12 +37,11 @@ def get_starting_time(self, symbol: str) -> int:
'start': 1514811660000
}

response = requests.get(self.endpoint + '/v5/market/kline', params=payload)
response = self.session.get(self.endpoint + '/v5/market/kline', params=payload, timeout=10)
self.validate_response(response)
data = response.json()['result']['list']
# Reverse the data list
data = data[::-1]

return int(data[1][0])

def fetch(self, symbol: str, start_timestamp: int, timeframe: str = '1m') -> Union[list, None]:
Expand All @@ -44,7 +55,7 @@ def fetch(self, symbol: str, start_timestamp: int, timeframe: str = '1m') -> Uni
'limit': self.count
}

response = requests.get(self.endpoint + '/v5/market/kline', params=payload)
response = self.session.get(self.endpoint + '/v5/market/kline', params=payload, timeout=10)

if response.json()['retMsg'] != 'OK':
raise exceptions.SymbolNotFound(response.json()['retMsg'])
Expand All @@ -68,8 +79,7 @@ def fetch(self, symbol: str, start_timestamp: int, timeframe: str = '1m') -> Uni
]

def get_available_symbols(self) -> list:
response = requests.get(self.endpoint + '/v5/market/instruments-info?category=' + self.category)
response = self.session.get(self.endpoint + '/v5/market/instruments-info?category=' + self.category, timeout=10)
self.validate_response(response)
data = response.json()['result']['list']

return [jh.dashy_symbol(d['symbol']) for d in data]

0 comments on commit 551e424

Please sign in to comment.