diff --git a/examples/check-key.py b/examples/check-key.py index a2af801..a53dc9c 100644 --- a/examples/check-key.py +++ b/examples/check-key.py @@ -1,17 +1,8 @@ import requests -from config.configuration import Config +from lunduke.client import DiscourseClient -cfg = Config() - -username = cfg.username -url = cfg.get_api_url(endpoint='/site.json') -api_key = cfg.api_key - -headers={ - "api_key": api_key, - "api_username": username -} -response = requests.get(url,headers=headers) +client = DiscourseClient() +response = client.get('/site.json') print(response.status_code) print(response.json()) diff --git a/examples/draft_post.py b/examples/draft_post.py index 0a295b1..c3a0966 100644 --- a/examples/draft_post.py +++ b/examples/draft_post.py @@ -1,41 +1,15 @@ -from lunduke.config import DiscourseConfig -from lunduke.auth import DiscourseAuth from lunduke.client import DiscourseClient def main(): - # Set up configuration - config = DiscourseConfig( - host='forum.lunduke.com', - username='gmgauthier' - ) - # Set up authentication - auth = DiscourseAuth( - api_key_file='../keys/cli_key.txt', - username=config.username - ) - - # Create client - client = DiscourseClient(config, auth) - - # Create a draft post + client = DiscourseClient() try: - draft_data = { "title": "Simple forum post", "raw": "Simple forum post content" } - # draft_data = { - # "draft_key": "new_topic", - # "data": { - # "title": "Test Draft Topic from API", - # "raw": "This is the content of my draft post created via the API.", - # "category_id": 1 # Replace with your desired category ID - # } - # } - response = client.post('/posts.json', data=draft_data) print(f"Draft created successfully: {response}") diff --git a/examples/example.py b/examples/example.py index fdc75f1..f4ce5dc 100644 --- a/examples/example.py +++ b/examples/example.py @@ -1,31 +1,12 @@ -from config.configuration import Config -from lunduke.config import DiscourseConfig -from lunduke.auth import DiscourseAuth from lunduke.client import DiscourseClient def main(): - config_data = Config() - # Set up configuration - config = DiscourseConfig( - host=config_data.host, - username=config_data.username - ) - - # Set up authentication - auth = DiscourseAuth( - api_key_file=config_data.api_key_file, - username=config_data.username - ) - - # Create client - client = DiscourseClient(config, auth) - - # Use the client + client = DiscourseClient() try: response = client.get('/posts.json') - posts = response['latest_posts'] + posts = response.json()['latest_posts'] print(f"Found {len(posts)} posts") for post in posts: # print(post) diff --git a/lunduke/client.py b/lunduke/client.py index 8c03000..2f5a820 100644 --- a/lunduke/client.py +++ b/lunduke/client.py @@ -1,6 +1,7 @@ -import json - import requests +from requests import Response + +from config.configuration import Config from lunduke.config import DiscourseConfig from lunduke.auth import DiscourseAuth @@ -8,19 +9,30 @@ from lunduke.auth import DiscourseAuth class DiscourseClient: """Main client for interacting with the Discourse API.""" - def __init__(self, config, auth): + def __init__(self, config_file="config.toml"): """ - Initialize with configuration and authentication. + Initialize with just a config file path. Args: - config: DiscourseConfig instance - auth: DiscourseAuth instance + config_file: Path to configuration file (defaults to config.toml) """ - self.config = config - self.auth = auth + # 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) -> dict: + def get(self, endpoint, params=None) -> Response: """ Make a GET request to the API. @@ -36,10 +48,9 @@ class DiscourseClient: response = self.session.get(url, headers=headers, params=params) response.raise_for_status() # Raise exception for error status codes - return response.json() + return response - # Add other HTTP methods as needed (post, put, delete) - def post(self, endpoint, data=None) -> dict: + def post(self, endpoint, data=None) -> Response: """ Make a POST request to the API. @@ -55,4 +66,4 @@ class DiscourseClient: response = self.session.post(url, headers=headers, json=data) response.raise_for_status() # Raise exception for error status codes - return response.json() + return response