radically simplify DiscourseClient() signature
This commit is contained in:
parent
e21703965f
commit
b9e6374ba0
@ -1,17 +1,8 @@
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
from config.configuration import Config
|
from lunduke.client import DiscourseClient
|
||||||
|
|
||||||
cfg = Config()
|
client = DiscourseClient()
|
||||||
|
response = client.get('/site.json')
|
||||||
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)
|
|
||||||
print(response.status_code)
|
print(response.status_code)
|
||||||
print(response.json())
|
print(response.json())
|
||||||
|
@ -1,41 +1,15 @@
|
|||||||
from lunduke.config import DiscourseConfig
|
|
||||||
from lunduke.auth import DiscourseAuth
|
|
||||||
from lunduke.client import DiscourseClient
|
from lunduke.client import DiscourseClient
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Set up configuration
|
|
||||||
config = DiscourseConfig(
|
|
||||||
host='forum.lunduke.com',
|
|
||||||
username='gmgauthier'
|
|
||||||
)
|
|
||||||
|
|
||||||
# Set up authentication
|
client = DiscourseClient()
|
||||||
auth = DiscourseAuth(
|
|
||||||
api_key_file='../keys/cli_key.txt',
|
|
||||||
username=config.username
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create client
|
|
||||||
client = DiscourseClient(config, auth)
|
|
||||||
|
|
||||||
# Create a draft post
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
draft_data = {
|
draft_data = {
|
||||||
"title": "Simple forum post",
|
"title": "Simple forum post",
|
||||||
"raw": "Simple forum post content"
|
"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)
|
response = client.post('/posts.json', data=draft_data)
|
||||||
print(f"Draft created successfully: {response}")
|
print(f"Draft created successfully: {response}")
|
||||||
|
|
||||||
|
@ -1,31 +1,12 @@
|
|||||||
from config.configuration import Config
|
|
||||||
from lunduke.config import DiscourseConfig
|
|
||||||
from lunduke.auth import DiscourseAuth
|
|
||||||
from lunduke.client import DiscourseClient
|
from lunduke.client import DiscourseClient
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config_data = Config()
|
|
||||||
|
|
||||||
# Set up configuration
|
client = DiscourseClient()
|
||||||
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
|
|
||||||
try:
|
try:
|
||||||
response = client.get('/posts.json')
|
response = client.get('/posts.json')
|
||||||
posts = response['latest_posts']
|
posts = response.json()['latest_posts']
|
||||||
print(f"Found {len(posts)} posts")
|
print(f"Found {len(posts)} posts")
|
||||||
for post in posts:
|
for post in posts:
|
||||||
# print(post)
|
# print(post)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from requests import Response
|
||||||
|
|
||||||
|
from config.configuration import Config
|
||||||
from lunduke.config import DiscourseConfig
|
from lunduke.config import DiscourseConfig
|
||||||
from lunduke.auth import DiscourseAuth
|
from lunduke.auth import DiscourseAuth
|
||||||
|
|
||||||
@ -8,19 +9,30 @@ from lunduke.auth import DiscourseAuth
|
|||||||
class DiscourseClient:
|
class DiscourseClient:
|
||||||
"""Main client for interacting with the Discourse API."""
|
"""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:
|
Args:
|
||||||
config: DiscourseConfig instance
|
config_file: Path to configuration file (defaults to config.toml)
|
||||||
auth: DiscourseAuth instance
|
|
||||||
"""
|
"""
|
||||||
self.config = config
|
# Load all configuration from a single file
|
||||||
self.auth = auth
|
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()
|
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.
|
Make a GET request to the API.
|
||||||
|
|
||||||
@ -36,10 +48,9 @@ class DiscourseClient:
|
|||||||
|
|
||||||
response = self.session.get(url, headers=headers, params=params)
|
response = self.session.get(url, headers=headers, params=params)
|
||||||
response.raise_for_status() # Raise exception for error status codes
|
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) -> Response:
|
||||||
def post(self, endpoint, data=None) -> dict:
|
|
||||||
"""
|
"""
|
||||||
Make a POST request to the API.
|
Make a POST request to the API.
|
||||||
|
|
||||||
@ -55,4 +66,4 @@ 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.json()
|
return response
|
||||||
|
Loading…
Reference in New Issue
Block a user