From 380a565ae90637a0b73c968f2e530860bef8ee74 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 30 Jan 2018 20:31:33 +0100 Subject: [PATCH] Package app: Load the ID with the rest of the user data. --- app/user.go | 36 ++++++++++++++++++++---------------- fitbit/fitbit.go | 14 ++------------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/app/user.go b/app/user.go index ffe65e9..9fe7e5d 100644 --- a/app/user.go +++ b/app/user.go @@ -14,8 +14,10 @@ import ( ) type User struct { + key *datastore.Key + + ID string Email string - key *datastore.Key } type dbUser struct { @@ -23,14 +25,23 @@ type dbUser struct { } func NewUser(ctx context.Context, email string) (*User, error) { + var id string err := datastore.RunInTransaction(ctx, func(ctx legacy_context.Context) error { key := datastore.NewKey(ctx, "User", email, 0, nil) - if err := datastore.Get(ctx, key, &dbUser{}); err != datastore.ErrNoSuchEntity { - return err // may be nil + + var u dbUser + err := datastore.Get(ctx, key, &u) + if err != nil && err != datastore.ErrNoSuchEntity { + return err + } + if err == nil { + id = u.ID + return nil } - _, err := datastore.Put(ctx, key, &dbUser{ - ID: uuid.New().String(), + id = uuid.New().String() + _, err = datastore.Put(ctx, key, &dbUser{ + ID: id, }) return err }, nil) @@ -39,8 +50,9 @@ func NewUser(ctx context.Context, email string) (*User, error) { } return &User{ - Email: email, key: datastore.NewKey(ctx, "User", email, 0, nil), + ID: id, + Email: email, }, nil } @@ -55,20 +67,12 @@ func UserByID(ctx context.Context, id string) (*User, error) { } return &User{ - Email: keys[0].StringID(), key: keys[0], + ID: id, + Email: keys[0].StringID(), }, nil } -func (u *User) ID(ctx context.Context) (string, error) { - var db dbUser - if err := datastore.Get(ctx, u.key, &db); err != nil { - return "", err - } - - return db.ID, nil -} - func (u *User) Token(ctx context.Context, svc string) (*oauth2.Token, error) { key := datastore.NewKey(ctx, "Token", svc, 0, u.key) diff --git a/fitbit/fitbit.go b/fitbit/fitbit.go index 36a6e87..d7a885d 100644 --- a/fitbit/fitbit.go +++ b/fitbit/fitbit.go @@ -177,13 +177,8 @@ func (c *Client) ActivitySummary(ctx context.Context, date string) (*ActivitySum } func (c *Client) Subscribe(ctx context.Context, collection string) error { - subscriberID, err := c.appUser.ID(ctx) - if err != nil { - return err - } - url := fmt.Sprintf("https://api.fitbit.com/1/user/%s/%s/apiSubscriptions/%s.json", - c.fitbitUserID, collection, subscriberID) + c.fitbitUserID, collection, c.appUser.ID) res, err := c.client.Post(url, "", nil) if err != nil { return err @@ -200,13 +195,8 @@ func (c *Client) Subscribe(ctx context.Context, collection string) error { } func (c *Client) Unsubscribe(ctx context.Context, collection string) error { - subscriberID, err := c.appUser.ID(ctx) - if err != nil { - return err - } - url := fmt.Sprintf("https://api.fitbit.com/1/user/%s/%s/apiSubscriptions/%s.json", - c.fitbitUserID, collection, subscriberID) + c.fitbitUserID, collection, c.appUser.ID) req, err := http.NewRequest(http.MethodDelete, url, nil) if err != nil { return err -- 2.11.0