Back

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

Aug 12, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of OnceHub API integration with Go? You're in for a treat. OnceHub's API is a powerful tool for managing scheduling and bookings, and when combined with Go's efficiency, you've got a recipe for success. Let's get cracking!

Prerequisites

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

  • Go installed on your machine (you're a pro, so I'm sure you do)
  • A OnceHub account with API credentials (if not, hop over to OnceHub and set one up)

Setting up the project

Let's kick things off by setting up our Go project:

mkdir oncehub-integration cd oncehub-integration go mod init oncehub-integration

Now, let's grab the HTTP client we'll need:

go get github.com/go-resty/resty/v2

Authentication

First things first, let's authenticate. OnceHub uses API keys, so let's implement that:

package main import ( "github.com/go-resty/resty/v2" ) const ( baseURL = "https://api.oncehub.com/v2" apiKey = "your-api-key-here" ) func main() { client := resty.New(). SetBaseURL(baseURL). SetHeader("Authorization", "Bearer "+apiKey) // We'll use this client for all our requests }

Making API requests

Now that we're authenticated, let's make some requests! Here's a quick example of a GET request:

resp, err := client.R(). SetResult(&YourStructHere{}). Get("/endpoint") if err != nil { // Handle error } // Use resp.Result().(*YourStructHere) to access the response

And here's a POST request:

resp, err := client.R(). SetBody(map[string]interface{}{ "key": "value", }). Post("/endpoint") if err != nil { // Handle error }

Implementing key OnceHub features

Let's implement some core features. Here's how you might fetch available time slots:

func getAvailableSlots(date string) ([]TimeSlot, error) { var result struct { TimeSlots []TimeSlot `json:"time_slots"` } _, err := client.R(). SetQueryParam("date", date). SetResult(&result). Get("/booking/slots") return result.TimeSlots, err }

Creating a booking might look something like this:

func createBooking(slot TimeSlot, customerInfo CustomerInfo) error { _, err := client.R(). SetBody(map[string]interface{}{ "slot_id": slot.ID, "customer_info": customerInfo, }). Post("/bookings") return err }

Error handling and logging

Don't forget to implement robust error handling and logging. Here's a quick example:

import ( "log" ) func makeRequest() { resp, err := client.R().Get("/endpoint") if err != nil { log.Printf("Error making request: %v", err) return } if resp.IsError() { log.Printf("API error: %s", resp.String()) return } // Process successful response }

Testing the integration

Testing is crucial. Here's a simple unit test example:

func TestGetAvailableSlots(t *testing.T) { slots, err := getAvailableSlots("2023-06-01") if err != nil { t.Fatalf("Error getting slots: %v", err) } if len(slots) == 0 { t.Error("Expected slots, got none") } }

Best practices and optimization

Remember to implement rate limiting to respect API constraints:

client.SetRetryCount(3). SetRetryWaitTime(5 * time.Second). SetRetryMaxWaitTime(20 * time.Second)

And consider caching responses to reduce API calls:

var cache = make(map[string][]TimeSlot) func getCachedSlots(date string) ([]TimeSlot, error) { if slots, ok := cache[date]; ok { return slots, nil } slots, err := getAvailableSlots(date) if err != nil { return nil, err } cache[date] = slots return slots, nil }

Conclusion

And there you have it! You've just built a solid OnceHub API integration in Go. Remember, this is just the beginning. There's always room to expand and optimize your integration. Keep exploring the OnceHub API docs for more features you can implement.

Happy coding, and may your bookings always be on time! 🚀