diff options
| author | Christine Dodrill <me@christine.website> | 2018-10-04 20:06:42 -0700 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2018-10-04 20:06:53 -0700 |
| commit | def885cab56da5d4808185fd1feb80ea57fc0cbb (patch) | |
| tree | 7772d5cef5612cd8e5bb106478fdda0ebfc2a2ad /web | |
| parent | a49a42f602e301fc7d4137d98b75248285fd81da (diff) | |
| download | x-def885cab56da5d4808185fd1feb80ea57fc0cbb.tar.xz x-def885cab56da5d4808185fd1feb80ea57fc0cbb.zip | |
web/switchcounter: use a new-style go api
Diffstat (limited to 'web')
| -rw-r--r-- | web/switchcounter/switchc.go | 70 |
1 files changed, 16 insertions, 54 deletions
diff --git a/web/switchcounter/switchc.go b/web/switchcounter/switchc.go index d4bfbd3..232b3df 100644 --- a/web/switchcounter/switchc.go +++ b/web/switchcounter/switchc.go @@ -1,13 +1,9 @@ -// Package switchcounter is a simple interface to the https://www.switchcounter.science/ API. +// Package switchcounter is a simple interface to the https://www.switcaounter.science/ API. package switchcounter import ( "bytes" - "context" "encoding/json" - "fmt" - "io/ioutil" - "log" "net/http" "time" ) @@ -17,27 +13,17 @@ type arg struct { MemberName string `json:"member_name,omitempty"` } -// API is the switchcounter API as an abstract interface. -type API interface { - // Status returns the front of the system for this API client. - Status(ctx context.Context) (Status, error) - - // Switch changes who is in front. - Switch(ctx context.Context, front string) (Status, error) -} - // Status is the API response. type Status struct { Front string `json:"member_name"` StartedAt time.Time `json:"started_at"` } -type httpClient struct { - hc *http.Client +type API struct { url string // webhook url } -func (hc httpClient) makeRequestWith(ctx context.Context, body interface{}) (*Status, error) { +func (a API) makeRequestWith(body interface{}) (*http.Request, error) { env := struct { Webhook interface{} `json:"webhook"` }{ @@ -48,59 +34,35 @@ func (hc httpClient) makeRequestWith(ctx context.Context, body interface{}) (*St return nil, err } - req, err := http.NewRequest("POST", hc.url, bytes.NewBuffer(data)) + req, err := http.NewRequest(http.MethodPost, a.url, bytes.NewBuffer(data)) if err != nil { return nil, err } - req = req.WithContext(ctx) req.Header.Add("Content-Type", "application/json") + req.Header.Add("Accept", "application/json") - resp, err := hc.hc.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - data, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - log.Printf("body: %s", string(data)) - - return nil, fmt.Errorf("http response code %d", resp.StatusCode) - } - - var result Status - err = json.NewDecoder(resp.Body).Decode(&result) - if err != nil { - return nil, err - } - - return &result, nil + return req, nil } -func (hc httpClient) Status(ctx context.Context) (Status, error) { - result, err := hc.makeRequestWith(ctx, arg{Command: "switch"}) +func (a API) Status() *http.Request { + result, err := a.makeRequestWith(arg{Command: "switch"}) if err != nil { - return Status{}, err + panic(err) } - return *result, nil + return result } -func (hc httpClient) Switch(ctx context.Context, front string) (Status, error) { - result, err := hc.makeRequestWith(ctx, arg{Command: "switch", MemberName: front}) +func (a API) Switch(front string) *http.Request { + result, err := a.makeRequestWith(arg{Command: "switch", MemberName: front}) if err != nil { - return Status{}, err + panic(err) } - return *result, nil + return result } // NewHTTPClient creates a new instance of API over HTTP. -func NewHTTPClient(hc *http.Client, webhookURL string) API { - return httpClient{ - hc: hc, +func NewHTTPClient(a *http.Client, webhookURL string) API { + return API{ url: webhookURL, } } |
