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 Acuity Scheduling API into your Go project. This powerful API will let you manage appointments like a pro, and we'll make it happen with some slick Go code.
Before we jump in, make sure you've got:
Let's kick things off by creating a new Go project:
mkdir acuity-integration cd acuity-integration go mod init acuity-integration
Now, let's grab the HTTP client we'll need:
go get github.com/go-resty/resty/v2
Acuity uses API key authentication. Let's set that up:
package main import ( "github.com/go-resty/resty/v2" ) const ( baseURL = "https://acuityscheduling.com/api/v1" userID = "YOUR_USER_ID" apiKey = "YOUR_API_KEY" ) func main() { client := resty.New(). SetBaseURL(baseURL). SetBasicAuth(userID, apiKey) // We'll use this client for all our requests }
Now that we've got our authenticated client, let's make some requests!
func getAppointmentTypes(client *resty.Client) ([]AppointmentType, error) { var types []AppointmentType _, err := client.R(). SetResult(&types). Get("/appointment-types") return types, err }
Let's implement some key features:
func getAvailability(client *resty.Client, appointmentTypeID int) ([]TimeSlot, error) { var slots []TimeSlot _, err := client.R(). SetQueryParam("appointmentTypeID", strconv.Itoa(appointmentTypeID)). SetResult(&slots). Get("/availability/times") return slots, err } func createAppointment(client *resty.Client, appointment Appointment) (CreatedAppointment, error) { var created CreatedAppointment _, err := client.R(). SetBody(appointment). SetResult(&created). Post("/appointments") return created, err }
Don't forget to handle those pesky errors:
if err != nil { if restErr, ok := err.(*resty.ResponseError); ok { fmt.Printf("API error: %v\n", restErr.Response.Status()) // Handle specific error codes here } else { fmt.Printf("Request error: %v\n", err) } return }
Let's make it interactive:
func main() { // ... client setup ... types, err := getAppointmentTypes(client) if err != nil { log.Fatal(err) } fmt.Println("Available appointment types:") for i, t := range types { fmt.Printf("%d. %s\n", i+1, t.Name) } // ... user input and appointment creation ... }
Don't forget to test your code! Here's a quick example:
func TestGetAppointmentTypes(t *testing.T) { client := setupTestClient() types, err := getAppointmentTypes(client) assert.NoError(t, err) assert.NotEmpty(t, types) }
Remember to respect rate limits and consider caching frequently accessed data to optimize your integration.
And there you have it! You've just built a solid Acuity Scheduling API integration in Go. From here, you can expand on this foundation to create more complex scheduling applications. Keep coding, and happy scheduling!