@@ -809,6 +809,7 @@ func (a *API) GenerateCode() ([]byte, error) {
809
809
"errors" ,
810
810
"fmt" ,
811
811
"io" ,
812
+ "log/slog" ,
812
813
"net/http" ,
813
814
"net/url" ,
814
815
"strconv" ,
@@ -820,6 +821,7 @@ func (a *API) GenerateCode() ([]byte, error) {
820
821
if a .Name == "storage" {
821
822
pn (" %q" , "github.com/googleapis/gax-go/v2" )
822
823
}
824
+ pn (" %q" , "github.com/googleapis/gax-go/v2/internallog" )
823
825
for _ , imp := range []struct {
824
826
pkg string
825
827
lname string
@@ -895,7 +897,10 @@ func (a *API) GenerateCode() ([]byte, error) {
895
897
896
898
pn ("client, endpoint, err := htransport.NewClient(ctx, opts...)" )
897
899
pn ("if err != nil { return nil, err }" )
898
- pn ("s, err := New(client)" )
900
+ pn ("s := &%s{client: client, BasePath: basePath, logger: internaloption.GetLogger(opts)}" , service )
901
+ for _ , res := range a .doc .Resources { // add top level resources.
902
+ pn ("s.%s = New%s(s)" , resourceGoField (res , nil ), resourceGoType (res ))
903
+ }
899
904
pn ("if err != nil { return nil, err }" )
900
905
pn (`if endpoint != "" { s.BasePath = endpoint }` )
901
906
pn ("return s, nil" )
@@ -908,15 +913,12 @@ func (a *API) GenerateCode() ([]byte, error) {
908
913
pn ("// If you are using google.golang.org/api/googleapis/transport.APIKey, use option.WithAPIKey with NewService instead." )
909
914
pn ("func New(client *http.Client) (*%s, error) {" , service )
910
915
pn ("if client == nil { return nil, errors.New(\" client is nil\" ) }" )
911
- pn ("s := &%s{client: client, BasePath: basePath}" , service )
912
- for _ , res := range a .doc .Resources { // add top level resources.
913
- pn ("s.%s = New%s(s)" , resourceGoField (res , nil ), resourceGoType (res ))
914
- }
915
- pn ("return s, nil" )
916
+ pn ("return NewService(option.WithHTTPClient(client))" )
916
917
pn ("}" )
917
918
918
919
pn ("\n type %s struct {" , service )
919
920
pn (" client *http.Client" )
921
+ pn (" logger *slog.Logger" )
920
922
pn (" BasePath string // API endpoint base URL" )
921
923
pn (" UserAgent string // optional additional User-Agent fragment" )
922
924
@@ -995,6 +997,7 @@ func splitFileHeading(w io.Writer, pkg string) {
995
997
"context" ,
996
998
"fmt" ,
997
999
"io" ,
1000
+ "log/slog" ,
998
1001
"net/http" ,
999
1002
} {
1000
1003
pn (" %q" , imp )
@@ -2280,26 +2283,32 @@ func (meth *Method) generateCode() {
2280
2283
pn ("}" )
2281
2284
}
2282
2285
pn ("var body io.Reader = nil" )
2286
+ var hasBody bool
2283
2287
if meth .IsRawRequest () {
2284
- pn ("body = c.body_" )
2288
+ pn ("body := bytes.NewBuffer(nil)" )
2289
+ pn ("_, err := body.ReadFrom(c.body_)" )
2290
+ pn ("if err != nil { return nil, err }" )
2291
+ hasBody = true
2285
2292
} else if meth .IsProtoStructRequest () {
2286
2293
pn ("protoBytes, err := json.Marshal(c.req)" )
2287
2294
pn ("if err != nil { return nil, err }" )
2288
- pn ("body = bytes.NewReader(protoBytes)" )
2295
+ pn ("body := bytes.NewReader(protoBytes)" )
2296
+ hasBody = true
2289
2297
} else {
2290
2298
if ba := args .bodyArg (); ba != nil && httpMethod != "GET" {
2291
2299
if meth .m .ID == "ml.projects.predict" {
2292
2300
// TODO(cbro): move ML API to rawHTTP (it will be a breaking change)
2293
- // Skip JSONReader for APIs that require clients to pass in JSON already.
2294
- pn ("body = strings.NewReader (c.%s.HttpBody.Data)" , ba .goname )
2301
+ // Skip JSONBuffer for APIs that require clients to pass in JSON already.
2302
+ pn ("body := bytes.NewBufferString (c.%s.HttpBody.Data)" , ba .goname )
2295
2303
} else {
2296
2304
style := "WithoutDataWrapper"
2297
2305
if a .needsDataWrapper () {
2298
2306
style = "WithDataWrapper"
2299
2307
}
2300
- pn ("body, err := googleapi.%s.JSONReader (c.%s)" , style , ba .goname )
2308
+ pn ("body, err := googleapi.%s.JSONBuffer (c.%s)" , style , ba .goname )
2301
2309
pn ("if err != nil { return nil, err }" )
2302
2310
}
2311
+ hasBody = true
2303
2312
}
2304
2313
pn (`c.urlParams_.Set("alt", alt)` )
2305
2314
pn (`c.urlParams_.Set("prettyPrint", "false")` )
@@ -2337,12 +2346,18 @@ func (meth *Method) generateCode() {
2337
2346
}
2338
2347
pn (`})` )
2339
2348
}
2349
+ logBody := "nil"
2350
+ if hasBody {
2351
+ logBody = "body.Bytes()"
2352
+ }
2340
2353
if meth .supportsMediaUpload () && meth .api .Name == "storage" {
2354
+ pn (`c.s.logger.DebugContext(c.ctx_, "api request", "serviceName", apiName, "rpcName", %q, "request", internallog.HTTPRequest(req, %s))` , meth .Id (), logBody )
2341
2355
pn ("if c.retry != nil {" )
2342
2356
pn (" return gensupport.SendRequestWithRetry(c.ctx_, c.s.client, req, c.retry)" )
2343
2357
pn ("}" )
2344
2358
pn ("return gensupport.SendRequest(c.ctx_, c.s.client, req)" )
2345
2359
} else {
2360
+ pn (`c.s.logger.DebugContext(c.ctx_, "api request", "serviceName", apiName, "rpcName", %q, "request", internallog.HTTPRequest(req, %s))` , meth .Id (), logBody )
2346
2361
pn ("return gensupport.SendRequest(c.ctx_, c.s.client, req)" )
2347
2362
}
2348
2363
pn ("}" )
@@ -2421,6 +2436,7 @@ func (meth *Method) generateCode() {
2421
2436
pn ("}" )
2422
2437
}
2423
2438
if retTypeComma == "" {
2439
+ pn (`c.s.logger.DebugContext(c.ctx_, "api response", "serviceName", apiName, "rpcName", %q, "response", internallog.HTTPResponse(res, nil))` , meth .Id ())
2424
2440
pn ("return nil" )
2425
2441
} else {
2426
2442
if meth .IsProtoStructResponse () {
@@ -2449,8 +2465,11 @@ func (meth *Method) generateCode() {
2449
2465
pn ("if err := res.Body.Close(); err != nil { return nil, err }" )
2450
2466
pn ("if err := json.NewDecoder(bytes.NewReader(b.Bytes())).Decode(target); err != nil { return nil, err }" )
2451
2467
pn ("ret.Data = b.String()" )
2468
+ pn (`c.s.logger.DebugContext(c.ctx_, "api response", "serviceName", apiName, "rpcName", %q, "response", internallog.HTTPResponse(res, b.Bytes()))` , meth .Id ())
2452
2469
} else {
2453
- pn ("if err := gensupport.DecodeResponse(target, res); err != nil { return nil, err }" )
2470
+ pn ("b, err := gensupport.DecodeResponseBytes(target, res)" )
2471
+ pn ("if err != nil { return nil, err }" )
2472
+ pn (`c.s.logger.DebugContext(c.ctx_, "api response", "serviceName", apiName, "rpcName", %q, "response", internallog.HTTPResponse(res, b))` , meth .Id ())
2454
2473
}
2455
2474
pn ("return ret, nil" )
2456
2475
}
0 commit comments