diff --git a/examples/draft_post.py b/examples/draft_post.py new file mode 100644 index 0000000..0a295b1 --- /dev/null +++ b/examples/draft_post.py @@ -0,0 +1,47 @@ +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 + 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}") + + except Exception as e: + print(f"Error creating draft: {e}") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/example.py b/examples/example.py index 017ed6c..662197c 100644 --- a/examples/example.py +++ b/examples/example.py @@ -25,7 +25,8 @@ def main(): posts = response['latest_posts'] print(f"Found {len(posts)} posts") for post in posts: - print(post['post_type'], post['username'], post['topic_id']) + # print(post) + print(post['username'], post['topic_title'], post['excerpt']) except Exception as e: print(f"Error: {e}") diff --git a/lunduke/client.py b/lunduke/client.py index 7d53e9a..8c03000 100644 --- a/lunduke/client.py +++ b/lunduke/client.py @@ -38,4 +38,21 @@ class DiscourseClient: response.raise_for_status() # Raise exception for error status codes return response.json() - # Add other HTTP methods as needed (post, put, delete) \ No newline at end of file + # Add other HTTP methods as needed (post, put, delete) + def post(self, endpoint, data=None) -> dict: + """ + 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.json()