+
+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")
+}
+
+type Profile struct {
+ Name string
+ Timezone *time.Location
+}
+
+func (c *Client) Profile(ctx context.Context) (*Profile, error) {
+ res, err := c.client.Get("https://api.fitbit.com/1/user/-/profile.json")
+ if err != nil {
+ return nil, err
+ }
+ defer res.Body.Close()
+
+ if res.StatusCode >= 400 {
+ data, _ := ioutil.ReadAll(res.Body)
+ log.Errorf(ctx, "reading profile failed: %s", data)
+ return nil, fmt.Errorf("HTTP %d error", res.StatusCode)
+ }
+
+ var data struct {
+ User struct {
+ FullName string
+ OffsetFromUTCMillis int
+ Timezone string
+ }
+ }
+ if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
+ return nil, err
+ }
+
+ loc, err := time.LoadLocation(data.User.Timezone)
+ if err != nil {
+ loc = time.FixedZone("Fitbit preference", data.User.OffsetFromUTCMillis/1000)
+ }
+
+ return &Profile{
+ Name: data.User.FullName,
+ Timezone: loc,
+ }, nil
+}