Back

Step by Step Guide to Building a Setmore Appointments API Integration in Go

Aug 16, 20247 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to dive into the world of appointment scheduling? We're about to embark on a journey to integrate the Setmore Appointments API into your Go project. This powerful API will let you manage appointments, services, and staff with ease. Let's get started!

Prerequisites

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

  • Go installed on your machine (if not, head over to golang.org and get it set up)
  • A Setmore account with API credentials (don't have one? Sign up at setmore.com)

Got those? Great! Let's code.

Setting up the project

First things first, let's create a new Go project:

mkdir setmore-integration cd setmore-integration go mod init setmore-integration

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

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

Authentication

Alright, time to get that access token. Create a new file called main.go and add this:

package main import ( "fmt" "github.com/go-resty/resty/v2" ) const baseURL = "https://developer.setmore.com/api/v1" func getAccessToken(clientID, clientSecret string) (string, error) { client := resty.New() resp, err := client.R(). SetFormData(map[string]string{ "grant_type": "client_credentials", "client_id": clientID, "client_secret": clientSecret, }). Post(baseURL + "/o/oauth2/token") if err != nil { return "", err } // Parse the response and extract the access token // For brevity, we're simplifying error handling return resp.Result().(map[string]interface{})["access_token"].(string), nil }

Basic API requests

Now that we've got authentication sorted, let's make some API calls:

func getAppointments(accessToken string) { client := resty.New() resp, err := client.R(). SetAuthToken(accessToken). Get(baseURL + "/bookings") if err != nil { fmt.Println("Error:", err) return } fmt.Println("Appointments:", resp.String()) } func createAppointment(accessToken string) { client := resty.New() resp, err := client.R(). SetAuthToken(accessToken). SetBody(map[string]interface{}{ "service_key": "your_service_key", "staff_key": "your_staff_key", "start_time": "2023-05-01T10:00:00Z", // Add other required fields }). Post(baseURL + "/bookings") if err != nil { fmt.Println("Error:", err) return } fmt.Println("Appointment created:", resp.String()) }

Handling responses

Let's parse those JSON responses and handle errors like pros:

import "encoding/json" type Appointment struct { Key string `json:"key"` StartTime string `json:"start_time"` // Add other fields as needed } func parseAppointments(jsonData []byte) ([]Appointment, error) { var appointments []Appointment err := json.Unmarshal(jsonData, &appointments) return appointments, err } // Use it like this: appointments, err := parseAppointments(resp.Body()) if err != nil { fmt.Println("Error parsing appointments:", err) return }

Implementing key features

Now, let's implement some core functionality:

func listAvailableTimeSlots(accessToken, serviceKey, staffKey, date string) { // Implement API call to fetch available time slots } func bookAppointment(accessToken, serviceKey, staffKey, startTime string) { // Implement API call to book an appointment } func cancelAppointment(accessToken, appointmentKey string) { // Implement API call to cancel an appointment }

Optimizing the integration

To keep things smooth, let's add some rate limiting and caching:

import ( "time" "golang.org/x/time/rate" ) var limiter = rate.NewLimiter(rate.Every(time.Second), 5) // 5 requests per second func rateLimitedRequest(client *resty.Client, req *resty.Request) (*resty.Response, error) { if err := limiter.Wait(context.Background()); err != nil { return nil, err } return req.Send() } // Simple in-memory cache var cache = make(map[string]interface{}) func getCachedData(key string) (interface{}, bool) { value, ok := cache[key] return value, ok } func setCachedData(key string, value interface{}) { cache[key] = value }

Testing

Don't forget to test your integration! Here's a simple example:

func TestGetAppointments(t *testing.T) { accessToken, err := getAccessToken("your_client_id", "your_client_secret") if err != nil { t.Fatal(err) } appointments, err := getAppointments(accessToken) if err != nil { t.Fatal(err) } if len(appointments) == 0 { t.Error("Expected appointments, got none") } }

Conclusion

And there you have it! You've just built a solid Setmore Appointments API integration in Go. You've learned how to authenticate, make API calls, handle responses, and even optimize your integration with rate limiting and caching.

Remember, this is just the beginning. You can expand on this foundation to create a full-fledged appointment scheduling system. Why not add a slick web interface or integrate with other services?

Keep coding, keep learning, and most importantly, have fun with it!

Resources

Now go forth and schedule those appointments like a boss! 💪🚀