Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: adapt code to get ready for adding logging #2914

Merged
merged 5 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ go.opentelemetry.io/otel/trace v1.22.0/go.mod h1:RbbHXVqKES9QhzZq/fE5UnOSILqRt40
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
44 changes: 24 additions & 20 deletions google-api-go-generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,10 @@ func (a *API) GenerateCode() ([]byte, error) {

pn("client, endpoint, err := htransport.NewClient(ctx, opts...)")
pn("if err != nil { return nil, err }")
pn("s, err := New(client)")
pn("s := &%s{client: client, BasePath: basePath}", service)
for _, res := range a.doc.Resources { // add top level resources.
pn("s.%s = New%s(s)", resourceGoField(res, nil), resourceGoType(res))
}
pn("if err != nil { return nil, err }")
pn(`if endpoint != "" { s.BasePath = endpoint }`)
pn("return s, nil")
Expand All @@ -908,11 +911,7 @@ func (a *API) GenerateCode() ([]byte, error) {
pn("// If you are using google.golang.org/api/googleapis/transport.APIKey, use option.WithAPIKey with NewService instead.")
pn("func New(client *http.Client) (*%s, error) {", service)
pn("if client == nil { return nil, errors.New(\"client is nil\") }")
pn("s := &%s{client: client, BasePath: basePath}", service)
for _, res := range a.doc.Resources { // add top level resources.
pn("s.%s = New%s(s)", resourceGoField(res, nil), resourceGoType(res))
}
pn("return s, nil")
pn("return NewService(context.Background(), option.WithHTTPClient(client))")
pn("}")

pn("\ntype %s struct {", service)
Expand Down Expand Up @@ -994,7 +993,6 @@ func splitFileHeading(w io.Writer, pkg string) {
for _, imp := range []string{
"context",
"fmt",
"io",
"net/http",
} {
pn(" %q", imp)
Expand Down Expand Up @@ -2265,7 +2263,7 @@ func (meth *Method) generateCode() {

pn("\nfunc (c *%s) doRequest(alt string) (*http.Response, error) {", callName)
var contentType = `""`
if !meth.IsRawRequest() && args.bodyArg() != nil && httpMethod != "GET" {
if !meth.IsRawRequest() && args.bodyArg() != nil && httpMethod != "GET" || meth.supportsMediaUpload() && args.bodyArg() == nil {
codyoss marked this conversation as resolved.
Show resolved Hide resolved
contentType = `"application/json"`
}
apiVersion := meth.m.APIVersion
Expand All @@ -2279,31 +2277,40 @@ func (meth *Method) generateCode() {
pn(` reqHeaders.Set("If-None-Match", c.ifNoneMatch_)`)
pn("}")
}
pn("var body io.Reader = nil")
var hasBody bool
if meth.IsRawRequest() {
pn("body = c.body_")
pn("body := bytes.NewBuffer(nil)")
pn("_, err := body.ReadFrom(c.body_)")
pn("if err != nil { return nil, err }")
hasBody = true
} else if meth.IsProtoStructRequest() {
pn("protoBytes, err := json.Marshal(c.req)")
pn("if err != nil { return nil, err }")
pn("body = bytes.NewReader(protoBytes)")
pn("body := bytes.NewReader(protoBytes)")
hasBody = true
} else {
if ba := args.bodyArg(); ba != nil && httpMethod != "GET" {
if meth.m.ID == "ml.projects.predict" {
// TODO(cbro): move ML API to rawHTTP (it will be a breaking change)
// Skip JSONReader for APIs that require clients to pass in JSON already.
pn("body = strings.NewReader(c.%s.HttpBody.Data)", ba.goname)
// Skip JSONBuffer for APIs that require clients to pass in JSON already.
pn("body := bytes.NewBufferString(c.%s.HttpBody.Data)", ba.goname)
} else {
style := "WithoutDataWrapper"
if a.needsDataWrapper() {
style = "WithDataWrapper"
}
pn("body, err := googleapi.%s.JSONReader(c.%s)", style, ba.goname)
pn("body, err := googleapi.%s.JSONBuffer(c.%s)", style, ba.goname)
pn("if err != nil { return nil, err }")
}
hasBody = true
}
pn(`c.urlParams_.Set("alt", alt)`)
pn(`c.urlParams_.Set("prettyPrint", "false")`)
}
body := "nil"
codyoss marked this conversation as resolved.
Show resolved Hide resolved
if hasBody {
body = "body"
}

pn("urls := googleapi.ResolveRelative(c.s.BasePath, %q)", meth.m.Path)
if meth.supportsMediaUpload() {
Expand All @@ -2312,15 +2319,12 @@ func (meth *Method) generateCode() {
pn(` c.urlParams_.Set("uploadType", c.mediaInfo_.UploadType())`)
pn("}")

pn("if body == nil {")
pn(" body = new(bytes.Buffer)")
pn(` reqHeaders.Set("Content-Type", "application/json")`)
pn("}")
pn("body, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)")
pn("newBody, getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, %s)", body)
pn("defer cleanup()")
body = "newBody"
}
pn(`urls += "?" + c.urlParams_.Encode()`)
pn("req, err := http.NewRequest(%q, urls, body)", httpMethod)
pn("req, err := http.NewRequest(%q, urls, %s)", httpMethod, body)
pn("if err != nil { return nil, err }")
pn("req.Header = reqHeaders")
if meth.supportsMediaUpload() {
Expand Down
52 changes: 18 additions & 34 deletions google-api-go-generator/testdata/any.want
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, err
if err != nil {
return nil, err
}
s, err := New(client)
s := &Service{client: client, BasePath: basePath}
s.Projects = NewProjectsService(s)
if err != nil {
return nil, err
}
Expand All @@ -133,9 +134,7 @@ func New(client *http.Client) (*Service, error) {
if client == nil {
return nil, errors.New("client is nil")
}
s := &Service{client: client, BasePath: basePath}
s.Projects = NewProjectsService(s)
return s, nil
return NewService(context.Background(), option.WithHTTPClient(client))
}

type Service struct {
Expand Down Expand Up @@ -752,12 +751,11 @@ func (c *ProjectsLogServicesListCall) doRequest(alt string) (*http.Response, err
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta3/projects/{projectsId}/logServices")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("GET", urls, body)
req, err := http.NewRequest("GET", urls, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -935,12 +933,11 @@ func (c *ProjectsLogServicesIndexesListCall) doRequest(alt string) (*http.Respon
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta3/projects/{projectsId}/logServices/{logServicesId}/indexes")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("GET", urls, body)
req, err := http.NewRequest("GET", urls, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1059,8 +1056,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_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
body, err := googleapi.WithoutDataWrapper.JSONReader(c.logsink)
body, err := googleapi.WithoutDataWrapper.JSONBuffer(c.logsink)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1165,12 +1161,11 @@ func (c *ProjectsLogServicesSinksDeleteCall) Header() http.Header {

func (c *ProjectsLogServicesSinksDeleteCall) doRequest(alt string) (*http.Response, error) {
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")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta3/projects/{projectsId}/logServices/{logServicesId}/sinks/{sinksId}")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("DELETE", urls, body)
req, err := http.NewRequest("DELETE", urls, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1280,12 +1275,11 @@ func (c *ProjectsLogServicesSinksGetCall) doRequest(alt string) (*http.Response,
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta3/projects/{projectsId}/logServices/{logServicesId}/sinks/{sinksId}")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("GET", urls, body)
req, err := http.NewRequest("GET", urls, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1393,12 +1387,11 @@ func (c *ProjectsLogServicesSinksListCall) doRequest(alt string) (*http.Response
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta3/projects/{projectsId}/logServices/{logServicesId}/sinks")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("GET", urls, body)
req, err := http.NewRequest("GET", urls, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1498,8 +1491,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_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
body, err := googleapi.WithoutDataWrapper.JSONReader(c.logsink)
body, err := googleapi.WithoutDataWrapper.JSONBuffer(c.logsink)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1603,12 +1595,11 @@ func (c *ProjectsLogsDeleteCall) Header() http.Header {

func (c *ProjectsLogsDeleteCall) doRequest(alt string) (*http.Response, error) {
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")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta3/projects/{projectsId}/logs/{logsId}")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("DELETE", urls, body)
req, err := http.NewRequest("DELETE", urls, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1752,12 +1743,11 @@ func (c *ProjectsLogsListCall) doRequest(alt string) (*http.Response, error) {
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta3/projects/{projectsId}/logs")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("GET", urls, body)
req, err := http.NewRequest("GET", urls, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1881,8 +1871,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_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
body, err := googleapi.WithoutDataWrapper.JSONReader(c.writelogentriesrequest)
body, err := googleapi.WithoutDataWrapper.JSONBuffer(c.writelogentriesrequest)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1987,8 +1976,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_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
body, err := googleapi.WithoutDataWrapper.JSONReader(c.logsink)
body, err := googleapi.WithoutDataWrapper.JSONBuffer(c.logsink)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2093,12 +2081,11 @@ func (c *ProjectsLogsSinksDeleteCall) Header() http.Header {

func (c *ProjectsLogsSinksDeleteCall) doRequest(alt string) (*http.Response, error) {
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")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta3/projects/{projectsId}/logs/{logsId}/sinks/{sinksId}")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("DELETE", urls, body)
req, err := http.NewRequest("DELETE", urls, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2208,12 +2195,11 @@ func (c *ProjectsLogsSinksGetCall) doRequest(alt string) (*http.Response, error)
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta3/projects/{projectsId}/logs/{logsId}/sinks/{sinksId}")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("GET", urls, body)
req, err := http.NewRequest("GET", urls, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2320,12 +2306,11 @@ func (c *ProjectsLogsSinksListCall) doRequest(alt string) (*http.Response, error
if c.ifNoneMatch_ != "" {
reqHeaders.Set("If-None-Match", c.ifNoneMatch_)
}
var body io.Reader = nil
c.urlParams_.Set("alt", alt)
c.urlParams_.Set("prettyPrint", "false")
urls := googleapi.ResolveRelative(c.s.BasePath, "v1beta3/projects/{projectsId}/logs/{logsId}/sinks")
urls += "?" + c.urlParams_.Encode()
req, err := http.NewRequest("GET", urls, body)
req, err := http.NewRequest("GET", urls, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2425,8 +2410,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_, "x-goog-api-version", "v1_20240501")
var body io.Reader = nil
body, err := googleapi.WithoutDataWrapper.JSONReader(c.logsink)
body, err := googleapi.WithoutDataWrapper.JSONBuffer(c.logsink)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions google-api-go-generator/testdata/arrayofarray-1.want
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, err
if err != nil {
return nil, err
}
s, err := New(client)
s := &Service{client: client, BasePath: basePath}
if err != nil {
return nil, err
}
Expand All @@ -118,8 +118,7 @@ func New(client *http.Client) (*Service, error) {
if client == nil {
return nil, errors.New("client is nil")
}
s := &Service{client: client, BasePath: basePath}
return s, nil
return NewService(context.Background(), option.WithHTTPClient(client))
}

type Service struct {
Expand Down
5 changes: 2 additions & 3 deletions google-api-go-generator/testdata/arrayofenum.want
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, err
if err != nil {
return nil, err
}
s, err := New(client)
s := &Service{client: client, BasePath: basePath}
if err != nil {
return nil, err
}
Expand All @@ -118,8 +118,7 @@ func New(client *http.Client) (*Service, error) {
if client == nil {
return nil, errors.New("client is nil")
}
s := &Service{client: client, BasePath: basePath}
return s, nil
return NewService(context.Background(), option.WithHTTPClient(client))
}

type Service struct {
Expand Down
5 changes: 2 additions & 3 deletions google-api-go-generator/testdata/arrayofmapofobjects.want
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewService(ctx context.Context, opts ...option.ClientOption) (*Service, err
if err != nil {
return nil, err
}
s, err := New(client)
s := &Service{client: client, BasePath: basePath}
if err != nil {
return nil, err
}
Expand All @@ -118,8 +118,7 @@ func New(client *http.Client) (*Service, error) {
if client == nil {
return nil, errors.New("client is nil")
}
s := &Service{client: client, BasePath: basePath}
return s, nil
return NewService(context.Background(), option.WithHTTPClient(client))
}

type Service struct {
Expand Down
Loading