"github.com/octo/kraftakt/app"
"github.com/octo/kraftakt/fitbit"
- "github.com/octo/retry"
"golang.org/x/oauth2"
oauth2google "golang.org/x/oauth2/google"
fitness "google.golang.org/api/fitness/v1"
return strings.Join(fields, ":")
}
-func wrapGoogleError(err error) error {
- if err == nil {
- return nil
- }
-
- if gerr, ok := err.(*googleapi.Error); ok && gerr.Code >= 400 && gerr.Code < 500 {
- return retry.Abort(err)
- }
-
- return err
-}
-
func (c *Client) DataSourceCreate(ctx context.Context, dataSource *fitness.DataSource) (string, error) {
- var dataStreamID string
- err := retry.Do(ctx, func(ctx context.Context) error {
- res, err := c.Service.Users.DataSources.Create(userID, dataSource).Context(ctx).Do()
- if err != nil {
- return wrapGoogleError(err)
- }
- dataStreamID = res.DataStreamId
- return nil
- })
+ res, err := c.Service.Users.DataSources.Create(userID, dataSource).Context(ctx).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusConflict {
if dataSource.DataStreamId != "" {
}
return DataStreamID(dataSource), nil
}
- log.Errorf(ctx, "c.Service.Users.DataSources.Create(%q) = %v", DataStreamID(dataSource), err)
- return "", err
+ return "", fmt.Errorf("DataSources.Create(%q) = %v", DataStreamID(dataSource), err)
}
- return dataStreamID, nil
+
+ return res.DataStreamId, nil
}
-func (c *Client) DatasetGet(ctx context.Context, dataStreamID string, startTime, endTime time.Time) (*fitness.Dataset, error) {
+func (c *Client) DatasetGet(ctx context.Context, dataSourceID string, startTime, endTime time.Time) (*fitness.Dataset, error) {
datasetID := fmt.Sprintf("%d-%d", startTime.UnixNano(), endTime.UnixNano())
- var dataset *fitness.Dataset
- err := retry.Do(ctx, func(ctx context.Context) error {
- res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataStreamID, datasetID).Context(ctx).Do()
- if err != nil {
- return wrapGoogleError(err)
- }
- dataset = res
- return nil
- })
+ res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataSourceID, datasetID).Context(ctx).Do()
if err != nil {
- log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataStreamID, datasetID, err)
- return nil, err
+ return nil, fmt.Errorf("DataSources.Datasets.Get(%q, %q) = %v", dataSourceID, datasetID, err)
}
- return dataset, nil
+ return res, nil
}
func (c *Client) DatasetPatch(ctx context.Context, dataSourceID string, points []*fitness.DataPoint) error {
Point: points,
}
- err := retry.Do(ctx, func(ctx context.Context) error {
- _, err := c.Service.Users.DataSources.Datasets.Patch(userID, dataSourceID, datasetID, dataset).Context(ctx).Do()
- return wrapGoogleError(err)
- })
+ _, err := c.Service.Users.DataSources.Datasets.Patch(userID, dataSourceID, datasetID, dataset).Context(ctx).Do()
if err != nil {
- log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Patch() = %v", err)
+ log.Errorf(ctx, "DataSources.Datasets.Patch(%q, %q) = %v", dataSourceID, datasetID, err)
return err
}
return nil