github.com/wtfutil/wtf@v0.43.0/modules/bamboohr/client.go (about)

     1  package bamboohr
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  )
     7  
     8  // A Client represents the data required to connect to the BambooHR API
     9  type Client struct {
    10  	apiBase   string
    11  	apiKey    string
    12  	subdomain string
    13  }
    14  
    15  // NewClient creates and returns a new BambooHR client
    16  func NewClient(url string, apiKey string, subdomain string) *Client {
    17  	client := Client{
    18  		apiBase:   url,
    19  		apiKey:    apiKey,
    20  		subdomain: subdomain,
    21  	}
    22  
    23  	return &client
    24  }
    25  
    26  /* -------------------- Public Functions -------------------- */
    27  
    28  // Away returns a string representation of the people who are out of the office during the defined period
    29  func (client *Client) Away(itemType, startDate, endDate string) []Item {
    30  	calendar, err := client.getWhoIsAway(startDate, endDate)
    31  	if err != nil {
    32  		return []Item{}
    33  	}
    34  
    35  	items := calendar.ItemsByType(itemType)
    36  
    37  	return items
    38  }
    39  
    40  /* -------------------- Private Functions -------------------- */
    41  
    42  // getWhoIsAway is the private interface for retrieving structural data about who will be out of the office
    43  // This method does the actual communication with BambooHR and returns the raw Go
    44  // data structures used by the public interface
    45  func (client *Client) getWhoIsAway(startDate, endDate string) (cal Calendar, err error) {
    46  	apiURL := fmt.Sprintf(
    47  		"%s/%s/v1/time_off/whos_out?start=%s&end=%s",
    48  		client.apiBase,
    49  		client.subdomain,
    50  		startDate,
    51  		endDate,
    52  	)
    53  
    54  	data, err := Request(client.apiKey, apiURL)
    55  	if err != nil {
    56  		return cal, err
    57  	}
    58  	err = xml.Unmarshal(data, &cal)
    59  
    60  	return
    61  }