import requests
import hmac
import hashlib
import time

# إعداد بيانات API الخاصة بك
API_KEY = 'Zsb1k2PttgoA9RTZWP'  # استبدل هذا بمفتاح API الخاص بك
API_SECRET = '2auxrBHoJxUjXebxznSNihvRaWDGhI9Bso15'
BASE_URL = 'https://a...content-available-to-author-only...t.com'

# دالة لتوليد التوقيع
def generate_signature(params, api_secret):
    sorted_params = '&'.join(f"{key}={params[key]}" for key in sorted(params.keys()))
    return hmac.new(
        api_secret.encode('utf-8'),
        sorted_params.encode('utf-8'),
        hashlib.sha256
    ).hexdigest()

# دالة للتحقق من الاتصال
def check_connection():
    endpoint = "/v2/private/wallet/balance"
    timestamp = int(time.time() * 1000)
    params = {
        'api_key': API_KEY,
        'timestamp': timestamp,
    }
    # إضافة التوقيع إلى المعاملات
    params['sign'] = generate_signature(params, API_SECRET)
    
    try:
        response = requests.get(BASE_URL + endpoint, params=params)
        
        if response.status_code == 200:
            print("تم الاتصال بنجاح بحسابك التجريبي على Bybit!")
            print("الاستجابة:", response.json())
        elif response.status_code == 429:
            print("فشل الاتصال بالخادم العام: تم تجاوز الحد المسموح به من الطلبات (429).")
            print("حاول إضافة فترة انتظار أو تقليل عدد الطلبات.")
        elif response.status_code == 404:
            print("فشل الاتصال بالحساب الخاص: المسار غير موجود (404).")
            print("تحقق من صحة المسار في التوثيق.")
        else:
            print(f"فشل الاتصال: رمز الخطأ {response.status_code}")
            print("الاستجابة:", response.json())
    except Exception as e:
        print(f"حدث خطأ أثناء محاولة الاتصال: {e}")

# تنفيذ العملية
check_connection()
