Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 11 additions & 5 deletions internal/gengapic/gengapic.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,22 +268,28 @@ func (g *generator) genAndCommitHelpers(scopes []string) error {
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/googleapi"}] = true
g.imports[pbinfo.ImportSpec{Path: "github.com/googleapis/gax-go/v2/internallog"}] = true

p("func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {")
p("func executeHTTPRequestWithResponse(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, *http.Response, error) {")
p(` logger.DebugContext(ctx, "api request", "serviceName", serviceName, "rpcName", rpc, "request", internallog.HTTPRequest(req, body))`)
p(" resp, err := client.Do(req)")
p(" if err != nil{")
p(" return nil, err")
p(" return nil, nil, err")
p(" }")
p(" defer resp.Body.Close()")
p(" buf, err := io.ReadAll(resp.Body)")
p(" if err != nil {")
p(" return nil, err")
p(" return nil, nil, err")
p(" }")
p(` logger.DebugContext(ctx, "api response", "serviceName", serviceName, "rpcName", rpc, "response", internallog.HTTPResponse(resp, buf))`)
p(" if err = googleapi.CheckResponse(resp); err != nil {")
p(" return nil, err")
p(" return nil, nil, err")
p(" }")
p(" return buf, nil")
p(" return buf, resp, nil")
p("}")
p("")

p("func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {")
p(" buf, _, err := executeHTTPRequestWithResponse(ctx, client, req, logger, body, rpc)")
p(" return buf, err")
p("}")
p("")

Expand Down
6 changes: 5 additions & 1 deletion internal/gengapic/genrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,11 @@ func (g *generator) unaryRESTCall(servName string, m *descriptorpb.MethodDescrip
p(" httpReq = httpReq.WithContext(ctx)")
p(" httpReq.Header = headers")
p("")
p(" buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, %s, %q)", logBody, m.GetName())
if isHTTPBodyMessage {
p(" buf, httpRsp, err := executeHTTPRequestWithResponse(ctx, c.httpClient, httpReq, c.logger, %s, %q)", logBody, m.GetName())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does httpRsp get used in the client?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would direct you to the updated generated file: internal/gengapic/testdata/rest_HttpBodyRPC.want

Copy link
Member

@quartzmo quartzmo Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, when I look at the whole file I see the usage:

if headers := httpRsp.Header; len(headers["Content-Type"]) > 0 {
			resp.ContentType = headers["Content-Type"][0]
		}

But it doesn't show up in the diff. Odd that this usage existed before.

} else {
p(" buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, %s, %q)", logBody, m.GetName())
}
p(" if err != nil{")
p(" return err")
p(" }")
Expand Down
15 changes: 10 additions & 5 deletions internal/gengapic/testdata/helpers_default_scope.want
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@ func DefaultAuthScopes() []string {
}
}

func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
func executeHTTPRequestWithResponse(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, *http.Response, error) {
logger.DebugContext(ctx, "api request", "serviceName", serviceName, "rpcName", rpc, "request", internallog.HTTPRequest(req, body))
resp, err := client.Do(req)
if err != nil{
return nil, err
return nil, nil, err
}
defer resp.Body.Close()
buf, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
return nil, nil, err
}
logger.DebugContext(ctx, "api response", "serviceName", serviceName, "rpcName", rpc, "response", internallog.HTTPResponse(resp, buf))
if err = googleapi.CheckResponse(resp); err != nil {
return nil, err
return nil, nil, err
}
return buf, nil
return buf, resp, nil
}

func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
buf, _, err := executeHTTPRequestWithResponse(ctx, client, req, logger, body, rpc)
return buf, err
}

func executeStreamingHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) (*http.Response, error) {
Expand Down
15 changes: 10 additions & 5 deletions internal/gengapic/testdata/helpers_multiple_scopes.want
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ func DefaultAuthScopes() []string {
}
}

func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
func executeHTTPRequestWithResponse(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, *http.Response, error) {
logger.DebugContext(ctx, "api request", "serviceName", serviceName, "rpcName", rpc, "request", internallog.HTTPRequest(req, body))
resp, err := client.Do(req)
if err != nil{
return nil, err
return nil, nil, err
}
defer resp.Body.Close()
buf, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
return nil, nil, err
}
logger.DebugContext(ctx, "api response", "serviceName", serviceName, "rpcName", rpc, "response", internallog.HTTPResponse(resp, buf))
if err = googleapi.CheckResponse(resp); err != nil {
return nil, err
return nil, nil, err
}
return buf, nil
return buf, resp, nil
}

func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
buf, _, err := executeHTTPRequestWithResponse(ctx, client, req, logger, body, rpc)
return buf, err
}

func executeStreamingHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) (*http.Response, error) {
Expand Down
15 changes: 10 additions & 5 deletions internal/gengapic/testdata/helpers_no_scopes.want
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@ func DefaultAuthScopes() []string {
}
}

func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
func executeHTTPRequestWithResponse(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, *http.Response, error) {
logger.DebugContext(ctx, "api request", "serviceName", serviceName, "rpcName", rpc, "request", internallog.HTTPRequest(req, body))
resp, err := client.Do(req)
if err != nil{
return nil, err
return nil, nil, err
}
defer resp.Body.Close()
buf, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
return nil, nil, err
}
logger.DebugContext(ctx, "api response", "serviceName", serviceName, "rpcName", rpc, "response", internallog.HTTPResponse(resp, buf))
if err = googleapi.CheckResponse(resp); err != nil {
return nil, err
return nil, nil, err
}
return buf, nil
return buf, resp, nil
}

func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
buf, _, err := executeHTTPRequestWithResponse(ctx, client, req, logger, body, rpc)
return buf, err
}

func executeStreamingHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) (*http.Response, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/gengapic/testdata/rest_HttpBodyRPC.want
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *fooRESTClient) HttpBodyRPC(ctx context.Context, req *foopb.Foo, opts ..
httpReq = httpReq.WithContext(ctx)
httpReq.Header = headers

buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "HttpBodyRPC")
buf, httpRsp, err := executeHTTPRequestWithResponse(ctx, c.httpClient, httpReq, c.logger, jsonReq, "HttpBodyRPC")
if err != nil{
return err
}
Expand Down
Loading