finally got the thing working
This commit is contained in:
		
							parent
							
								
									bb0474fe07
								
							
						
					
					
						commit
						4ed17de9ae
					
				
							
								
								
									
										47
									
								
								examples/draft_post.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								examples/draft_post.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					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
 | 
				
			||||||
 | 
					    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}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    except Exception as e:
 | 
				
			||||||
 | 
					        print(f"Error creating draft: {e}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == "__main__":
 | 
				
			||||||
 | 
					    main()
 | 
				
			||||||
@ -25,7 +25,8 @@ def main():
 | 
				
			|||||||
        posts = response['latest_posts']
 | 
					        posts = response['latest_posts']
 | 
				
			||||||
        print(f"Found {len(posts)} posts")
 | 
					        print(f"Found {len(posts)} posts")
 | 
				
			||||||
        for post in posts:
 | 
					        for post in posts:
 | 
				
			||||||
            print(post['post_type'], post['username'], post['topic_id'])
 | 
					            # print(post)
 | 
				
			||||||
 | 
					            print(post['username'], post['topic_title'], post['excerpt'])
 | 
				
			||||||
    except Exception as e:
 | 
					    except Exception as e:
 | 
				
			||||||
        print(f"Error: {e}")
 | 
					        print(f"Error: {e}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -38,4 +38,21 @@ class DiscourseClient:
 | 
				
			|||||||
        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.json()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Add other HTTP methods as needed (post, put, delete)
 | 
					    # 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()
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user