From 76cdfd3de35ba0c179cdaba7124fa363ebf7ffd1 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Fri, 28 Feb 2020 19:34:59 -0600 Subject: [PATCH] add more methods --- endpoints.go | 7 ++++++- rest.go | 49 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/endpoints.go b/endpoints.go index ad2ba1a..ba1f703 100644 --- a/endpoints.go +++ b/endpoints.go @@ -14,5 +14,10 @@ func BuildURL(rawurl, rawpath string) string { var ( Endpoint = "https://api.lifx.com/v1" - EndpointState = func(selector string) string { return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state", selector)) } + EndpointState = func(selector string) string { + return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state", selector)) + } + EndpointToggle = func(selector string) string { + return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector)) + } ) diff --git a/rest.go b/rest.go index 1d1dcb1..7ad2473 100644 --- a/rest.go +++ b/rest.go @@ -28,48 +28,77 @@ func (s *Session) NewRequest(method, url string, body io.Reader) (req *http.Requ func (s *Session) Request(method, url string, body io.Reader) ([]Result, error) { req, err := s.NewRequest(method, url, body) if err != nil { - return []Result{}, err + return nil, err } resp, err := s.Client.Do(req) if err != nil { - return []Result{}, err + return nil, err } switch resp.StatusCode { case http.StatusAccepted: - return []Result{}, nil + return nil, nil case http.StatusMultiStatus: body, err := ioutil.ReadAll(resp.Body) if err != nil { - return []Result{}, err + return nil, err } r := Results{} err = json.Unmarshal(body, &r) if err != nil { - return []Result{}, err + return nil, err } return r.Results, nil } - return []Result{}, nil + return nil, nil } func (s *Session) SetState(selector string, state *State) ([]Result, error) { j, err := json.Marshal(state) if err != nil { - return []Result{}, err + return nil, err } res, err := s.Request("PUT", EndpointState(selector), bytes.NewBuffer(j)) if err != nil { - return []Result{}, err + return nil, err } return res, nil } -func (s *Session) PowerOff(selector string) { - s.SetState(selector, &State{Power: "off"}) +func (s *Session) Toggle(selector string, duration float64) ([]Result, error) { + m := make(map[string]interface{}) + m["duration"] = duration + j, err := json.Marshal(m) + if err != nil { + return nil, err + } + + res, err := s.Request("POST", EndpointToggle(selector), bytes.NewBuffer(j)) + if err != nil { + return nil, err + } + + return res, nil +} + +func (s *Session) PowerOff(selector string) ([]Result, error) { + return s.SetState(selector, &State{Power: "off"}) + +} + +func (s *Session) FastPowerOff(selector string) { + s.SetState(selector, &State{Power: "off", Fast: true}) +} + +func (s *Session) PowerOn(selector string) ([]Result, error) { + return s.SetState(selector, &State{Power: "on"}) +} + +func (s *Session) FastPowerOn(selector string) { + s.SetState(selector, &State{Power: "on", Fast: true}) }