Debian Codegraph @

apihub/apiv1/helpers.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16
17 package apihub
18
19 import (
20 "context"
21 "fmt"
22 "io"
23 "log/slog"
24 "net/http"
25
26 "github.com/googleapis/gax-go/v2/internallog"
27 "google.golang.org/api/googleapi"
28 "google.golang.org/api/option"
29 "google.golang.org/protobuf/runtime/protoimpl"
30 )
31
32 const serviceName = "apihub.googleapis.com"
33
34 var protoVersion = fmt.Sprintf("1.%d", protoimpl.MaxVersion)
35
36 // For more information on implementing a client constructor hook, see
37 // https://github.com/googleapis/google-cloud-go/wiki/Customizing-constructors.
38 type clientHookParams struct{}
39 type clientHook func(context.Context, clientHookParams) ([]option.ClientOption, error)
40
41 var versionClient string
42
43 func getVersionClient() string {
44 if versionClient == "" {
45 return "UNKNOWN"
46 }
47 return versionClient
48 }
49
50 // DefaultAuthScopes reports the default set of authentication scopes to use with this package.
51 func DefaultAuthScopes() []string {
52 return []string{
53 "https://www.googleapis.com/auth/cloud-platform",
54 }
55 }
56
57 func executeHTTPRequestWithResponse(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, *http.Response, error) {
58 logger.DebugContext(ctx, "api request", "serviceName", serviceName, "rpcName", rpc, "request", internallog.HTTPRequest(req, body))
59 resp, err := client.Do(req)
60 if err != nil {
61 return nil, nil, err
62 }
63 defer resp.Body.Close()
64 buf, err := io.ReadAll(resp.Body)
65 if err != nil {
66 return nil, nil, err
67 }
68 logger.DebugContext(ctx, "api response", "serviceName", serviceName, "rpcName", rpc, "response", internallog.HTTPResponse(resp, buf))
69 if err = googleapi.CheckResponseWithBody(resp, buf); err != nil {
70 return nil, nil, err
71 }
72 return buf, resp, nil
73 }
74
75 func executeHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) ([]byte, error) {
76 buf, _, err := executeHTTPRequestWithResponse(ctx, client, req, logger, body, rpc)
77 return buf, err
78 }
79
80 func executeStreamingHTTPRequest(ctx context.Context, client *http.Client, req *http.Request, logger *slog.Logger, body []byte, rpc string) (*http.Response, error) {
81 logger.DebugContext(ctx, "api request", "serviceName", serviceName, "rpcName", rpc, "request", internallog.HTTPRequest(req, body))
82 resp, err := client.Do(req)
83 if err != nil {
84 return nil, err
85 }
86 logger.DebugContext(ctx, "api response", "serviceName", serviceName, "rpcName", rpc, "response", internallog.HTTPResponse(resp, nil))
87 if err = googleapi.CheckResponse(resp); err != nil {
88 return nil, err
89 }
90 return resp, nil
91 }