From: Florian Forster Date: Tue, 30 Jan 2018 08:33:19 +0000 (+0100) Subject: Package fitbit: Implement Client.Unsubscribe() and Client.DeleteToken() X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=e45460a9cf2134526a755cfa0c8124282f8ccf92;p=kraftakt.git Package fitbit: Implement Client.Unsubscribe() and Client.DeleteToken() --- diff --git a/fitbit/fitbit.go b/fitbit/fitbit.go index 394cfd8..c7a6aa7 100644 --- a/fitbit/fitbit.go +++ b/fitbit/fitbit.go @@ -200,6 +200,38 @@ func (c *Client) Subscribe(ctx context.Context, collection string) error { return nil } +func (c *Client) Unsubscribe(ctx context.Context) error { + subscriberID, err := c.appUser.ID(ctx) + if err != nil { + return err + } + + url := fmt.Sprintf("https://api.fitbit.com/1/user/%s/apiSubscriptions/%s.json", + c.fitbitUserID, subscriberID) + req, err := http.NewRequest(http.MethodDelete, url, nil) + if err != nil { + return err + } + + res, err := c.client.Do(req.WithContext(ctx)) + if err != nil { + return err + } + defer res.Body.Close() + + if res.StatusCode >= 400 && res.StatusCode != http.StatusConflict { + data, _ := ioutil.ReadAll(res.Body) + log.Errorf(ctx, "creating subscription failed: status %d %q", res.StatusCode, data) + return fmt.Errorf("deleting subscription failed") + } + + return nil +} + +func (c *Client) DeleteToken(ctx context.Context) error { + return c.appUser.DeleteToken(ctx, "Fitbit") +} + type Profile struct { Name string Timezone *time.Location