34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
class DiscourseAuth:
|
|
"""Handles authentication with the Discourse API."""
|
|
|
|
def __init__(self, api_key=None, api_key_file=None, username=None):
|
|
"""
|
|
Initialize with API key details.
|
|
|
|
Args:
|
|
api_key: The API key as a string
|
|
api_key_file: Path to file containing the API key
|
|
username: The API username
|
|
"""
|
|
self.username = username
|
|
self.api_key = None
|
|
|
|
if api_key:
|
|
self.api_key = api_key
|
|
elif api_key_file:
|
|
self._load_key_from_file(api_key_file)
|
|
|
|
def _load_key_from_file(self, key_file):
|
|
"""Load API key from file and clean it."""
|
|
with open(key_file, 'r') as f:
|
|
self.api_key = f.read().strip().replace('\n', '').replace('\r', '')
|
|
|
|
def get_headers(self):
|
|
"""Return headers for API requests."""
|
|
if not self.api_key or not self.username:
|
|
raise ValueError("API key and username must be set")
|
|
|
|
return {
|
|
"api_key": self.api_key,
|
|
"api_username": self.username
|
|
} |