rename Session struct to Client

This commit is contained in:
Ryan Cavicchioni 2020-02-28 21:56:33 -06:00
parent 76cdfd3de3
commit e05efa9d57
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
2 changed files with 18 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package lifx
import ( import (
"bytes" "bytes"
//"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -9,14 +10,17 @@ import (
"net/http" "net/http"
) )
func NewSession(token string) *Session { func NewClient(token string) *Client {
return &Session{ tr := &http.Transport{
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
return &Client{
token: token, token: token,
Client: &http.Client{}, Client: &http.Client{Transport: tr},
} }
} }
func (s *Session) NewRequest(method, url string, body io.Reader) (req *http.Request, err error) { func (s *Client) NewRequest(method, url string, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest(method, url, body) req, err = http.NewRequest(method, url, body)
if err != nil { if err != nil {
return return
@ -25,7 +29,7 @@ func (s *Session) NewRequest(method, url string, body io.Reader) (req *http.Requ
return return
} }
func (s *Session) Request(method, url string, body io.Reader) ([]Result, error) { func (s *Client) Request(method, url string, body io.Reader) ([]Result, error) {
req, err := s.NewRequest(method, url, body) req, err := s.NewRequest(method, url, body)
if err != nil { if err != nil {
return nil, err return nil, err
@ -56,7 +60,7 @@ func (s *Session) Request(method, url string, body io.Reader) ([]Result, error)
return nil, nil return nil, nil
} }
func (s *Session) SetState(selector string, state *State) ([]Result, error) { func (s *Client) SetState(selector string, state *State) ([]Result, error) {
j, err := json.Marshal(state) j, err := json.Marshal(state)
if err != nil { if err != nil {
return nil, err return nil, err
@ -70,7 +74,7 @@ func (s *Session) SetState(selector string, state *State) ([]Result, error) {
return res, nil return res, nil
} }
func (s *Session) Toggle(selector string, duration float64) ([]Result, error) { func (s *Client) Toggle(selector string, duration float64) ([]Result, error) {
m := make(map[string]interface{}) m := make(map[string]interface{})
m["duration"] = duration m["duration"] = duration
j, err := json.Marshal(m) j, err := json.Marshal(m)
@ -86,19 +90,19 @@ func (s *Session) Toggle(selector string, duration float64) ([]Result, error) {
return res, nil return res, nil
} }
func (s *Session) PowerOff(selector string) ([]Result, error) { func (s *Client) PowerOff(selector string) ([]Result, error) {
return s.SetState(selector, &State{Power: "off"}) return s.SetState(selector, &State{Power: "off"})
} }
func (s *Session) FastPowerOff(selector string) { func (s *Client) FastPowerOff(selector string) {
s.SetState(selector, &State{Power: "off", Fast: true}) s.SetState(selector, &State{Power: "off", Fast: true})
} }
func (s *Session) PowerOn(selector string) ([]Result, error) { func (s *Client) PowerOn(selector string) ([]Result, error) {
return s.SetState(selector, &State{Power: "on"}) return s.SetState(selector, &State{Power: "on"})
} }
func (s *Session) FastPowerOn(selector string) { func (s *Client) FastPowerOn(selector string) {
s.SetState(selector, &State{Power: "on", Fast: true}) s.SetState(selector, &State{Power: "on", Fast: true})
} }

View File

@ -16,10 +16,9 @@ type (
Fast bool `json:"fast,omitempty"` Fast bool `json:"fast,omitempty"`
} }
Session struct { Client struct {
BaseUrl string token string
token string Client *http.Client
Client *http.Client
} }
Results struct { Results struct {