Package fitbit: Implement UnsubscribeAll() and ListSubscriptions().
authorFlorian Forster <ff@octo.it>
Wed, 31 Jan 2018 07:02:40 +0000 (08:02 +0100)
committerFlorian Forster <ff@octo.it>
Wed, 31 Jan 2018 07:02:40 +0000 (08:02 +0100)
fitbit/fitbit.go
kraftakt.go

index 6f15b7c..52b89d7 100644 (file)
@@ -15,6 +15,7 @@ import (
        "github.com/octo/kraftakt/app"
        "golang.org/x/oauth2"
        oauth2fitbit "golang.org/x/oauth2/fitbit"
+       "google.golang.org/appengine"
        "google.golang.org/appengine/log"
 )
 
@@ -131,6 +132,11 @@ type Subscription struct {
        SubscriptionID string `json:"subscriptionId"`
 }
 
+func (s Subscription) String() string {
+       return fmt.Sprintf("https://api.fitbit.com/1/%s/%s/%s/apiSubscriptions/%s.json",
+               s.OwnerType, s.OwnerID, s.CollectionType, s.SubscriptionID)
+}
+
 type Client struct {
        fitbitUserID string
        appUser      *app.User
@@ -144,7 +150,7 @@ func NewClient(ctx context.Context, fitbitUserID string, u *app.User) (*Client,
 
        c, err := u.OAuthClient(ctx, "Fitbit", oauthConfig())
        if err != nil {
-               return nil, err
+               return nil, fmt.Errorf("OAuthClient(%q) = %v", "Fitbit", err)
        }
 
        return &Client{
@@ -231,6 +237,59 @@ func (c *Client) Unsubscribe(ctx context.Context, collection string) error {
        return nil
 }
 
+func (c *Client) UnsubscribeAll(ctx context.Context) error {
+       subs, err := c.ListSubscriptions(ctx)
+       if err != nil {
+               return err
+       }
+
+       var errs appengine.MultiError
+       for _, s := range subs {
+               if s.OwnerType != "user" {
+                       log.Infof(ctx, "unexpected OwnerType: %q", s.OwnerType)
+                       continue
+               }
+               if err := c.Unsubscribe(ctx, s.CollectionType); err != nil {
+                       errs = append(errs, err)
+               }
+       }
+       if len(errs) != 0 {
+               return errs
+       }
+
+       return nil
+}
+
+func (c *Client) ListSubscriptions(ctx context.Context) ([]Subscription, error) {
+       url := fmt.Sprintf("https://api.fitbit.com/1/user/%s/apiSubscriptions.json", c.fitbitUserID)
+       res, err := c.client.Get(url)
+       if err != nil {
+               return nil, fmt.Errorf("Get(%q) = %v", url, err)
+       }
+       defer res.Body.Close()
+
+       if res.StatusCode >= 400 && res.StatusCode != http.StatusNotFound {
+               data, _ := ioutil.ReadAll(res.Body)
+               log.Errorf(ctx, "listing subscriptions failed: status %d %q", res.StatusCode, data)
+               return nil, fmt.Errorf("listing subscriptions failed")
+       }
+       if res.StatusCode == http.StatusNotFound {
+               log.Infof(ctx, "listing subscriptions: not found")
+               return nil, nil
+       }
+
+       var subscriptions []Subscription
+       if err := json.NewDecoder(res.Body).Decode(&subscriptions); err != nil {
+               return nil, err
+       }
+
+       for i, s := range subscriptions {
+               log.Debugf(ctx, "ListSubscriptions() = %d: %s", i, s)
+       }
+
+       return subscriptions, nil
+}
+
 func (c *Client) DeleteToken(ctx context.Context) error {
        return c.appUser.DeleteToken(ctx, "Fitbit")
 }
index 8d56299..61a576e 100644 (file)
@@ -171,21 +171,9 @@ func fitbitDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http
                return err
        }
 
-       var errs appengine.MultiError
-
-       for _, collection := range []string{"activities", "sleep"} {
-               if err := c.Unsubscribe(ctx, collection); err != nil {
-                       errs = append(errs, fmt.Errorf("Unsubscribe(%q) = %v", collection, err))
-                       continue
-               }
-               log.Infof(ctx, "Successfully unsubscribed from %q", collection)
-       }
-
-       if err := c.DeleteToken(ctx); err != nil {
-               errs = append(errs, fmt.Errorf("DeleteToken() = %v", err))
-       }
-       if len(errs) != 0 {
-               return errs
+       if err := c.UnsubscribeAll(ctx); err != nil {
+               log.Errorf(ctx, "UnsubscribeAll() = %v", err)
+               return fmt.Errorf("deleting all subscriptions failed")
        }
 
        redirectURL := r.URL