diff --git a/README.md b/README.md index 2828c12..8992ccc 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,22 @@ api.cmd_api_account_user( ) ``` +Using GET Method + +The API class supports both POST and GET methods for interacting with the DirectAdmin API. By default, the call method uses POST, but you can specify GET by setting the method parameter to "get". + +Example + +Here is an example of how to use the GET method with the API class: +``` + +from directadmin.api import API + + +API().cmd_file_manager(json="yes", method="get", action="json_all", filemanager_du=0, ipp=50, page=1, path=path, comparison6="contains", value6=file) + +``` + ## Impersonation You can use `become()` to impersonate another user as an admin or reseller. For diff --git a/directadmin/api.py b/directadmin/api.py index 2988703..81b06e5 100644 --- a/directadmin/api.py +++ b/directadmin/api.py @@ -18,6 +18,15 @@ class UnauthorizedException(Exception): class API(object): '''API() connects, sends and processes API calls.''' def __init__(self, username, password, server, json=False, debug=False): + ''' + Initialize the API object. + + :param username: str - The username for authentication. + :param password: str - The password for authentication. + :param server: str - The server URL. + :param json: bool - Whether to request JSON responses (default: False). + :param debug: bool - Whether to enable debug mode (default: False). + ''' self.username = username self.password = password self.server = server @@ -32,11 +41,11 @@ def process_errors(self, data): # Check for errors if 'error' in data: if self.json: - details = data['result'] - text = data['error'] + details = data['result'] if 'result' in data else '' + text = data['error'] if 'error' in data else '' else: - details = data['details'][0] if 'details' in data else '' - text = data['text'][0] if 'text' in data else '' + details = data['details'][0] if 'details' in data and data['details'] else '' + text = data['text'][0] if 'text' in data and data['text'] else '' error = '{}: {}'.format( text.rstrip('\r\n'), self.lowerfirst(details).rstrip('\r\n') @@ -54,7 +63,7 @@ def process_response(self, response): try: data = response.json() - except simplejson.errors.JSONDecodeError: + except simplejson.JSONDecodeError: data = parse_qs(response.text) self.process_errors(data) @@ -72,18 +81,25 @@ def debug_response(self, response): print('Headers:\r\n{}'.format(response.headers)) print('Output:\r\n{}'.format(response.text)) - def call(self, command, params={}): + def call(self, command, params={}, method="post"): '''Send data to the API through a post request''' # Only request json when we can if self.json: params['json'] = 'yes' - response = requests.post( - url='{}/{}'.format(self.server, command), - auth=(self.username, self.password), - params=params, - ) - + if method == "get": + response = requests.get( + url='{}/{}'.format(self.server, command), + auth=(self.username, self.password), + params=params, + ) + else: + response = requests.post( + url='{}/{}'.format(self.server, command), + auth=(self.username, self.password), + params=params, + ) + if self.debug: self.debug_response(response=response) @@ -107,12 +123,13 @@ def method(*args, **kwargs): params = args[0] else: params = kwargs - + method = params.pop('method', 'post') # Validate data before sending a request to the API if isinstance(params, dict): return self.call( command=name.upper(), params=params, + method=method ) raise ResponseException('Invalid argument')