lunduke-cli/lunduke/client.py

59 lines
1.7 KiB
Python

import json
import requests
from lunduke.config import DiscourseConfig
from lunduke.auth import DiscourseAuth
class DiscourseClient:
"""Main client for interacting with the Discourse API."""
def __init__(self, config, auth):
"""
Initialize with configuration and authentication.
Args:
config: DiscourseConfig instance
auth: DiscourseAuth instance
"""
self.config = config
self.auth = auth
self.session = requests.Session()
def get(self, endpoint, params=None) -> dict:
"""
Make a GET request to the API.
Args:
endpoint: API endpoint (e.g., '/posts.json')
params: Optional query parameters
Returns:
Response data as JSON
"""
url = f"{self.config.get_base_url()}{endpoint}"
headers = self.auth.get_headers()
response = self.session.get(url, headers=headers, params=params)
response.raise_for_status() # Raise exception for error status codes
return response.json()
# 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()