Package fitbit: Implement the Profile() method.
authorFlorian Forster <ff@octo.it>
Fri, 12 Jan 2018 12:57:54 +0000 (13:57 +0100)
committerFlorian Forster <ff@octo.it>
Fri, 12 Jan 2018 15:32:59 +0000 (16:32 +0100)
fitbit/fitbit.go
gfitsync.go

index 81d4ebc..9f661d6 100644 (file)
@@ -23,7 +23,7 @@ var oauth2Config = &oauth2.Config{
        ClientSecret: "@FITBIT_CLIENT_SECRET@",
        Endpoint:     oauth2fitbit.Endpoint,
        RedirectURL:  "https://fitbit-gfit-sync.appspot.com/fitbit/grant",
        ClientSecret: "@FITBIT_CLIENT_SECRET@",
        Endpoint:     oauth2fitbit.Endpoint,
        RedirectURL:  "https://fitbit-gfit-sync.appspot.com/fitbit/grant",
-       Scopes:       []string{"activity"},
+       Scopes:       []string{"activity", "heartrate", "profile"},
 }
 
 const csrfToken = "@CSRFTOKEN@"
 }
 
 const csrfToken = "@CSRFTOKEN@"
@@ -179,3 +179,43 @@ func (c *Client) Subscribe(ctx context.Context, collection string) error {
 
        return nil
 }
 
        return nil
 }
+
+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
+}
index 22f4c56..b7aa36d 100644 (file)
@@ -219,12 +219,18 @@ func handleNotification(ctx context.Context, s *fitbit.Subscription) error {
                return err
        }
 
                return err
        }
 
-       tm, err := time.Parse("2006-01-02", s.Date)
+       fitbitClient, err := fitbit.NewClient(ctx, s.OwnerID, u)
        if err != nil {
                return err
        }
 
        if err != nil {
                return err
        }
 
-       fitbitClient, err := fitbit.NewClient(ctx, s.OwnerID, u)
+       profile, err := fitbitClient.Profile(ctx)
+       if err != nil {
+               return err
+       }
+       log.Debugf(ctx, "profile = %+v", profile)
+
+       tm, err := time.ParseInLocation("2006-01-02", s.Date, profile.Timezone)
        if err != nil {
                return err
        }
        if err != nil {
                return err
        }
@@ -233,7 +239,8 @@ func handleNotification(ctx context.Context, s *fitbit.Subscription) error {
        if err != nil {
                return err
        }
        if err != nil {
                return err
        }
-       log.Debugf(ctx, "ActivitySummary for %s = %+v", u.Email, summary)
+       log.Debugf(ctx, "%s (%s) took %d steps on %s",
+               profile.Name, u.Email, summary.Summary.Steps, s.Date)
 
        gfitClient, err := gfit.NewClient(ctx, u)
        if err != nil {
 
        gfitClient, err := gfit.NewClient(ctx, u)
        if err != nil {