Skip to content

Commit

Permalink
gensupport: set GetBody for media upload
Browse files Browse the repository at this point in the history
Since Go 1.8, you can set the GetBody field of http.Request to
re-create the request body for retryable errors.

This happens automatically for resumable upload, because each chunk
reader is a bytes.Reader, and net/http creates a GetBody for that.
But for single-chunk uploads, we must create the GetBody function
ourselves.

This CL adds the functionality to gensupport in a backwards-compatible
way, and updates the generator. It also regenerate the clients.

Other minor changes in this CL are from the go 1.10 vet checks run
during go test.

Fixes googleapis/google-cloud-go#811.

Change-Id: I9bcbf71a05850c994e5fd011c0a48e150a889d07
Reviewed-on: https://code-review.googlesource.com/22290
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
jba committed Jan 31, 2018
1 parent ffa5046 commit 8ec5d73
Show file tree
Hide file tree
Showing 28 changed files with 229 additions and 53 deletions.
3 changes: 2 additions & 1 deletion analytics/v3/analytics-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16606,11 +16606,12 @@ func (c *ManagementUploadsUploadDataCall) doRequest(alt string) (*http.Response,
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"accountId": c.accountId,
"webPropertyId": c.webPropertyId,
Expand Down
12 changes: 8 additions & 4 deletions androidpublisher/v2/androidpublisher-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4240,11 +4240,12 @@ func (c *EditsApksUploadCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"packageName": c.packageNameid,
"editId": c.editId,
Expand Down Expand Up @@ -4462,11 +4463,12 @@ func (c *EditsDeobfuscationfilesUploadCall) doRequest(alt string) (*http.Respons
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"packageName": c.packageNameid,
"editId": c.editId,
Expand Down Expand Up @@ -5662,11 +5664,12 @@ func (c *EditsExpansionfilesUploadCall) doRequest(alt string) (*http.Response, e
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"packageName": c.packageNameid,
"editId": c.editId,
Expand Down Expand Up @@ -6427,11 +6430,12 @@ func (c *EditsImagesUploadCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"packageName": c.packageNameid,
"editId": c.editId,
Expand Down
3 changes: 2 additions & 1 deletion bigquery/v2/bigquery-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4886,11 +4886,12 @@ func (c *JobsInsertCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"projectId": c.projectId,
})
Expand Down
3 changes: 2 additions & 1 deletion dfareporting/v2.7/dfareporting-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23106,11 +23106,12 @@ func (c *CreativeAssetsInsertCall) doRequest(alt string) (*http.Response, error)
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"profileId": strconv.FormatInt(c.profileId, 10),
"advertiserId": strconv.FormatInt(c.advertiserId, 10),
Expand Down
3 changes: 2 additions & 1 deletion dfareporting/v2.8/dfareporting-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23411,11 +23411,12 @@ func (c *CreativeAssetsInsertCall) doRequest(alt string) (*http.Response, error)
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"profileId": strconv.FormatInt(c.profileId, 10),
"advertiserId": strconv.FormatInt(c.advertiserId, 10),
Expand Down
3 changes: 2 additions & 1 deletion dfareporting/v3.0/dfareporting-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -24358,11 +24358,12 @@ func (c *CreativeAssetsInsertCall) doRequest(alt string) (*http.Response, error)
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"profileId": strconv.FormatInt(c.profileId, 10),
"advertiserId": strconv.FormatInt(c.advertiserId, 10),
Expand Down
9 changes: 6 additions & 3 deletions drive/v2/drive-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7335,11 +7335,12 @@ func (c *FilesInsertCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
return gensupport.SendRequest(c.ctx_, c.s.client, req)
}

Expand Down Expand Up @@ -8826,11 +8827,12 @@ func (c *FilesUpdateCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("PUT", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"fileId": c.fileId,
})
Expand Down Expand Up @@ -12237,11 +12239,12 @@ func (c *RealtimeUpdateCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("PUT", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"fileId": c.fileId,
})
Expand Down
6 changes: 4 additions & 2 deletions drive/v3/drive-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4146,11 +4146,12 @@ func (c *FilesCreateCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
return gensupport.SendRequest(c.ctx_, c.s.client, req)
}

Expand Down Expand Up @@ -5409,11 +5410,12 @@ func (c *FilesUpdateCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("PATCH", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"fileId": c.fileId,
})
Expand Down
6 changes: 4 additions & 2 deletions fusiontables/v1/fusiontables-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3805,11 +3805,12 @@ func (c *TableImportRowsCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"tableId": c.tableId,
})
Expand Down Expand Up @@ -4054,11 +4055,12 @@ func (c *TableImportTableCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
return gensupport.SendRequest(c.ctx_, c.s.client, req)
}

Expand Down
9 changes: 6 additions & 3 deletions fusiontables/v2/fusiontables-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3867,11 +3867,12 @@ func (c *TableImportRowsCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"tableId": c.tableId,
})
Expand Down Expand Up @@ -4116,11 +4117,12 @@ func (c *TableImportTableCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
return gensupport.SendRequest(c.ctx_, c.s.client, req)
}

Expand Down Expand Up @@ -4935,11 +4937,12 @@ func (c *TableReplaceRowsCall) doRequest(alt string) (*http.Response, error) {
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"tableId": c.tableId,
})
Expand Down
3 changes: 2 additions & 1 deletion gamesconfiguration/v1configuration/gamesconfiguration-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1539,11 +1539,12 @@ func (c *ImageConfigurationsUploadCall) doRequest(alt string) (*http.Response, e
body = new(bytes.Buffer)
reqHeaders.Set("Content-Type", "application/json")
}
body, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)
defer cleanup()
urls += "?" + c.urlParams_.Encode()
req, _ := http.NewRequest("POST", urls, body)
req.Header = reqHeaders
gensupport.SetGetBody(req, getBody)
googleapi.Expand(req.URL, map[string]string{
"resourceId": c.resourceId,
"imageType": c.imageType,
Expand Down
17 changes: 17 additions & 0 deletions gensupport/go18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.8

package gensupport

import (
"io"
"net/http"
)

// SetGetBody sets the GetBody field of req to f.
func SetGetBody(req *http.Request, f func() (io.ReadCloser, error)) {
req.GetBody = f
}
2 changes: 1 addition & 1 deletion gensupport/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func TestParseJSONTag(t *testing.T) {
t.Fatalf("parsing json:\n got err: %v\ntag: %q", err, tc.tag)
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("parseJSONTage:\ngot :%s\nwant:%s", got, tc.want)
t.Errorf("parseJSONTage:\ngot :%v\nwant:%v", got, tc.want)
}
}
}
Expand Down
40 changes: 36 additions & 4 deletions gensupport/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package gensupport

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"strings"

"google.golang.org/api/googleapi"
)
Expand Down Expand Up @@ -251,11 +253,11 @@ func (mi *MediaInfo) UploadType() string {
}

// UploadRequest sets up an HTTP request for media upload. It adds headers
// as necessary, and returns a replacement for the body.
func (mi *MediaInfo) UploadRequest(reqHeaders http.Header, body io.Reader) (newBody io.Reader, cleanup func()) {
// as necessary, and returns a replacement for the body and a function for http.Request.GetBody.
func (mi *MediaInfo) UploadRequest(reqHeaders http.Header, body io.Reader) (newBody io.Reader, getBody func() (io.ReadCloser, error), cleanup func()) {
cleanup = func() {}
if mi == nil {
return body, cleanup
return body, nil, cleanup
}
var media io.Reader
if mi.media != nil {
Expand All @@ -269,15 +271,45 @@ func (mi *MediaInfo) UploadRequest(reqHeaders http.Header, body io.Reader) (newB
media, _, _, _ = mi.buffer.Chunk()
}
if media != nil {
fb := readerFunc(body)
fm := readerFunc(media)
combined, ctype := CombineBodyMedia(body, "application/json", media, mi.mType)
if fb != nil && fm != nil {
getBody = func() (io.ReadCloser, error) {
rb := ioutil.NopCloser(fb())
rm := ioutil.NopCloser(fm())
r, _ := CombineBodyMedia(rb, "application/json", rm, mi.mType)
return r, nil
}
}
cleanup = func() { combined.Close() }
reqHeaders.Set("Content-Type", ctype)
body = combined
}
if mi.buffer != nil && mi.mType != "" && !mi.singleChunk {
reqHeaders.Set("X-Upload-Content-Type", mi.mType)
}
return body, cleanup
return body, getBody, cleanup
}

// readerFunc returns a function that always returns an io.Reader that has the same
// contents as r, provided that can be done without consuming r. Otherwise, it
// returns nil.
// See http.NewRequest (in net/http/request.go).
func readerFunc(r io.Reader) func() io.Reader {
switch r := r.(type) {
case *bytes.Buffer:
buf := r.Bytes()
return func() io.Reader { return bytes.NewReader(buf) }
case *bytes.Reader:
snapshot := *r
return func() io.Reader { r := snapshot; return &r }
case *strings.Reader:
snapshot := *r
return func() io.Reader { r := snapshot; return &r }
default:
return nil
}
}

// ResumableUpload returns an appropriately configured ResumableUpload value if the
Expand Down
Loading

0 comments on commit 8ec5d73

Please sign in to comment.