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 managing websites, and we're about to harness that power with Java. This guide will walk you through creating a robust integration that'll have you manipulating sites, content, and more in no time.
Before we jump in, make sure you've got:
Let's get the boring stuff out of the way:
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.10.0</version> </dependency>
Duda uses API key and secret for authentication. Let's set that up:
public class DudaApiClient { private final String apiKey; private final String apiSecret; private final OkHttpClient client; public DudaApiClient(String apiKey, String apiSecret) { this.apiKey = apiKey; this.apiSecret = apiSecret; this.client = new OkHttpClient(); } private Request.Builder getAuthenticatedBuilder(String url) { return new Request.Builder() .url(url) .addHeader("Authorization", Credentials.basic(apiKey, apiSecret)); } }
Now for the fun part - let's start making some requests:
public String getSite(String siteName) throws IOException { Request request = getAuthenticatedBuilder("https://api.duda.co/api/sites/multiscreen/" + siteName) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); return response.body().string(); } }
Let's add methods for some crucial operations:
public String createSite(String templateId, String siteName) throws IOException { RequestBody body = RequestBody.create( "{\"template_id\":\"" + templateId + "\",\"default_domain_prefix\":\"" + siteName + "\"}", MediaType.get("application/json; charset=utf-8") ); Request request = getAuthenticatedBuilder("https://api.duda.co/api/sites/multiscreen/create") .post(body) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); return response.body().string(); } } public String updateContent(String siteName, String contentJson) throws IOException { RequestBody body = RequestBody.create(contentJson, MediaType.get("application/json; charset=utf-8")); Request request = getAuthenticatedBuilder("https://api.duda.co/api/sites/multiscreen/" + siteName + "/content") .post(body) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); return response.body().string(); } }
Let's add some robust error handling:
private String handleResponse(Response response) throws IOException { if (!response.isSuccessful()) { String errorBody = response.body().string(); throw new DudaApiException("API request failed: " + response.code() + " " + errorBody); } return response.body().string(); } // Use this in your API methods: try (Response response = client.newCall(request).execute()) { return handleResponse(response); }
Remember to:
Don't forget to test! Here's a quick example:
@Test public void testGetSite() { DudaApiClient client = new DudaApiClient("your-api-key", "your-api-secret"); String siteInfo = client.getSite("test-site"); assertNotNull(siteInfo); // Add more assertions based on expected response }
And there you have it! You've just built a solid foundation for integrating with the Duda API. From here, you can expand to cover more endpoints, implement more sophisticated error handling, and build some really cool features.
Remember, the Duda API documentation is your best friend for diving deeper. Now go forth and build something awesome!