41 lines
1.1 KiB
Python
41 lines
1.1 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)
|