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!
Before we jump in, make sure you've got:
Got those? Great! Let's code.
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
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 }
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()) }
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 }
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 }
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 }
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") } }
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!
Now go forth and schedule those appointments like a boss! 💪🚀