Back

Step by Step Guide to Building a OnceHub API Integration in Java

Aug 12, 20245 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your scheduling game with OnceHub? Let's dive into building a slick Java integration that'll have you managing bookings like a pro. We'll keep things snappy and to the point, so you can get up and running in no time.

Prerequisites

Before we jump in, make sure you've got:

  • Your favorite Java IDE fired up
  • A OnceHub account with API credentials in hand
  • An HTTP client library (we'll use OkHttp, but feel free to use your preferred option)

Setting up the project

First things first, let's get our project set up:

  1. Create a new Java project in your IDE
  2. Add the OkHttp dependency to your pom.xml or build.gradle:
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.10.0</version> </dependency>

Authentication

OnceHub uses API keys for authentication. Here's how to implement it:

String apiKey = "your_api_key_here"; OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(chain -> { Request original = chain.request(); Request request = original.newBuilder() .header("Authorization", "Bearer " + apiKey) .build(); return chain.proceed(request); }) .build();

Making API requests

Now, let's create a helper method for API calls:

private String makeApiCall(String endpoint, String method, String body) throws IOException { Request.Builder requestBuilder = new Request.Builder() .url("https://api.oncehub.com/v2" + endpoint) .method(method, body != null ? RequestBody.create(body, MediaType.parse("application/json")) : null); Response response = client.newCall(requestBuilder.build()).execute(); return response.body().string(); }

Implementing key OnceHub features

Let's tackle some core features:

Retrieving booking pages

String bookingPages = makeApiCall("/booking_pages", "GET", null); System.out.println(bookingPages);

Creating a booking

String bookingData = "{\"booking_page_id\": \"123\", \"date\": \"2023-05-01\", \"time\": \"09:00\"}"; String newBooking = makeApiCall("/bookings", "POST", bookingData); System.out.println(newBooking);

Handling webhooks

For real-time updates, set up a simple HTTP server to receive webhook events:

HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); server.createContext("/webhook", exchange -> { String requestBody = new String(exchange.getRequestBody().readAllBytes()); // Process the webhook payload System.out.println("Received webhook: " + requestBody); exchange.sendResponseHeaders(200, -1); }); server.start();

Error handling and logging

Don't forget to wrap your API calls in try-catch blocks and log responses:

try { String response = makeApiCall("/booking_pages", "GET", null); logger.info("API response: " + response); } catch (IOException e) { logger.error("API call failed", e); }

Testing the integration

Write some unit tests to ensure your integration is solid:

@Test public void testGetBookingPages() { // Mock the HTTP client and test the getBookingPages method }

Best practices and optimization

  • Implement rate limiting to avoid hitting API limits
  • Cache frequently accessed data to reduce API calls
  • Use connection pooling for better performance

Conclusion

And there you have it! You've just built a robust OnceHub API integration in Java. Remember to check out the OnceHub API documentation for more details on available endpoints and features.

Now go forth and schedule like a boss! Happy coding! 🚀