diff options
| author | Christine Dodrill <me@christine.website> | 2019-02-02 06:53:12 -0800 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2019-02-02 06:53:12 -0800 |
| commit | 7bcfbc6a4da40f498af83e40c3c57ef02ec212cf (patch) | |
| tree | ee224b7b08722e06d78f49bc557249891264e38f /web/switchcounter | |
| parent | da3595468056f1dd04d79c908f8a65dc0c5a397f (diff) | |
| download | x-7bcfbc6a4da40f498af83e40c3c57ef02ec212cf.tar.xz x-7bcfbc6a4da40f498af83e40c3c57ef02ec212cf.zip | |
web: use error type, simplify some code
Diffstat (limited to 'web/switchcounter')
| -rw-r--r-- | web/switchcounter/switchc.go | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/web/switchcounter/switchc.go b/web/switchcounter/switchc.go index b74eb42..f40929f 100644 --- a/web/switchcounter/switchc.go +++ b/web/switchcounter/switchc.go @@ -6,6 +6,8 @@ import ( "encoding/json" "net/http" "time" + + "github.com/Xe/x/web" ) type arg struct { @@ -19,6 +21,16 @@ type Status struct { StartedAt time.Time `json:"started_at"` } +// Validate ensures a HTTP response contains the expected fields. +func Validate(resp *http.Response) error { + if resp.StatusCode == http.StatusOK { + return nil + } + + return web.NewError(http.StatusOK, resp) +} + +// API is a builder for HTTP requests to interface with Switch Counter. type API struct { url string // webhook url } @@ -44,6 +56,7 @@ func (a API) makeRequestWith(body interface{}) (*http.Request, error) { return req, nil } +// Status returns a request for which systemmate is currently in front. func (a API) Status() *http.Request { result, err := a.makeRequestWith(arg{Command: "switch"}) if err != nil { @@ -52,6 +65,7 @@ func (a API) Status() *http.Request { return result } +// Switch changes the recorded front to the given systemmate. func (a API) Switch(front string) *http.Request { result, err := a.makeRequestWith(arg{Command: "switch", MemberName: front}) if err != nil { @@ -61,7 +75,7 @@ func (a API) Switch(front string) *http.Request { } // NewHTTPClient creates a new instance of API over HTTP. -func NewHTTPClient(a *http.Client, webhookURL string) API { +func NewHTTPClient(webhookURL string) API { return API{ url: webhookURL, } |
