From 96a8838d1da13bdef01c69ac3bde15fe06fe168e Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Sun, 13 Apr 2025 12:07:45 +0100 Subject: [PATCH] still doesn't really work for anything other than get requests --- examples/draft_post.py | 6 ++---- examples/get_all_users.py | 20 ++++++++++++++++++++ examples/get_categories.py | 18 ++++++++++++++++++ examples/get_pms.py | 18 ++++++++++++++++++ examples/get_user.py | 18 ++++++++++++++++++ examples/update_user.py | 23 +++++++++++++++++++++++ lunduke/client.py | 17 +++++++++++++++++ 7 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 examples/get_all_users.py create mode 100644 examples/get_categories.py create mode 100644 examples/get_pms.py create mode 100644 examples/get_user.py create mode 100644 examples/update_user.py diff --git a/examples/draft_post.py b/examples/draft_post.py index 337dec4..9a45637 100644 --- a/examples/draft_post.py +++ b/examples/draft_post.py @@ -3,14 +3,12 @@ from lunduke.client import DiscourseClient def main(): client = DiscourseClient() - categories = client.get('/categories.json').json()['category_list'] - print(categories) + # https://docs.discourse.org/#tag/Topics/operation/createTopicPostPM + # TODO: FAILS. 403. Forbidden for url: https://forum.lunduke.com/posts.json try: draft_data = { - "title": "Testing 1 2 3", "raw": "This is only a test", - "category": 15 } response = client.post('/posts.json', data=draft_data) diff --git a/examples/get_all_users.py b/examples/get_all_users.py new file mode 100644 index 0000000..d32508e --- /dev/null +++ b/examples/get_all_users.py @@ -0,0 +1,20 @@ +from lunduke.client import DiscourseClient + + +def main(): + client = DiscourseClient() + param_data = { + "period": "all", + "order": "likes_received" + } + try: + response = client.get('/directory_items.json', params=param_data) + print(response.status_code) + print(response.json()) + except Exception as e: + print(f"Error: {e}") + + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/get_categories.py b/examples/get_categories.py new file mode 100644 index 0000000..a3bde06 --- /dev/null +++ b/examples/get_categories.py @@ -0,0 +1,18 @@ +from lunduke.client import DiscourseClient + + +def main(): + + client = DiscourseClient() + try: + response = client.get('/categories.json') + categories = response.json()['category_list']['categories'] + for cat_dict in categories: + for key,val in cat_dict.items(): + print(key, ": ", val) + except Exception as e: + print(f"Error: {e}") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/get_pms.py b/examples/get_pms.py new file mode 100644 index 0000000..44410bf --- /dev/null +++ b/examples/get_pms.py @@ -0,0 +1,18 @@ +from config.configuration import Config +from lunduke.client import DiscourseClient + + +def main(): + cfg = Config() + client = DiscourseClient() + try: + response = client.get(f"/topics/private-messages/{cfg.username}.json") + print(response.status_code) + print(response.json()) + except Exception as e: + print(f"Error: {e}") + + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/get_user.py b/examples/get_user.py new file mode 100644 index 0000000..0b61a75 --- /dev/null +++ b/examples/get_user.py @@ -0,0 +1,18 @@ +from config.configuration import Config +from lunduke.client import DiscourseClient + + +def main(): + user = Config().username + client = DiscourseClient() + try: + response = client.get(f"/u/{user}.json") + print(response.status_code) + print(response.json()) + except Exception as e: + print(f"Error: {e}") + + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/update_user.py b/examples/update_user.py new file mode 100644 index 0000000..d26786c --- /dev/null +++ b/examples/update_user.py @@ -0,0 +1,23 @@ +from config.configuration import Config +from lunduke.client import DiscourseClient + + +def main(): + user = Config().username + client = DiscourseClient() + # https://docs.discourse.org/#tag/Users/operation/updateUser + # TODO: FAILS: 404 + put_data = { + "location": "cyberspace", + "external_ids": {} + } + try: + response = client.put(f"/u/{user}.json", data=put_data) + print(response.status_code) + print(response.json()) + except Exception as e: + print(f"Error: {e}") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/lunduke/client.py b/lunduke/client.py index 2f5a820..68379c6 100644 --- a/lunduke/client.py +++ b/lunduke/client.py @@ -67,3 +67,20 @@ class DiscourseClient: 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