Skip to content

Commit b947c65

Browse files
authored
fix: have a helper that returns a http Response as well (#1594)
When a rpc returns an httpBody, see https://pkg.go.dev/google.golang.org/genproto/googleapis/api/httpbody#HttpBody, we need to return a response from the helper so we can examine the headers.
1 parent 37b423a commit b947c65

File tree

6 files changed

+47
-22
lines changed

6 files changed

+47
-22
lines changed

internal/gengapic/gengapic.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,22 +268,28 @@ func (g *generator) genAndCommitHelpers(scopes []string) error {
268268
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/googleapi"}] = true
269269
g.imports[pbinfo.ImportSpec{Path: "github.com/googleapis/gax-go/v2/internallog"}] = true
270270

271-
p("func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {")
271+
p("func executeHTTPRequestWithResponse(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, *http.Response, error) {")
272272
p(` logger.DebugContext(ctx, "api request", "serviceName", serviceName, "rpcName", rpc, "request", internallog.HTTPRequest(req, body))`)
273273
p(" resp, err := client.Do(req)")
274274
p(" if err != nil{")
275-
p(" return nil, err")
275+
p(" return nil, nil, err")
276276
p(" }")
277277
p(" defer resp.Body.Close()")
278278
p(" buf, err := io.ReadAll(resp.Body)")
279279
p(" if err != nil {")
280-
p(" return nil, err")
280+
p(" return nil, nil, err")
281281
p(" }")
282282
p(` logger.DebugContext(ctx, "api response", "serviceName", serviceName, "rpcName", rpc, "response", internallog.HTTPResponse(resp, buf))`)
283283
p(" if err = googleapi.CheckResponse(resp); err != nil {")
284-
p(" return nil, err")
284+
p(" return nil, nil, err")
285285
p(" }")
286-
p(" return buf, nil")
286+
p(" return buf, resp, nil")
287+
p("}")
288+
p("")
289+
290+
p("func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {")
291+
p(" buf, _, err := executeHTTPRequestWithResponse(ctx, client, req, logger, body, rpc)")
292+
p(" return buf, err")
287293
p("}")
288294
p("")
289295

internal/gengapic/genrest.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,11 @@ func (g *generator) unaryRESTCall(servName string, m *descriptorpb.MethodDescrip
11351135
p(" httpReq = httpReq.WithContext(ctx)")
11361136
p(" httpReq.Header = headers")
11371137
p("")
1138-
p(" buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, %s, %q)", logBody, m.GetName())
1138+
if isHTTPBodyMessage {
1139+
p(" buf, httpRsp, err := executeHTTPRequestWithResponse(ctx, c.httpClient, httpReq, c.logger, %s, %q)", logBody, m.GetName())
1140+
} else {
1141+
p(" buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, %s, %q)", logBody, m.GetName())
1142+
}
11391143
p(" if err != nil{")
11401144
p(" return err")
11411145
p(" }")

internal/gengapic/testdata/helpers_default_scope.want

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,27 @@ func DefaultAuthScopes() []string {
2121
}
2222
}
2323

24-
func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
24+
func executeHTTPRequestWithResponse(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, *http.Response, error) {
2525
logger.DebugContext(ctx, "api request", "serviceName", serviceName, "rpcName", rpc, "request", internallog.HTTPRequest(req, body))
2626
resp, err := client.Do(req)
2727
if err != nil{
28-
return nil, err
28+
return nil, nil, err
2929
}
3030
defer resp.Body.Close()
3131
buf, err := io.ReadAll(resp.Body)
3232
if err != nil {
33-
return nil, err
33+
return nil, nil, err
3434
}
3535
logger.DebugContext(ctx, "api response", "serviceName", serviceName, "rpcName", rpc, "response", internallog.HTTPResponse(resp, buf))
3636
if err = googleapi.CheckResponse(resp); err != nil {
37-
return nil, err
37+
return nil, nil, err
3838
}
39-
return buf, nil
39+
return buf, resp, nil
40+
}
41+
42+
func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
43+
buf, _, err := executeHTTPRequestWithResponse(ctx, client, req, logger, body, rpc)
44+
return buf, err
4045
}
4146

4247
func executeStreamingHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) (*http.Response, error) {

internal/gengapic/testdata/helpers_multiple_scopes.want

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,27 @@ func DefaultAuthScopes() []string {
2323
}
2424
}
2525

26-
func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
26+
func executeHTTPRequestWithResponse(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, *http.Response, error) {
2727
logger.DebugContext(ctx, "api request", "serviceName", serviceName, "rpcName", rpc, "request", internallog.HTTPRequest(req, body))
2828
resp, err := client.Do(req)
2929
if err != nil{
30-
return nil, err
30+
return nil, nil, err
3131
}
3232
defer resp.Body.Close()
3333
buf, err := io.ReadAll(resp.Body)
3434
if err != nil {
35-
return nil, err
35+
return nil, nil, err
3636
}
3737
logger.DebugContext(ctx, "api response", "serviceName", serviceName, "rpcName", rpc, "response", internallog.HTTPResponse(resp, buf))
3838
if err = googleapi.CheckResponse(resp); err != nil {
39-
return nil, err
39+
return nil, nil, err
4040
}
41-
return buf, nil
41+
return buf, resp, nil
42+
}
43+
44+
func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
45+
buf, _, err := executeHTTPRequestWithResponse(ctx, client, req, logger, body, rpc)
46+
return buf, err
4247
}
4348

4449
func executeStreamingHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) (*http.Response, error) {

internal/gengapic/testdata/helpers_no_scopes.want

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,27 @@ func DefaultAuthScopes() []string {
2020
}
2121
}
2222

23-
func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
23+
func executeHTTPRequestWithResponse(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, *http.Response, error) {
2424
logger.DebugContext(ctx, "api request", "serviceName", serviceName, "rpcName", rpc, "request", internallog.HTTPRequest(req, body))
2525
resp, err := client.Do(req)
2626
if err != nil{
27-
return nil, err
27+
return nil, nil, err
2828
}
2929
defer resp.Body.Close()
3030
buf, err := io.ReadAll(resp.Body)
3131
if err != nil {
32-
return nil, err
32+
return nil, nil, err
3333
}
3434
logger.DebugContext(ctx, "api response", "serviceName", serviceName, "rpcName", rpc, "response", internallog.HTTPResponse(resp, buf))
3535
if err = googleapi.CheckResponse(resp); err != nil {
36-
return nil, err
36+
return nil, nil, err
3737
}
38-
return buf, nil
38+
return buf, resp, nil
39+
}
40+
41+
func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
42+
buf, _, err := executeHTTPRequestWithResponse(ctx, client, req, logger, body, rpc)
43+
return buf, err
3944
}
4045

4146
func executeStreamingHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) (*http.Response, error) {

internal/gengapic/testdata/rest_HttpBodyRPC.want

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *fooRESTClient) HttpBodyRPC(ctx context.Context, req *foopb.Foo, opts ..
3939
httpReq = httpReq.WithContext(ctx)
4040
httpReq.Header = headers
4141

42-
buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "HttpBodyRPC")
42+
buf, httpRsp, err := executeHTTPRequestWithResponse(ctx, c.httpClient, httpReq, c.logger, jsonReq, "HttpBodyRPC")
4343
if err != nil{
4444
return err
4545
}

0 commit comments

Comments
 (0)