Skip to content

Commit

Permalink
adding support for login with usr/pwd
Browse files Browse the repository at this point in the history
  • Loading branch information
bigoulours committed May 20, 2023
1 parent e46b0f2 commit ce7bd23
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
34 changes: 18 additions & 16 deletions slskd_api/apis/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,26 @@ def state(self) -> dict:
return response.json()


# Getting Error 'Unauthorized' even with admin API-Key:
# def stop(self):
# """
# Stops the application.
# """
# url = self.api_url + '/application'
# response = requests.delete(url, headers=self.header)
# return response.ok
def stop(self) -> bool:
"""
Stops the application. Only works with token (usr/pwd login). 'Unauthorized' with API-Key.
:return: True if successful.
"""
url = self.api_url + '/application'
response = requests.delete(url, headers=self.header)
return response.ok


# Getting Error 'Unauthorized' even with admin API-Key:
# def restart(self):
# """
# Restarts the application.
# """
# url = self.api_url + '/application'
# response = requests.put(url, headers=self.header)
# return response.json()
def restart(self) -> bool:
"""
Restarts the application. Only works with token (usr/pwd login). 'Unauthorized' with API-Key.
:return: True if successful.
"""
url = self.api_url + '/application'
response = requests.put(url, headers=self.header)
return response.ok


def version(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion slskd_api/apis/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_startup(self) -> dict:
return response.json()


# Getting error 'Unauthorized':
# Getting error 'Unauthorized'(API-Key) or 'Forbidden' (token):
# def get_debug(self) -> dict:
# """
# Gets the debug view of the current application options.
Expand Down
22 changes: 16 additions & 6 deletions slskd_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,31 @@ class SlskdClient:
"""
The main class that allows access to the different APIs of a slskd instance.
An API-Key with appropriate permissions (`readwrite` for most use cases) must be set in slskd config file.
Alternatively, provide your username and password.
Usage::
slskd = slskd_api.SlskdClient(host, api_key, url_base)
app_status = slskd.application.state()
"""

def __init__(self,
host: str,
api_key: str,
url_base: str = '/'
api_key: str = None,
url_base: str = '/',
username: str = None,
password: str = None,
):
api_url = reduce(urljoin, [host, f'{url_base}/', f'api/{API_VERSION}'])
header = {
'accept': '*/*',
'X-API-Key': api_key
}

header = {'accept': '*/*'}

if api_key:
header['X-API-Key'] = api_key
elif username and password:
header['Authorization'] = 'Bearer ' + \
SessionApi(api_url, header).login(username, password)['token']
else:
raise ValueError('Please provide an API-Key or a valid username/password pair.')

base_args = (api_url, header)

self.application = ApplicationApi(*base_args)
Expand Down

0 comments on commit ce7bd23

Please sign in to comment.