Skip to content

Commit

Permalink
AddProtectedRange, BatchUpdateValues, QuotaUser
Browse files Browse the repository at this point in the history
- Adds a dedicated method for adding protected ranges which
  handles bogus 500 from Google API
- Adds a batch UpdateValues method
- Adds a way to specify the quota user
  • Loading branch information
Bowbaq committed Jun 8, 2020
1 parent a197f9d commit 6980fa1
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 14 deletions.
28 changes: 17 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Client struct {

Sheets *sheets.Service
Drive *drive.Service

options []googleapi.CallOption
}

func NewServiceAccountClientFromReader(creds io.Reader) (*Client, error) {
Expand Down Expand Up @@ -75,13 +77,17 @@ func NewClientFromConfig(config *jwt.Config) (*Client, error) {
}, nil
}

func (c *Client) AddOptions(opts ...googleapi.CallOption) {
c.options = append(c.options, opts...)
}

func (c *Client) ListFiles(query string) ([]*drive.File, error) {
var resp *drive.FileList
err := googleRetry(func() error {
var rerr error
resp, rerr = c.Drive.Files.List().PageSize(10).
Q(query).
Fields("nextPageToken, files(id, name, mimeType)").Do()
Fields("nextPageToken, files(id, name, mimeType)").Do(c.options...)

return rerr
})
Expand All @@ -98,7 +104,7 @@ func (c *Client) CopySpreadsheetFrom(fileID, newName string) (*Spreadsheet, erro
var rerr error
file, rerr = c.Drive.Files.Copy(fileID, &drive.File{
Name: newName,
}).Do()
}).Do(c.options...)

return rerr
})
Expand Down Expand Up @@ -126,7 +132,7 @@ func (c *Client) CreateSpreadsheet(title string) (*Spreadsheet, error) {
var ssInfo *sheets.Spreadsheet
err := googleRetry(func() error {
var rerr error
ssInfo, rerr = c.Sheets.Spreadsheets.Create(ssProps).Do()
ssInfo, rerr = c.Sheets.Spreadsheets.Create(ssProps).Do(c.options...)

return rerr
})
Expand Down Expand Up @@ -162,7 +168,7 @@ func (c *Client) GetSpreadsheet(spreadsheetId string) (*Spreadsheet, error) {
var ssInfo *sheets.Spreadsheet
err := googleRetry(func() error {
var rerr error
ssInfo, rerr = c.Sheets.Spreadsheets.Get(spreadsheetId).Do()
ssInfo, rerr = c.Sheets.Spreadsheets.Get(spreadsheetId).Do(c.options...)

return rerr
})
Expand All @@ -177,7 +183,7 @@ func (c *Client) GetSpreadsheetWithData(spreadsheetId string) (*Spreadsheet, err
var ssInfo *sheets.Spreadsheet
err := googleRetry(func() error {
var rerr error
ssInfo, rerr = c.Sheets.Spreadsheets.Get(spreadsheetId).IncludeGridData(true).Do()
ssInfo, rerr = c.Sheets.Spreadsheets.Get(spreadsheetId).IncludeGridData(true).Do(c.options...)

return rerr
})
Expand All @@ -192,7 +198,7 @@ func (c *Client) Delete(fileId string) error {
req := c.Drive.Files.Delete(fileId)

return googleRetry(func() error {
return req.Do()
return req.Do(c.options...)
})
}

Expand All @@ -213,7 +219,7 @@ func (c *Client) ShareWithAnyone(fileID string) error {
}

return googleRetry(func() error {
_, err := c.Drive.Permissions.Create(fileID, &perm).Do()
_, err := c.Drive.Permissions.Create(fileID, &perm).Do(c.options...)
return err
})
}
Expand All @@ -227,7 +233,7 @@ func (c *Client) shareFile(fileID, email string, notify bool) error {
req := c.Drive.Permissions.Create(fileID, &perm).SendNotificationEmail(notify)

return googleRetry(func() error {
_, err := req.Do()
_, err := req.Do(c.options...)
return err
})
}
Expand All @@ -236,7 +242,7 @@ func (c *Client) Revoke(fileID, email string) error {
var permissions *drive.PermissionList
err := googleRetry(func() error {
var rerr error
permissions, rerr = c.Drive.Permissions.List(fileID).Fields("nextPageToken, permissions(id, emailAddress, type, role)").Do()
permissions, rerr = c.Drive.Permissions.List(fileID).Fields("nextPageToken, permissions(id, emailAddress, type, role)").Do(c.options...)

return rerr
})
Expand All @@ -250,7 +256,7 @@ func (c *Client) Revoke(fileID, email string) error {
}

return googleRetry(func() error {
return c.Drive.Permissions.Delete(fileID, p.Id).Do()
return c.Drive.Permissions.Delete(fileID, p.Id).Do(c.options...)
})
}

Expand All @@ -267,7 +273,7 @@ func (c *Client) TransferOwnership(fileID, email string) error {
req := c.Drive.Permissions.Create(fileID, &perm).TransferOwnership(true)

return googleRetry(func() error {
_, err := req.Do()
_, err := req.Do(c.options...)
return err
})
}
Expand Down
71 changes: 68 additions & 3 deletions spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,47 @@ func isFakeDuplicateSheetError(err error) bool {
return firstErrorIsNotDuplicate && hasSubsequentDuplicate
}

func (s *Spreadsheet) AddProtectedRange(req *sheets.AddProtectedRangeRequest) error {
_, err := s.DoBatch(&sheets.Request{
AddProtectedRange: req,
})
if err != nil {
if !isFakeProtectedRangeError(err) {
return errors.Wrap(err, "couldn't add protected range to sheet")
}
}

return nil
}

func isFakeProtectedRangeError(err error) bool {
rerr, ok := err.(retry.Error)
if !ok {
return false
}

var (
firstErrorIsNotBadRequest = true
hasSubsequentBadRequest = false
)
for i, e := range rerr.WrappedErrors() {
if gerr, ok := e.(*googleapi.Error); ok {
if gerr.Code == 400 && strings.Contains(gerr.Message, "addProtectedRange") {
if i == 0 {
firstErrorIsNotBadRequest = false
} else {
hasSubsequentBadRequest = true
}
}
}
if e != nil {
fmt.Fprintf(os.Stderr, "%d - %v\n", i, e)
}
}

return firstErrorIsNotBadRequest && hasSubsequentBadRequest
}

func (s *Sheet) Title() string {
return s.Properties.Title
}
Expand Down Expand Up @@ -225,7 +266,31 @@ func (s *Sheet) UpdateFromPositionIface(data [][]interface{}, start CellPos) err
req.ValueInputOption("USER_ENTERED")

return googleRetry(func() error {
_, err := req.Do()
_, err := req.Do(s.Client.options...)
return err
})
}

type ValueUpdateRequest struct {
Start CellPos

Data [][]interface{}
}

func (s *Sheet) BatchUpdateFromPositionIface(requests ...*ValueUpdateRequest) error {
updates := sheets.BatchUpdateValuesRequest{
ValueInputOption: "USER_ENTERED",
}

for i := range requests {
updates.Data = append(updates.Data, &sheets.ValueRange{
Range: fmt.Sprintf("%s!%s", s.Title(), requests[i].Start.RangeForData(requests[i].Data).String()),
Values: requests[i].Data,
})
}

return googleRetry(func() error {
_, err := s.Client.Sheets.Spreadsheets.Values.BatchUpdate(s.Spreadsheet.Id(), &updates).Do(s.Client.options...)
return err
})
}
Expand All @@ -241,7 +306,7 @@ func (s *Sheet) Append(data [][]interface{}) error {
req.ValueInputOption("USER_ENTERED")

return googleRetry(func() error {
_, err := req.Do()
_, err := req.Do(s.Client.options...)
return err
})
}
Expand All @@ -255,7 +320,7 @@ func (s *Spreadsheet) DoBatch(reqs ...*sheets.Request) (*sheets.BatchUpdateSprea
var resp *sheets.BatchUpdateSpreadsheetResponse
err := googleRetry(func() error {
var rerr error
resp, rerr = s.Client.Sheets.Spreadsheets.BatchUpdate(s.Id(), &batchUpdateReq).Do()
resp, rerr = s.Client.Sheets.Spreadsheets.BatchUpdate(s.Id(), &batchUpdateReq).Do(s.Client.options...)
return rerr
})
if err != nil {
Expand Down

0 comments on commit 6980fa1

Please sign in to comment.