)
type User struct {
+ key *datastore.Key
+
+ ID string
Email string
- key *datastore.Key
}
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)
}
return &User{
- Email: email,
key: datastore.NewKey(ctx, "User", email, 0, nil),
+ ID: id,
+ Email: email,
}, nil
}
}
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)
}
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
}
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