1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/bamboohr/client.go
Sean Smith 14d85715b0 Decouple bamboohr client from widget
Have the client be in charge of API
Have the widget be in charge of widget config
This is a step in the direction of running multiple version (with their own configs)
2019-02-15 23:43:39 -05:00

62 lines
1.4 KiB
Go

package bamboohr
import (
"encoding/xml"
"fmt"
)
// A Client represents the data required to connect to the BambooHR API
type Client struct {
apiBase string
apiKey string
subdomain string
}
// NewClient creates and returns a new BambooHR client
func NewClient(url string, apiKey string, subdomain string) *Client {
client := Client{
apiBase: url,
apiKey: apiKey,
subdomain: subdomain,
}
return &client
}
/* -------------------- Public Functions -------------------- */
// Away returns a string representation of the people who are out of the office during the defined period
func (client *Client) Away(itemType, startDate, endDate string) []Item {
calendar, err := client.away(startDate, endDate)
if err != nil {
return []Item{}
}
items := calendar.ItemsByType(itemType)
return items
}
/* -------------------- Private Functions -------------------- */
// away is the private interface for retrieving structural data about who will be out of the office
// This method does the actual communication with BambooHR and returns the raw Go
// data structures used by the public interface
func (client *Client) away(startDate, endDate string) (cal Calendar, err error) {
apiURL := fmt.Sprintf(
"%s/%s/v1/time_off/whos_out?start=%s&end=%s",
client.apiBase,
client.subdomain,
startDate,
endDate,
)
data, err := Request(client.apiKey, apiURL)
if err != nil {
return cal, err
}
err = xml.Unmarshal(data, &cal)
return
}