Skip to content

Commit

Permalink
all: add x-goog-api-client header to outgoing requests
Browse files Browse the repository at this point in the history
Also removes defunct gensupport/header.go.

Fixes #387

Change-Id: I7905e9ebab24ec9db8eb31770fae8628b62e494f
Reviewed-on: https://code-review.googlesource.com/c/google-api-go-client/+/43416
Reviewed-by: kokoro <[email protected]>
Reviewed-by: Chris Broadfoot <[email protected]>
  • Loading branch information
jeanbza committed Aug 5, 2019
1 parent 616965b commit 2639483
Show file tree
Hide file tree
Showing 21 changed files with 256 additions and 77 deletions.
1 change: 1 addition & 0 deletions RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
version `$NV`.
1. On master, run `git log $CV..` to list all the changes since the last
release.
1. `cd internal/version && go generate && cd ../..`
1. Edit `CHANGES.md` to include a summary of the changes.
1. Mail the CL containing the `CHANGES.md` changes. When the CL is approved,
submit it.
Expand Down
22 changes: 0 additions & 22 deletions gensupport/header.go

This file was deleted.

28 changes: 0 additions & 28 deletions gensupport/header_test.go

This file was deleted.

3 changes: 3 additions & 0 deletions google-api-go-generator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ GENERATOR=./google-api-go-generator
# Download all API specifications and rebuild Go bindings.
# All downloaded files are cached in $TMPDIR for reuse with 'cached' below.
all: $(GENERATOR)
cd ../internal/version
go generate
cd -
$(GENERATOR) -cache=false -install -api=* -gendir=..
go test . -v

Expand Down
1 change: 0 additions & 1 deletion google-api-go-generator/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (

//go:generate -command api go run gen.go docurls.go replacements.go -install -api

//go:generate api dfareporting:v2.8
//go:generate api monitoring:v3
//go:generate api storage:v1

Expand Down
2 changes: 2 additions & 0 deletions google-api-go-generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"unicode"

"google.golang.org/api/google-api-go-generator/internal/disco"
"google.golang.org/api/internal/version"
)

const (
Expand Down Expand Up @@ -1911,6 +1912,7 @@ func (meth *Method) generateCode() {

pn("\nfunc (c *%s) doRequest(alt string) (*http.Response, error) {", callName)
pn(`reqHeaders := make(http.Header)`)
pn(`reqHeaders.Set("x-goog-api-client", "gl-go/%s gdcl/%s")`, version.Go(), version.Repo)
pn("for k, v := range c.header_ {")
pn(" reqHeaders[k] = v")
pn("}")
Expand Down
60 changes: 34 additions & 26 deletions google-api-go-generator/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"testing"

"google.golang.org/api/internal/version"
)

var updateGolden = flag.Bool("update_golden", false, "If true, causes TestAPIs to update golden files")
Expand Down Expand Up @@ -54,34 +57,39 @@ func TestAPIs(t *testing.T) {
"wrapnewlines",
}
for _, name := range names {
t.Logf("TEST %s", name)
api, err := apiFromFile(filepath.Join("testdata", name+".json"))
if err != nil {
t.Errorf("Error loading API testdata/%s.json: %v", name, err)
continue
}
clean, err := api.GenerateCode()
if err != nil {
t.Errorf("Error generating code for %s: %v", name, err)
continue
}
goldenFile := filepath.Join("testdata", name+".want")
if *updateGolden {
if err := ioutil.WriteFile(goldenFile, clean, 0644); err != nil {
t.Run(name, func(t *testing.T) {
api, err := apiFromFile(filepath.Join("testdata", name+".json"))
if err != nil {
t.Fatalf("Error loading API testdata/%s.json: %v", name, err)
}
clean, err := api.GenerateCode()
if err != nil {
t.Fatalf("Error generating code for %s: %v", name, err)
}
goldenFile := filepath.Join("testdata", name+".want")
if *updateGolden {
if err := ioutil.WriteFile(goldenFile, clean, 0644); err != nil {
t.Fatal(err)
}
}
want, err := ioutil.ReadFile(goldenFile)
if err != nil {
t.Fatal(err)
}
}
want, err := ioutil.ReadFile(goldenFile)
if err != nil {
t.Error(err)
continue
}
if !bytes.Equal(want, clean) {
tf, _ := ioutil.TempFile("", "api-"+name+"-got-json.")
tf.Write(clean)
tf.Close()
t.Errorf("Output for API %s differs: diff -u %s %s", name, goldenFile, tf.Name())
}
wantStr := strings.Replace(string(want), "gl-go/1.12.5", fmt.Sprintf("gl-go/%s", version.Go()), -1)
wantStr = strings.Replace(wantStr, "gdcl/00000000", fmt.Sprintf("gdcl/%s", version.Repo), -1)
want = []byte(wantStr)
if !bytes.Equal(want, clean) {
tf, _ := ioutil.TempFile("", "api-"+name+"-got-json.")
if _, err := tf.Write(clean); err != nil {
t.Fatal(err)
}
if err := tf.Close(); err != nil {
t.Fatal(err)
}
t.Errorf("Output for API %s differs: diff -u %s %s", name, goldenFile, tf.Name())
}
})
}
}

Expand Down
15 changes: 15 additions & 0 deletions google-api-go-generator/testdata/any.want
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ func (c *ProjectsLogServicesListCall) Header() http.Header {

func (c *ProjectsLogServicesListCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -1084,6 +1085,7 @@ func (c *ProjectsLogServicesIndexesListCall) Header() http.Header {

func (c *ProjectsLogServicesIndexesListCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -1274,6 +1276,7 @@ func (c *ProjectsLogServicesSinksCreateCall) Header() http.Header {

func (c *ProjectsLogServicesSinksCreateCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -1421,6 +1424,7 @@ func (c *ProjectsLogServicesSinksDeleteCall) Header() http.Header {

func (c *ProjectsLogServicesSinksDeleteCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -1579,6 +1583,7 @@ func (c *ProjectsLogServicesSinksGetCall) Header() http.Header {

func (c *ProjectsLogServicesSinksGetCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -1738,6 +1743,7 @@ func (c *ProjectsLogServicesSinksListCall) Header() http.Header {

func (c *ProjectsLogServicesSinksListCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -1882,6 +1888,7 @@ func (c *ProjectsLogServicesSinksUpdateCall) Header() http.Header {

func (c *ProjectsLogServicesSinksUpdateCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -2036,6 +2043,7 @@ func (c *ProjectsLogsDeleteCall) Header() http.Header {

func (c *ProjectsLogsDeleteCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -2223,6 +2231,7 @@ func (c *ProjectsLogsListCall) Header() http.Header {

func (c *ProjectsLogsListCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -2406,6 +2415,7 @@ func (c *ProjectsLogsEntriesWriteCall) Header() http.Header {

func (c *ProjectsLogsEntriesWriteCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -2553,6 +2563,7 @@ func (c *ProjectsLogsSinksCreateCall) Header() http.Header {

func (c *ProjectsLogsSinksCreateCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -2700,6 +2711,7 @@ func (c *ProjectsLogsSinksDeleteCall) Header() http.Header {

func (c *ProjectsLogsSinksDeleteCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -2858,6 +2870,7 @@ func (c *ProjectsLogsSinksGetCall) Header() http.Header {

func (c *ProjectsLogsSinksGetCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -3017,6 +3030,7 @@ func (c *ProjectsLogsSinksListCall) Header() http.Header {

func (c *ProjectsLogsSinksListCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down Expand Up @@ -3161,6 +3175,7 @@ func (c *ProjectsLogsSinksUpdateCall) Header() http.Header {

func (c *ProjectsLogsSinksUpdateCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/1.12.5 gdcl/00000000")
for k, v := range c.header_ {
reqHeaders[k] = v
}
Expand Down
Loading

0 comments on commit 2639483

Please sign in to comment.