@@ -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(context.Background(), 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 )
@@ -2279,34 +2282,44 @@ func (meth *Method) generateCode() {
2279
2282
pn (` reqHeaders.Set("If-None-Match", c.ifNoneMatch_)` )
2280
2283
pn ("}" )
2281
2284
}
2282
- pn ( " var body io.Reader = nil" )
2285
+ var hasBody bool
2283
2286
if meth .IsRawRequest () {
2284
- pn ("body = c.body_" )
2287
+ pn ("body := bytes.NewBuffer(nil)" )
2288
+ pn ("_, err := body.ReadFrom(c.body_)" )
2289
+ pn ("if err != nil { return nil, err }" )
2290
+ hasBody = true
2285
2291
} else if meth .IsProtoStructRequest () {
2286
2292
pn ("protoBytes, err := json.Marshal(c.req)" )
2287
2293
pn ("if err != nil { return nil, err }" )
2288
- pn ("body = bytes.NewReader(protoBytes)" )
2294
+ pn ("body := bytes.NewReader(protoBytes)" )
2295
+ hasBody = true
2289
2296
} else {
2290
2297
if ba := args .bodyArg (); ba != nil && httpMethod != "GET" {
2291
2298
if meth .m .ID == "ml.projects.predict" {
2292
2299
// 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 )
2300
+ // Skip JSONBuffer for APIs that require clients to pass in JSON already.
2301
+ pn ("body := bytes.NewBufferString (c.%s.HttpBody.Data)" , ba .goname )
2295
2302
} else {
2296
2303
style := "WithoutDataWrapper"
2297
2304
if a .needsDataWrapper () {
2298
2305
style = "WithDataWrapper"
2299
2306
}
2300
- pn ("body, err := googleapi.%s.JSONReader (c.%s)" , style , ba .goname )
2307
+ pn ("body, err := googleapi.%s.JSONBuffer (c.%s)" , style , ba .goname )
2301
2308
pn ("if err != nil { return nil, err }" )
2302
2309
}
2310
+ hasBody = true
2303
2311
}
2304
2312
pn (`c.urlParams_.Set("alt", alt)` )
2305
2313
pn (`c.urlParams_.Set("prettyPrint", "false")` )
2306
2314
}
2315
+ body := "nil"
2316
+ if hasBody {
2317
+ body = body
2318
+ }
2307
2319
2308
2320
pn ("urls := googleapi.ResolveRelative(c.s.BasePath, %q)" , meth .m .Path )
2309
2321
if meth .supportsMediaUpload () {
2322
+ body = "newBody"
2310
2323
pn ("if c.mediaInfo_ != nil {" )
2311
2324
pn (" urls = googleapi.ResolveRelative(c.s.BasePath, %q)" , meth .mediaUploadPath ())
2312
2325
pn (` c.urlParams_.Set("uploadType", c.mediaInfo_.UploadType())` )
@@ -2316,11 +2329,11 @@ func (meth *Method) generateCode() {
2316
2329
pn (" body = new(bytes.Buffer)" )
2317
2330
pn (` reqHeaders.Set("Content-Type", "application/json")` )
2318
2331
pn ("}" )
2319
- pn ("body , getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)" )
2332
+ pn ("newBody , getBody, cleanup := c.mediaInfo_.UploadRequest(reqHeaders, body)" )
2320
2333
pn ("defer cleanup()" )
2321
2334
}
2322
2335
pn (`urls += "?" + c.urlParams_.Encode()` )
2323
- pn ("req, err := http.NewRequest(%q, urls, body )" , httpMethod )
2336
+ pn ("req, err := http.NewRequest(%q, urls, %s )" , httpMethod , body )
2324
2337
pn ("if err != nil { return nil, err }" )
2325
2338
pn ("req.Header = reqHeaders" )
2326
2339
if meth .supportsMediaUpload () {
@@ -2337,12 +2350,18 @@ func (meth *Method) generateCode() {
2337
2350
}
2338
2351
pn (`})` )
2339
2352
}
2353
+ logBody := "nil"
2354
+ if hasBody {
2355
+ logBody = "body.Bytes()"
2356
+ }
2340
2357
if meth .supportsMediaUpload () && meth .api .Name == "storage" {
2358
+ pn (`c.s.logger.DebugContext(c.ctx_, "api request", "serviceName", apiName, "rpcName", %q, "request", internallog.HTTPRequest(req, %s))` , meth .Id (), logBody )
2341
2359
pn ("if c.retry != nil {" )
2342
2360
pn (" return gensupport.SendRequestWithRetry(c.ctx_, c.s.client, req, c.retry)" )
2343
2361
pn ("}" )
2344
2362
pn ("return gensupport.SendRequest(c.ctx_, c.s.client, req)" )
2345
2363
} else {
2364
+ pn (`c.s.logger.DebugContext(c.ctx_, "api request", "serviceName", apiName, "rpcName", %q, "request", internallog.HTTPRequest(req, %s))` , meth .Id (), logBody )
2346
2365
pn ("return gensupport.SendRequest(c.ctx_, c.s.client, req)" )
2347
2366
}
2348
2367
pn ("}" )
@@ -2421,6 +2440,7 @@ func (meth *Method) generateCode() {
2421
2440
pn ("}" )
2422
2441
}
2423
2442
if retTypeComma == "" {
2443
+ pn (`c.s.logger.DebugContext(c.ctx_, "api response", "serviceName", apiName, "rpcName", %q, "response", internallog.HTTPResponse(res, nil))` , meth .Id ())
2424
2444
pn ("return nil" )
2425
2445
} else {
2426
2446
if meth .IsProtoStructResponse () {
@@ -2449,8 +2469,11 @@ func (meth *Method) generateCode() {
2449
2469
pn ("if err := res.Body.Close(); err != nil { return nil, err }" )
2450
2470
pn ("if err := json.NewDecoder(bytes.NewReader(b.Bytes())).Decode(target); err != nil { return nil, err }" )
2451
2471
pn ("ret.Data = b.String()" )
2472
+ pn (`c.s.logger.DebugContext(c.ctx_, "api response", "serviceName", apiName, "rpcName", %q, "response", internallog.HTTPResponse(res, b.Bytes()))` , meth .Id ())
2452
2473
} else {
2453
- pn ("if err := gensupport.DecodeResponse(target, res); err != nil { return nil, err }" )
2474
+ pn ("b, err := gensupport.DecodeResponseBytes(target, res)" )
2475
+ pn ("if err != nil { return nil, err }" )
2476
+ pn (`c.s.logger.DebugContext(c.ctx_, "api response", "serviceName", apiName, "rpcName", %q, "response", internallog.HTTPResponse(res, b))` , meth .Id ())
2454
2477
}
2455
2478
pn ("return ret, nil" )
2456
2479
}
0 commit comments