Skip to content

Commit

Permalink
Add includes and params to client API.
Browse files Browse the repository at this point in the history
Since the client API is limited in scope to an individual server there were only a few places it seemed prudent to add includes and params.  It's quite possible that some endpoints were overlooked, but I believe I got all the endpoints that cannot be updated in a way that is backwards compatible.
  • Loading branch information
iamkubi committed Oct 13, 2022
1 parent 0aab24a commit f516c1c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
19 changes: 14 additions & 5 deletions pydactyl/api/client/servers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ class ServersBase(base.PterodactylAPI):
when using PterodactylClient.
"""

def list_servers(self):
"""List all servers the client has access to."""
def list_servers(self, includes=None, params=None):
"""List all servers the client has access to.
Args:
includes(iter): List of includes, e.g. ('egg', 'subusers')
params(dict): Extra parameters to pass, e.g. {'per_page': 300}
"""
endpoint = 'client'
response = self._api_request(endpoint=endpoint)
response = self._api_request(endpoint=endpoint, includes=includes,
params=params)
return PaginatedResponse(self, endpoint, response)

def list_permissions(self):
Expand All @@ -26,16 +32,19 @@ def list_permissions(self):
response = self._api_request(endpoint=endpoint)
return response

def get_server(self, server_id, detail=False):
def get_server(self, server_id, detail=False, includes=None, params=None):
"""Get information for the specified server.
Args:
server_id(str): Server identifier (abbreviated UUID)
detail(bool): If True includes the object type and a nested data
structure. This is not particularly useful.
includes(iter): List of includes, e.g. ('egg', 'subusers')
params(dict): Extra parameters to pass, e.g. {'per_page': 300}
"""
response = self._api_request(
endpoint='client/servers/{}'.format(server_id))
endpoint='client/servers/{}'.format(server_id),
includes=includes, params=params)
return base.parse_response(response, detail)

def get_server_utilization(self, server_id, detail=False):
Expand Down
11 changes: 7 additions & 4 deletions pydactyl/api/client/servers/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
class Databases(base.PterodactylAPI):
"""Pterodactyl Client Server Databases API."""

def list_databases(self, server_id: str, include_passwords: bool = False):
def list_databases(self, server_id: str, include_passwords: bool = False,
includes: list = [], params: dict = None):
"""List all databases for a server.
Optionally includes the database user passwords.
Args:
server_id(str): Server identifier (abbreviated UUID)
include_passwords(bool): True to include database user passwords
includes(iter): List of includes, e.g. ('password')
params(dict): Extra parameters to pass, e.g. {'per_page': 300}
"""
params = {}
if include_passwords:
params['include'] = 'password'
includes.append('password')
endpoint = 'client/servers/{}/databases'.format(server_id)
response = self._api_request(endpoint=endpoint, params=params)
response = self._api_request(endpoint=endpoint,
includes=includes or None, params=params)
return response

def create_database(self, server_id: str, name: str, remote: str = '%'):
Expand Down
22 changes: 21 additions & 1 deletion tests/client/client_servers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,35 @@ class ClientServersTests(unittest.TestCase):

def setUp(self):
self.api = PterodactylClient(url='dummy', api_key='dummy')

def test_invalid_power_action_raises_exception(self):
with self.assertRaises(BadRequestError):
self.api.client.servers.send_power_action(1, 'BADSIGNAL')

@mock.patch('pydactyl.api.base.PterodactylAPI._api_request')
def test_list_servers(self, mock_api):
expected = {
'endpoint': 'client',
'includes': ('egg', 'subusers'),
'params': {'per_page': 800},
}
self.api.client.servers.list_servers(includes=('egg','subusers'),
params={'per_page': 800})
mock_api.assert_called_with(**expected)

@mock.patch('pydactyl.api.base.PterodactylAPI._api_request')
def test_list_permissions(self, mock_api):
expected = {
'endpoint': 'client/permissions',
}
self.api.client.servers.list_permissions()
mock_api.assert_called_with(**expected)

@mock.patch('pydactyl.api.base.PterodactylAPI._api_request')
def test_get_server(self, mock_api):
expected = {
'endpoint': 'client/servers/11',
'includes': None,
'params': None,
}
self.api.client.servers.get_server(11)
mock_api.assert_called_with(**expected)
Expand Down
6 changes: 4 additions & 2 deletions tests/client/databases_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def setUp(self):
def test_list_databases(self, mock_api):
expected = {
'endpoint': 'client/servers/fds173/databases',
'params': {},
'includes': None,
'params': None,
}
self.api.client.servers.databases.list_databases('fds173')
mock_api.assert_called_with(**expected)
Expand All @@ -22,7 +23,8 @@ def test_list_databases(self, mock_api):
def test_list_databases_with_passwords(self, mock_api):
expected = {
'endpoint': 'client/servers/fds173/databases',
'params': {'include': 'password'},
'includes': ['password'],
'params': None,
}
self.api.client.servers.databases.list_databases('fds173',
include_passwords=True)
Expand Down

0 comments on commit f516c1c

Please sign in to comment.