forked from Kiyoraka/BYBIT-SIGNAL-MONITOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_handler.py
More file actions
62 lines (54 loc) · 1.96 KB
/
api_handler.py
File metadata and controls
62 lines (54 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
import os
import ccxt
from pathlib import Path
class APIHandler:
def __init__(self):
self.settings_dir = "settings"
self.api_file = os.path.join(self.settings_dir, "apikey.json")
self.ensure_settings_dir()
def ensure_settings_dir(self):
"""Create settings directory if it doesn't exist"""
if not os.path.exists(self.settings_dir):
os.makedirs(self.settings_dir)
def save_api_credentials(self, api_key, api_secret):
"""Save API credentials to JSON file"""
try:
credentials = {
"api_key": api_key,
"api_secret": api_secret
}
with open(self.api_file, 'w') as f:
json.dump(credentials, f, indent=4)
return True, "API credentials saved successfully"
except Exception as e:
return False, f"Error saving API credentials: {str(e)}"
def test_connection(self, api_key, api_secret):
"""Test Bybit API connection"""
try:
exchange = ccxt.bybit({
'apiKey': api_key,
'secret': api_secret,
'options': {
'defaultType': 'future',
'adjustForTimeDifference': True,
'recvWindow': 60000
},
'enableRateLimit': True
})
# Test connection by fetching balance
exchange.fetch_balance()
return True, "API connection successful"
except Exception as e:
return False, f"API connection failed: {str(e)}"
def load_credentials(self):
"""Load existing API credentials"""
try:
if os.path.exists(self.api_file):
with open(self.api_file, 'r') as f:
return json.load(f)
return None
except Exception:
return None
# Create handler instance
api_handler = APIHandler()