radically simplify DiscourseClient() signature

This commit is contained in:
Greg Gauthier 2025-04-12 22:40:53 +01:00
parent e21703965f
commit b9e6374ba0
4 changed files with 30 additions and 73 deletions

View File

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

View File

@ -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}")

View File

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

View File

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