Hey there, fellow Go enthusiast! Ready to dive into the world of appointment scheduling? Today, we're going to walk through building an integration with SimplyBook.me's API. This nifty tool will let you manage bookings, services, and more, all from your Go application. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's set up our project:
mkdir simplybook-integration cd simplybook-integration go mod init simplybook-integration
Now, let's grab the HTTP client we'll be using:
go get -u github.com/go-resty/resty/v2
SimplyBook.me uses OAuth2, so we'll need to get an access token. Here's how:
package main import ( "fmt" "github.com/go-resty/resty/v2" ) func getAccessToken(client *resty.Client, clientID, secret string) (string, error) { resp, err := client.R(). SetFormData(map[string]string{ "grant_type": "client_credentials", "client_id": clientID, "client_secret": secret, }). Post("https://user-api.simplybook.me/token") if err != nil { return "", err } // Parse the response and extract the access token // You'll want to add proper error handling here return resp.Result().(map[string]interface{})["access_token"].(string), nil }
Now that we've got our token, let's make some requests:
func getServices(client *resty.Client, token string) ([]map[string]interface{}, error) { resp, err := client.R(). SetAuthToken(token). Get("https://user-api.simplybook.me/admin/services") if err != nil { return nil, err } return resp.Result().([]map[string]interface{}), nil }
Let's implement a function to create a booking:
func createBooking(client *resty.Client, token string, serviceID, clientName, startDateTime string) error { _, err := client.R(). SetAuthToken(token). SetBody(map[string]interface{}{ "service_id": serviceID, "client": map[string]string{"name": clientName}, "datetime": startDateTime, }). Post("https://user-api.simplybook.me/admin/bookings") return err }
Don't forget to add proper error handling and logging. Here's a quick example:
import "log" // In your functions if err != nil { log.Printf("Error occurred: %v", err) return nil, fmt.Errorf("failed to get services: %w", err) }
Always test your code! Here's a simple test for our getServices
function:
func TestGetServices(t *testing.T) { client := resty.New() token := "your-test-token" services, err := getServices(client, token) if err != nil { t.Fatalf("Error getting services: %v", err) } if len(services) == 0 { t.Error("No services returned") } }
Remember to implement rate limiting to avoid hitting API limits. You can use a package like golang.org/x/time/rate
for this.
For caching, consider using an in-memory store like github.com/patrickmn/go-cache
for frequently accessed data.
And there you have it! You've just built a SimplyBook.me API integration in Go. Pretty cool, right? From here, you can expand on this foundation to create a full-fledged booking system. The sky's the limit!
Happy coding, and may your bookings always be on time! 🚀