import requests from requests import Response from config.configuration import Config from lunduke.config import DiscourseConfig from lunduke.auth import DiscourseAuth class DiscourseClient: """Main client for interacting with the Discourse API.""" def __init__(self, config_file="config.toml"): """ Initialize with just a config file path. Args: config_file: Path to configuration file (defaults to config.toml) """ # Load all configuration from a single file config_data = Config(config_file) # Create config and auth internally self.config = DiscourseConfig( host=config_data.host, username=config_data.username ) self.auth = DiscourseAuth( api_key_file=config_data.api_key_file, username=config_data.username ) self.session = requests.Session() def get(self, endpoint, params=None) -> Response: """ Make a GET request to the API. Args: endpoint: API endpoint (e.g., '/posts.json') params: Optional query parameters Returns: Response data as JSON """ url = f"{self.config.get_base_url()}{endpoint}" headers = self.auth.get_headers() response = self.session.get(url, headers=headers, params=params) response.raise_for_status() # Raise exception for error status codes return response def post(self, endpoint, data=None) -> Response: """ Make a POST request to the API. Args: endpoint: API endpoint (e.g., '/posts.json') data: Data to be sent in the request body Returns: Response data as JSON """ url = f"{self.config.get_base_url()}{endpoint}" headers = self.auth.get_headers() response = self.session.post(url, headers=headers, json=data) response.raise_for_status() # Raise exception for error status codes return response def put(self, endpoint, data=None) -> Response: """ Make a PUT request to the API. Args: endpoint: API endpoint (e.g., '/posts.json') data: Data to be sent in the request body Returns: Response data as JSON """ url = f"{self.config.get_base_url()}{endpoint}" headers = self.auth.get_headers() response = self.session.post(url, headers=headers, json=data) response.raise_for_status() # Raise exception for error status codes return response