-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathclient.go
73 lines (63 loc) · 1.92 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package candlepin_client
import (
"context"
"fmt"
"io"
"net/http"
caliri "github.com/content-services/caliri/release/v4"
"github.com/content-services/content-sources-backend/pkg/config"
uuid2 "github.com/google/uuid"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
type cpClientImpl struct {
}
const DevelOrgKey = "content-sources-test"
const YumRepoType = "yum"
func errorWithResponseBody(message string, httpResp *http.Response, err error) error {
if httpResp != nil {
body, readErr := io.ReadAll(httpResp.Body)
if readErr != nil {
log.Logger.Error().Err(readErr).Msg("could not read http body")
}
errWithBody := fmt.Errorf("%w: %v", err, string(body))
return fmt.Errorf("%v: %w", errors.New(message), errWithBody)
}
return err
}
func getCorrelationId(ctx context.Context) string {
value := ctx.Value(config.ContextRequestIDKey{})
if value != nil {
valueStr, ok := value.(string)
if ok {
return valueStr
}
}
newId := uuid2.NewString()
log.Logger.Warn().Msgf("Creating correlation ID for candlepin request %v", newId)
return newId
}
func NewCandlepinClient() CandlepinClient {
return &cpClientImpl{}
}
func getCandlepinClient(ctx context.Context) (context.Context, *caliri.APIClient, error) {
httpClient, err := config.GetHTTPClient(&config.CandlepinCertUser{})
if err != nil {
return nil, nil, err
}
cpConfig := caliri.NewConfiguration()
cpConfig.DefaultHeader["X-Correlation-ID"] = getCorrelationId(ctx)
cpConfig.HTTPClient = &httpClient
cpConfig.Servers = caliri.ServerConfigurations{caliri.ServerConfiguration{
URL: config.Get().Clients.Candlepin.Server,
}}
// cpConfig.Debug = true
client := caliri.NewAPIClient(cpConfig)
if config.Get().Clients.Candlepin.Username != "" {
ctx = context.WithValue(ctx, caliri.ContextBasicAuth, caliri.BasicAuth{
UserName: config.Get().Clients.Candlepin.Username,
Password: config.Get().Clients.Candlepin.Password,
})
}
return ctx, client, nil
}