import requests

# Define connection details
server_url = 'http://localhost:8096'
username = 'admin'
password = 'Optimus0329'

backup_options = {
    "Database": True,      # Include database
    "Config": True,        # Include config files
    "Data": True,          # Include data files
    "Root": True,          # Include root folder
    "MediaFiles": False    # Exclude media files
}

# Build json payload with auth data
auth_data = {
    'username': username,
    'Pw': password
}

headers = {}

# Build required connection headers
authorization = 'MediaBrowser Client="other", Device="my-script", DeviceId="some-unique-id", Version="0.0.0"'

headers['Authorization'] = authorization

# Authenticate to server
r = requests.post(server_url + '/Users/AuthenticateByName', headers=headers, json=auth_data)

# Retrieve auth token and user id from returned data
token = r.json().get('AccessToken')
user_id = r.json().get('User').get('Id')

# Update the headers to include the auth token
headers['Authorization'] = f'{authorization}, Token="{token}"'
headers['Content-Type'] = 'application/json'

# Requests can be made with
#requests.get(f'{server_url}/api/endpoint', headers=headers)

# Create a backup (empty body is OK, will use default options)
print("Creating backup...")
response = requests.post(f'{server_url}/Backup/Create', headers=headers, json={})
print(f"Status: {response.status_code}")
print(response.text)

# List all backups
print("\nListing backups...")
response = requests.get(f'{server_url}/Backup', headers=headers)
print(f"Status: {response.status_code}")
print(response.text)
