From e45460a9cf2134526a755cfa0c8124282f8ccf92 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 30 Jan 2018 09:33:19 +0100 Subject: [PATCH] Package fitbit: Implement Client.Unsubscribe() and Client.DeleteToken() --- fitbit/fitbit.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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 -- 2.11.0