@@ -13,51 +13,70 @@ def __init__(self, server, username, password):
1313 self .password = password
1414 self .session = requests .Session ()
1515
16- def _make_request (self , cmd , method = 'GET' , data = None ):
17- url = f"{ self .server } /CMD_{ cmd } "
18-
19- # Use basic auth
20- auth = (self .username , self .password )
21-
22- try :
23- print (f"Making request to: { url } " )
24-
25- if method == 'GET' :
26- response = self .session .get (
27- url ,
28- auth = auth ,
29- verify = False ,
30- timeout = 10 ,
31- allow_redirects = False # Don't follow redirects
32- )
16+ def _make_request (self , endpoint , data = None ):
17+ """Make request to DirectAdmin API with better parsing"""
18+ try :
19+ url = f"{ self .server } { endpoint } "
20+
21+ # For GET requests (like listing)
22+ if data and data .get ('action' ) == 'list' :
23+ response = requests .get (
24+ url ,
25+ params = data ,
26+ auth = (self .username , self .password ),
27+ verify = False ,
28+ timeout = 10
29+ )
30+ else :
31+ # For POST requests
32+ response = requests .post (
33+ url ,
34+ data = data ,
35+ auth = (self .username , self .password ),
36+ verify = False ,
37+ timeout = 10
38+ )
39+
40+ print (f"API Response Status: { response .status_code } " )
41+ print (f"API Response Headers: { response .headers } " )
42+
43+ if response .status_code == 200 :
44+ content_type = response .headers .get ('Content-Type' , '' )
45+
46+ # Try to parse as JSON first
47+ if 'json' in content_type :
48+ return response .json ()
3349 else :
34- response = self .session .post (
35- url ,
36- auth = auth ,
37- data = data if isinstance (data , str ) else urlencode (data ) if data else None ,
38- verify = False ,
39- timeout = 10 ,
40- headers = {'Content-Type' : 'application/x-www-form-urlencoded' },
41- allow_redirects = False # Don't follow redirects
42- )
43-
44- print (f"Response status: { response .status_code } " )
45-
46- if response .status_code == 401 :
47- raise Exception ("Authentication failed - invalid username or password" )
48- elif response .status_code == 302 or response .status_code == 301 :
49- # DirectAdmin might redirect on auth failure
50- raise Exception ("Authentication required - check credentials" )
51-
52- return response
53- except requests .exceptions .ConnectionError :
54- raise Exception (f"Cannot connect to server at { self .server } " )
55- except requests .exceptions .Timeout :
56- raise Exception ("Connection timeout - server not responding" )
57- except requests .exceptions .SSLError :
58- raise Exception ("SSL error - try using http:// instead of https://" )
59- except requests .exceptions .RequestException as e :
60- raise Exception (f"Connection error: { str (e )} " )
50+ # Parse DirectAdmin's key=value format
51+ text = response .text .strip ()
52+ result = {}
53+
54+ # Handle URL encoded responses
55+ if 'urlencoded' in content_type or '=' in text :
56+ import urllib .parse
57+
58+ # For email lists, DA often returns: list[]=email1&list[]=email2
59+ if 'list[]=' in text :
60+ emails = []
61+ for part in text .split ('&' ):
62+ if part .startswith ('list[]=' ):
63+ email = urllib .parse .unquote (part [7 :])
64+ emails .append (email )
65+ return {'emails' : emails }
66+ else :
67+ # Standard key=value parsing
68+ for line in text .split ('\n ' ):
69+ if '=' in line :
70+ key , value = line .split ('=' , 1 )
71+ result [urllib .parse .unquote (key )] = urllib .parse .unquote (value )
72+
73+ return result if result else text
74+
75+ return None
76+
77+ except Exception as e :
78+ print (f"API request error: { e } " )
79+ return None
6180
6281 def test_connection (self ):
6382 """Test connection with a simple API call"""
0 commit comments