inwx-api-tool/inwx_config.py

85 lines
2.4 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json
# --- Konfiguration ---
API_ENDPOINT = "https://api.domrobot.com/jsonrpc/"
SESSION_ID = None
def api_call(method, params={}):
"""
Führt einen API-Aufruf aus (Post-Login) und gibt die JSON-Antwort zurück.
Verwendet die globale SESSION_ID im Cookie-Header.
"""
global SESSION_ID
# Stelle sicher, dass die Session-ID für alle Anfragen nach dem Login im Cookie-Header ist
headers = {
"Content-Type": "application/json",
"Cookie": f"domrobot={SESSION_ID}"
}
payload = {
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": 1
}
try:
response = requests.post(API_ENDPOINT, headers=headers, data=json.dumps(payload), timeout=10)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"\n❌ API-Fehler bei {method}: {e}")
return None
def login(user, password):
"""Loggt sich in die INWX API ein und speichert die Session-ID."""
global SESSION_ID
print("⏳ Versuche, mich einzuloggen...")
headers = {"Content-Type": "application/json"}
payload = {
"jsonrpc": "2.0",
"method": "account.login",
"params": {"user": user, "pass": password},
"id": 1
}
try:
response = requests.post(API_ENDPOINT, headers=headers, data=json.dumps(payload), timeout=10)
response.raise_for_status()
result_json = response.json()
if result_json.get('code') == 1000:
if 'domrobot' in response.cookies:
SESSION_ID = response.cookies['domrobot']
print("✅ Login erfolgreich! Session-ID gespeichert.")
return True
else:
print("❌ Login fehlgeschlagen: 'code 1000' erhalten, aber kein Session-Cookie.")
else:
print(f"❌ Login fehlgeschlagen: {result_json.get('msg', 'Unbekannter Fehler.')}")
except requests.exceptions.RequestException as e:
print(f"\n❌ API-Fehler beim Login-Request: {e}")
return False
def logout():
"""Loggt sich aus der INWX API aus."""
global SESSION_ID
if not SESSION_ID:
return
print("\n⏳ Logge mich aus...")
# Hier verwenden wir api_call, die jetzt die SESSION_ID verwendet
api_call("account.logout", {})
SESSION_ID = None
print("✅ Logout erfolgreich.")