Hey there, fellow Go enthusiast! Ready to supercharge your scheduling game? Let's dive into building a YouCanBookMe API integration. This nifty tool will let you programmatically manage bookings, fetch available slots, and more. Buckle up!
Before we jump in, make sure you've got:
Let's get our hands dirty:
mkdir ycbm-integration && cd ycbm-integration go mod init github.com/yourusername/ycbm-integration
We'll need a HTTP client, but Go's standard library has us covered. No extra dependencies required!
YouCanBookMe uses API key authentication. Let's create a client that'll handle this for us:
type Client struct { apiKey string baseURL string httpClient *http.Client } func NewClient(apiKey string) *Client { return &Client{ apiKey: apiKey, baseURL: "https://api.youcanbook.me/v1", httpClient: &http.Client{}, } }
Now, let's create a method to make authenticated requests:
func (c *Client) doRequest(method, path string, body io.Reader) (*http.Response, error) { req, err := http.NewRequest(method, c.baseURL+path, body) if err != nil { return nil, err } req.Header.Set("Authorization", "Bearer "+c.apiKey) req.Header.Set("Content-Type", "application/json") return c.httpClient.Do(req) }
Let's implement some key features:
func (c *Client) GetAvailableSlots(profileID string, start, end time.Time) ([]Slot, error) { path := fmt.Sprintf("/profiles/%s/slots?start=%s&end=%s", profileID, start.Format(time.RFC3339), end.Format(time.RFC3339)) resp, err := c.doRequest("GET", path, nil) if err != nil { return nil, err } defer resp.Body.Close() var slots []Slot err = json.NewDecoder(resp.Body).Decode(&slots) return slots, err }
func (c *Client) CreateBooking(profileID string, booking Booking) (*Booking, error) { body, _ := json.Marshal(booking) resp, err := c.doRequest("POST", "/profiles/"+profileID+"/bookings", bytes.NewBuffer(body)) if err != nil { return nil, err } defer resp.Body.Close() var createdBooking Booking err = json.NewDecoder(resp.Body).Decode(&createdBooking) return &createdBooking, err }
Always check the response status and handle errors gracefully:
if resp.StatusCode != http.StatusOK { return fmt.Errorf("API request failed with status %d", resp.StatusCode) }
Wrap all your methods in the Client
struct we created earlier. This gives you a nice, reusable package:
client := ycbm.NewClient("your-api-key") slots, err := client.GetAvailableSlots("profile-id", start, end)
Don't forget to test! Here's a quick example:
func TestGetAvailableSlots(t *testing.T) { client := NewClient("test-api-key") slots, err := client.GetAvailableSlots("test-profile", time.Now(), time.Now().Add(24*time.Hour)) assert.NoError(t, err) assert.NotEmpty(t, slots) }
And there you have it! You've just built a solid YouCanBookMe API integration in Go. With this foundation, you can expand to cover more endpoints, build a CLI tool, or even create a full-fledged scheduling application. The possibilities are endless!
Remember, the best way to learn is by doing. So go ahead, tweak this code, break things, and most importantly, have fun building!
Happy coding, Gophers!