still doesn't really work for anything other than get requests

This commit is contained in:
Greg Gauthier 2025-04-13 12:07:45 +01:00
parent e44b82c3a9
commit 96a8838d1d
7 changed files with 116 additions and 4 deletions

View File

@ -3,14 +3,12 @@ from lunduke.client import DiscourseClient
def main(): def main():
client = DiscourseClient() 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: try:
draft_data = { draft_data = {
"title": "Testing 1 2 3",
"raw": "This is only a test", "raw": "This is only a test",
"category": 15
} }
response = client.post('/posts.json', data=draft_data) response = client.post('/posts.json', data=draft_data)

20
examples/get_all_users.py Normal file
View File

@ -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()

View File

@ -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()

18
examples/get_pms.py Normal file
View File

@ -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()

18
examples/get_user.py Normal file
View File

@ -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()

23
examples/update_user.py Normal file
View File

@ -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()

View File

@ -67,3 +67,20 @@ class DiscourseClient:
response = self.session.post(url, headers=headers, json=data) response = self.session.post(url, headers=headers, json=data)
response.raise_for_status() # Raise exception for error status codes response.raise_for_status() # Raise exception for error status codes
return response 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