Hey there, fellow developer! Ready to dive into the world of Duda API integration? You're in for a treat. Duda's API is a powerhouse for website management, and with the dudaclient
package, we're about to make it sing in Python. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get dudaclient
installed:
pip install dudaclient
Easy peasy, right?
Now, let's get you authenticated:
from dudaclient import DudaClient client = DudaClient( user="your_username", pass="your_password", env="api.duda.co" # or your specific endpoint )
Boom! You're in.
Let's start with some basics:
site_data = client.sites.get("site_name") print(site_data)
new_site = client.sites.create( template_id="1234567", default_domain_prefix="my-awesome-site" )
client.content.update("site_name", { "content_library": { "text1": "Hello, Duda!" } })
Ready to level up? Let's tackle some advanced stuff:
collections = client.collections.list("site_name") new_item = client.collections.items.create("site_name", "collection_name", { "field1": "value1", "field2": "value2" })
client.accounts.grant_site_access("site_name", "[email protected]", "EDITOR")
webhook_data = client.webhooks.create("site_name", { "events": ["PUBLISH", "UNPUBLISH"], "url": "https://your-webhook-endpoint.com" })
Always wrap your API calls in try-except blocks:
try: site_data = client.sites.get("non_existent_site") except Exception as e: print(f"Oops! An error occurred: {str(e)}")
And remember, Duda has rate limits. Be nice to their servers!
Let's build a quick site management tool:
def list_all_sites(): return client.sites.list() def create_site(template_id, domain_prefix): return client.sites.create(template_id, domain_prefix) def update_site_content(site_name, content): return client.content.update(site_name, content) # Usage sites = list_all_sites() new_site = create_site("template123", "my-new-site") update_site_content(new_site['site_name'], {"title": "Welcome to my site!"})
For unit testing, consider using unittest
or pytest
. Here's a quick example:
import unittest from unittest.mock import patch class TestDudaIntegration(unittest.TestCase): @patch('dudaclient.DudaClient.sites.get') def test_get_site(self, mock_get): mock_get.return_value = {"site_name": "test_site"} site = client.sites.get("test_site") self.assertEqual(site["site_name"], "test_site")
And there you have it! You're now equipped to build some seriously cool stuff with Duda's API. Remember, the official Duda API docs are your best friend for diving deeper. Now go forth and create some awesome websites!
Happy coding! 🚀