Hey there, fellow developer! Ready to supercharge your project management with Wrike? Let's dive into building a Java integration with the Wrike API. This powerful tool will let you automate tasks, sync data, and take your workflow to the next level.
Before we jump in, make sure you've got:
First things first, let's get our project ready:
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.10.0</version> </dependency>
Wrike uses OAuth 2.0 for authentication. Here's a quick way to get your access token:
OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://www.wrike.com/oauth2/token") .post(new FormBody.Builder() .add("client_id", "your_client_id") .add("client_secret", "your_client_secret") .add("grant_type", "authorization_code") .add("code", "your_authorization_code") .build()) .build(); Response response = client.newCall(request).execute(); String accessToken = // Parse the access token from the response
Now that we're authenticated, let's make a simple GET request:
Request request = new Request.Builder() .url("https://www.wrike.com/api/v4/tasks") .addHeader("Authorization", "Bearer " + accessToken) .build(); Response response = client.newCall(request).execute(); String responseBody = response.body().string(); // Parse the JSON response
Let's run through the basic CRUD operations:
String json = "{\"title\":\"New Task\",\"description\":\"Task description\"}"; RequestBody body = RequestBody.create(json, MediaType.parse("application/json")); Request request = new Request.Builder() .url("https://www.wrike.com/api/v4/folders/XXXXXXXX/tasks") .post(body) .addHeader("Authorization", "Bearer " + accessToken) .build(); Response response = client.newCall(request).execute();
Request request = new Request.Builder() .url("https://www.wrike.com/api/v4/tasks/XXXXXXXX") .addHeader("Authorization", "Bearer " + accessToken) .build(); Response response = client.newCall(request).execute();
String json = "{\"title\":\"Updated Task Title\"}"; RequestBody body = RequestBody.create(json, MediaType.parse("application/json")); Request request = new Request.Builder() .url("https://www.wrike.com/api/v4/tasks/XXXXXXXX") .put(body) .addHeader("Authorization", "Bearer " + accessToken) .build(); Response response = client.newCall(request).execute();
Request request = new Request.Builder() .url("https://www.wrike.com/api/v4/tasks/XXXXXXXX") .delete() .addHeader("Authorization", "Bearer " + accessToken) .build(); Response response = client.newCall(request).execute();
Always check the response status and handle rate limits:
if (response.code() == 429) { // Handle rate limit, maybe wait and retry } else if (!response.isSuccessful()) { // Handle other errors }
Wrike supports webhooks for real-time updates. Here's how to set one up:
String json = "{\"hookUrl\":\"https://your-webhook-url.com\",\"events\":[\"taskCreated\",\"taskUpdated\"]}"; RequestBody body = RequestBody.create(json, MediaType.parse("application/json")); Request request = new Request.Builder() .url("https://www.wrike.com/api/v4/webhooks") .post(body) .addHeader("Authorization", "Bearer " + accessToken) .build(); Response response = client.newCall(request).execute();
Don't forget to test! Here's a simple unit test example:
@Test public void testCreateTask() { // Your test code here assertEquals(201, response.code()); }
And there you have it! You're now equipped to build a robust Wrike API integration in Java. Remember, this is just the tip of the iceberg. The Wrike API offers a ton more features to explore.
Keep coding, keep learning, and most importantly, have fun building awesome integrations!
For more details, check out the Wrike API documentation. Happy coding!