Skip to content

Commit

Permalink
feat(google-api-go-client): add x-goog-api-version header (#2563)
Browse files Browse the repository at this point in the history
  • Loading branch information
quartzmo committed May 2, 2024
1 parent b313e4b commit fe54ffd
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 18 deletions.
7 changes: 6 additions & 1 deletion google-api-go-generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,12 @@ func (meth *Method) generateCode() {
if !meth.IsRawRequest() && args.bodyArg() != nil && httpMethod != "GET" {
contentType = `"application/json"`
}
pn(`reqHeaders := gensupport.SetHeaders(c.s.userAgent(), %s, c.header_ )`, contentType)
apiVersion := meth.m.APIVersion
if apiVersion == "" {
pn(`reqHeaders := gensupport.SetHeaders(c.s.userAgent(), %s, c.header_)`, contentType)
} else {
pn(`reqHeaders := gensupport.SetHeaders(c.s.userAgent(), %s, c.header_, "x-goog-api-version", %q)`, contentType, apiVersion)
}
if httpMethod == "GET" {
pn(`if c.ifNoneMatch_ != "" {`)
pn(` reqHeaders.Set("If-None-Match", c.ifNoneMatch_)`)
Expand Down
1 change: 1 addition & 0 deletions google-api-go-generator/internal/disco/disco.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ type Method struct {
Scopes []string
MediaUpload *MediaUpload
SupportsMediaDownload bool
APIVersion string

JSONMap map[string]interface{} `json:"-"`
}
Expand Down
15 changes: 15 additions & 0 deletions google-api-go-generator/testdata/any.json
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@
"list": {
"id": "logging.projects.logs.list",
"path": "v1beta3/projects/{projectsId}/logs",
"apiVersion": "v1_20240501",
"httpMethod": "GET",
"description": "Lists log resources belonging to the specified project.",
"parameters": {
Expand Down Expand Up @@ -477,6 +478,7 @@
"delete": {
"id": "logging.projects.logs.delete",
"path": "v1beta3/projects/{projectsId}/logs/{logsId}",
"apiVersion": "v1_20240501",
"httpMethod": "DELETE",
"description": "Deletes the specified log resource and all log entries contained in it.",
"parameters": {
Expand Down Expand Up @@ -511,6 +513,7 @@
"write": {
"id": "logging.projects.logs.entries.write",
"path": "v1beta3/projects/{projectsId}/logs/{logsId}/entries:write",
"apiVersion": "v1_20240501",
"httpMethod": "POST",
"description": "Creates one or more log entries in a log. You must supply a list of `LogEntry` objects, named `entries`. Each `LogEntry` object must contain a payload object and a `LogEntryMetadata` object that describes the entry. You must fill in all the fields of the entry, metadata, and payload. You can also supply a map, `commonLabels`, that supplies default (key, value) data for the `entries[].metadata.labels` maps, saving you the trouble of creating identical copies for each entry.",
"parameters": {
Expand Down Expand Up @@ -548,6 +551,7 @@
"list": {
"id": "logging.projects.logs.sinks.list",
"path": "v1beta3/projects/{projectsId}/logs/{logsId}/sinks",
"apiVersion": "v1_20240501",
"httpMethod": "GET",
"description": "Lists log sinks associated with the specified log.",
"parameters": {
Expand Down Expand Up @@ -578,6 +582,7 @@
"get": {
"id": "logging.projects.logs.sinks.get",
"path": "v1beta3/projects/{projectsId}/logs/{logsId}/sinks/{sinksId}",
"apiVersion": "v1_20240501",
"httpMethod": "GET",
"description": "Gets the specified log sink resource.",
"parameters": {
Expand Down Expand Up @@ -615,6 +620,7 @@
"create": {
"id": "logging.projects.logs.sinks.create",
"path": "v1beta3/projects/{projectsId}/logs/{logsId}/sinks",
"apiVersion": "v1_20240501",
"httpMethod": "POST",
"description": "Creates the specified log sink resource.",
"parameters": {
Expand Down Expand Up @@ -648,6 +654,7 @@
"update": {
"id": "logging.projects.logs.sinks.update",
"path": "v1beta3/projects/{projectsId}/logs/{logsId}/sinks/{sinksId}",
"apiVersion": "v1_20240501",
"httpMethod": "PUT",
"description": "Creates or updates the specified log sink resource.",
"parameters": {
Expand Down Expand Up @@ -688,6 +695,7 @@
"delete": {
"id": "logging.projects.logs.sinks.delete",
"path": "v1beta3/projects/{projectsId}/logs/{logsId}/sinks/{sinksId}",
"apiVersion": "v1_20240501",
"httpMethod": "DELETE",
"description": "Deletes the specified log sink resource.",
"parameters": {
Expand Down Expand Up @@ -731,6 +739,7 @@
"list": {
"id": "logging.projects.logServices.list",
"path": "v1beta3/projects/{projectsId}/logServices",
"apiVersion": "v1_20240501",
"httpMethod": "GET",
"description": "Lists log services associated with log entries ingested for a project.",
"parameters": {
Expand Down Expand Up @@ -774,6 +783,7 @@
"list": {
"id": "logging.projects.logServices.indexes.list",
"path": "v1beta3/projects/{projectsId}/logServices/{logServicesId}/indexes",
"apiVersion": "v1_20240501",
"httpMethod": "GET",
"description": "Lists log service indexes associated with a log service.",
"parameters": {
Expand Down Expand Up @@ -835,6 +845,7 @@
"list": {
"id": "logging.projects.logServices.sinks.list",
"path": "v1beta3/projects/{projectsId}/logServices/{logServicesId}/sinks",
"apiVersion": "v1_20240501",
"httpMethod": "GET",
"description": "Lists log service sinks associated with the specified service.",
"parameters": {
Expand Down Expand Up @@ -865,6 +876,7 @@
"get": {
"id": "logging.projects.logServices.sinks.get",
"path": "v1beta3/projects/{projectsId}/logServices/{logServicesId}/sinks/{sinksId}",
"apiVersion": "v1_20240501",
"httpMethod": "GET",
"description": "Gets the specified log service sink resource.",
"parameters": {
Expand Down Expand Up @@ -902,6 +914,7 @@
"create": {
"id": "logging.projects.logServices.sinks.create",
"path": "v1beta3/projects/{projectsId}/logServices/{logServicesId}/sinks",
"apiVersion": "v1_20240501",
"httpMethod": "POST",
"description": "Creates the specified log service sink resource.",
"parameters": {
Expand Down Expand Up @@ -935,6 +948,7 @@
"update": {
"id": "logging.projects.logServices.sinks.update",
"path": "v1beta3/projects/{projectsId}/logServices/{logServicesId}/sinks/{sinksId}",
"apiVersion": "v1_20240501",
"httpMethod": "PUT",
"description": "Creates or update the specified log service sink resource.",
"parameters": {
Expand Down Expand Up @@ -975,6 +989,7 @@
"delete": {
"id": "logging.projects.logServices.sinks.delete",
"path": "v1beta3/projects/{projectsId}/logServices/{logServicesId}/sinks/{sinksId}",
"apiVersion": "v1_20240501",
"httpMethod": "DELETE",
"description": "Deletes the specified log service sink.",
"parameters": {
Expand Down
30 changes: 15 additions & 15 deletions google-api-go-generator/testdata/any.want
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ func (c *ProjectsLogServicesListCall) Header() http.Header {
}

func (c *ProjectsLogServicesListCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_, "x-goog-api-version", "v1_20240501")
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
Expand Down Expand Up @@ -932,7 +932,7 @@ func (c *ProjectsLogServicesIndexesListCall) Header() http.Header {
}

func (c *ProjectsLogServicesIndexesListCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_, "x-goog-api-version", "v1_20240501")
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
Expand Down Expand Up @@ -1059,7 +1059,7 @@ func (c *ProjectsLogServicesSinksCreateCall) Header() http.Header {
}

func (c *ProjectsLogServicesSinksCreateCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "application/json", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "application/json", c.header_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
body, err := googleapi.WithoutDataWrapper.JSONReader(c.logsink)
if err != nil {
Expand Down Expand Up @@ -1165,7 +1165,7 @@ func (c *ProjectsLogServicesSinksDeleteCall) Header() http.Header {
}

func (c *ProjectsLogServicesSinksDeleteCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
Expand Down Expand Up @@ -1277,7 +1277,7 @@ func (c *ProjectsLogServicesSinksGetCall) Header() http.Header {
}

func (c *ProjectsLogServicesSinksGetCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_, "x-goog-api-version", "v1_20240501")
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
Expand Down Expand Up @@ -1390,7 +1390,7 @@ func (c *ProjectsLogServicesSinksListCall) Header() http.Header {
}

func (c *ProjectsLogServicesSinksListCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_, "x-goog-api-version", "v1_20240501")
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
Expand Down Expand Up @@ -1498,7 +1498,7 @@ func (c *ProjectsLogServicesSinksUpdateCall) Header() http.Header {
}

func (c *ProjectsLogServicesSinksUpdateCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "application/json", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "application/json", c.header_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
body, err := googleapi.WithoutDataWrapper.JSONReader(c.logsink)
if err != nil {
Expand Down Expand Up @@ -1603,7 +1603,7 @@ func (c *ProjectsLogsDeleteCall) Header() http.Header {
}

func (c *ProjectsLogsDeleteCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
Expand Down Expand Up @@ -1749,7 +1749,7 @@ func (c *ProjectsLogsListCall) Header() http.Header {
}

func (c *ProjectsLogsListCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_, "x-goog-api-version", "v1_20240501")
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
Expand Down Expand Up @@ -1881,7 +1881,7 @@ func (c *ProjectsLogsEntriesWriteCall) Header() http.Header {
}

func (c *ProjectsLogsEntriesWriteCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "application/json", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "application/json", c.header_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
body, err := googleapi.WithoutDataWrapper.JSONReader(c.writelogentriesrequest)
if err != nil {
Expand Down Expand Up @@ -1987,7 +1987,7 @@ func (c *ProjectsLogsSinksCreateCall) Header() http.Header {
}

func (c *ProjectsLogsSinksCreateCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "application/json", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "application/json", c.header_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
body, err := googleapi.WithoutDataWrapper.JSONReader(c.logsink)
if err != nil {
Expand Down Expand Up @@ -2093,7 +2093,7 @@ func (c *ProjectsLogsSinksDeleteCall) Header() http.Header {
}

func (c *ProjectsLogsSinksDeleteCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
Expand Down Expand Up @@ -2205,7 +2205,7 @@ func (c *ProjectsLogsSinksGetCall) Header() http.Header {
}

func (c *ProjectsLogsSinksGetCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_, "x-goog-api-version", "v1_20240501")
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
Expand Down Expand Up @@ -2317,7 +2317,7 @@ func (c *ProjectsLogsSinksListCall) Header() http.Header {
}

func (c *ProjectsLogsSinksListCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "", c.header_, "x-goog-api-version", "v1_20240501")
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
Expand Down Expand Up @@ -2425,7 +2425,7 @@ func (c *ProjectsLogsSinksUpdateCall) Header() http.Header {
}

func (c *ProjectsLogsSinksUpdateCall) doRequest(alt string) (*http.Response, error) {
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "application/json", c.header_)
reqHeaders := gensupport.SetHeaders(c.s.userAgent(), "application/json", c.header_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
body, err := googleapi.WithoutDataWrapper.JSONReader(c.logsink)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions internal/gensupport/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ func SetOptions(u URLParams, opts ...googleapi.CallOption) {
}
}

// SetHeaders sets common headers for all requests.
func SetHeaders(userAgent, contentType string, userHeaders http.Header) http.Header {
// SetHeaders sets common headers for all requests. The keyvals header pairs
// should have a corresponding value for every key provided. If there is an odd
// number of keyvals this method will panic.
func SetHeaders(userAgent, contentType string, userHeaders http.Header, keyvals ...string) http.Header {
reqHeaders := make(http.Header)
reqHeaders.Set("x-goog-api-client", "gl-go/"+GoVersion()+" gdcl/"+internal.Version)
for i := 0; i < len(keyvals); i = i + 2 {
reqHeaders.Set(keyvals[i], keyvals[i+1])
}
reqHeaders.Set("User-Agent", userAgent)
if contentType != "" {
reqHeaders.Set("Content-Type", contentType)
Expand Down
13 changes: 13 additions & 0 deletions internal/gensupport/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gensupport

import (
"net/http"
"testing"

"google.golang.org/api/googleapi"
Expand All @@ -18,3 +19,15 @@ func TestSetOptionsGetMulti(t *testing.T) {
t.Fatalf("URLParams.Encode() = %q, want %q", got, want)
}
}

func TestSetHeaders(t *testing.T) {
userAgent := "google-api-go-client/123"
contentType := "application/json"
userHeaders := make(http.Header)
userHeaders.Set("baz", "300")
got := SetHeaders(userAgent, contentType, userHeaders, "foo", "100", "bar", "200")

if len(got) != 6 {
t.Fatalf("SetHeaders() = %q, want len(6)", got)
}
}

0 comments on commit fe54ffd

Please sign in to comment.