Skip to content

Commit c2df02c

Browse files
authored
Code cleanups from #547 (#552)
* remove unreachable code * Clean up duplicated code paths
1 parent 6fb5978 commit c2df02c

File tree

2 files changed

+20
-39
lines changed

2 files changed

+20
-39
lines changed

Diff for: cmd/ocm-backplane/managedJob/getManagedJob.go

-4
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ func newGetManagedJobCmd() *cobra.Command {
108108
return err
109109
}
110110

111-
if err != nil {
112-
return err
113-
}
114-
115111
if resp.StatusCode != http.StatusOK {
116112
return utils.TryPrintAPIError(resp, rawFlag)
117113
}

Diff for: pkg/backplaneapi/clientUtils.go

+20-35
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@ var (
3232
DefaultClientUtils ClientUtils = &DefaultClientUtilsImpl{}
3333
)
3434

35-
func (s *DefaultClientUtilsImpl) MakeRawBackplaneAPIClientWithAccessToken(base, accessToken string) (BackplaneApi.ClientInterface, error) {
36-
co := func(client *BackplaneApi.Client) error {
35+
func makeClientOptions(accessToken string) BackplaneApi.ClientOption {
36+
return func(client *BackplaneApi.Client) error {
3737
client.RequestEditors = append(client.RequestEditors, func(ctx context.Context, req *http.Request) error {
3838
req.Header.Add("Authorization", "Bearer "+accessToken)
3939
req.Header.Set("User-Agent", "backplane-cli"+info.Version)
4040
return nil
4141
})
4242
return nil
4343
}
44+
}
4445

46+
func (s *DefaultClientUtilsImpl) MakeRawBackplaneAPIClientWithAccessToken(base, accessToken string) (BackplaneApi.ClientInterface, error) {
4547
// Inject client Proxy Url from config
4648
if s.clientProxyURL == "" {
4749
bpConfig, err := config.GetBackplaneConfiguration()
@@ -53,39 +55,21 @@ func (s *DefaultClientUtilsImpl) MakeRawBackplaneAPIClientWithAccessToken(base,
5355
}
5456
}
5557

56-
// Update http proxy transport
57-
if s.clientProxyURL != "" {
58-
proxyURL, err := url.Parse(s.clientProxyURL)
59-
if err != nil {
60-
return nil, err
61-
}
62-
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
63-
64-
logger.Debugf("Using backplane Proxy URL: %s\n", s.clientProxyURL)
65-
}
66-
67-
return BackplaneApi.NewClient(base, co)
58+
return makeRawBackplaneAPIClientWithAccessTokenCustomProxy(base, accessToken, s.clientProxyURL)
6859
}
6960

7061
// makeRawBackplaneAPIClientWithAccessTokenCustomProxy creates a BackplaneApi.ClientInterface with a custom proxy url
7162
// proxyURL is optional, the default behavior is no proxy.
72-
func makeRawBackplaneAPIClientWithAccessTokenCustomProxy(server string, accessToken string, proxyURL *string) (BackplaneApi.ClientInterface, error) {
73-
co := func(client *BackplaneApi.Client) error {
74-
client.RequestEditors = append(client.RequestEditors, func(ctx context.Context, req *http.Request) error {
75-
req.Header.Add("Authorization", "Bearer "+accessToken)
76-
req.Header.Set("User-Agent", "backplane-cli"+info.Version)
77-
return nil
78-
})
79-
return nil
80-
}
63+
func makeRawBackplaneAPIClientWithAccessTokenCustomProxy(server string, accessToken string, proxyURL string) (BackplaneApi.ClientInterface, error) {
64+
co := makeClientOptions(accessToken)
8165

82-
if proxyURL != nil {
83-
proxy, err := url.Parse(*proxyURL)
66+
if proxyURL != "" {
67+
proxy, err := url.Parse(proxyURL)
8468
if err != nil {
8569
return nil, err
8670
}
8771
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxy)}
88-
logger.Debugf("Using backplane Proxy URL: %s\n", *proxyURL)
72+
logger.Debugf("Using backplane Proxy URL: %s\n", proxyURL)
8973
}
9074

9175
return BackplaneApi.NewClient(server, co)
@@ -100,14 +84,7 @@ func (s *DefaultClientUtilsImpl) MakeRawBackplaneAPIClient(base string) (Backpla
10084
}
10185

10286
func (*DefaultClientUtilsImpl) MakeBackplaneAPIClientWithAccessToken(base, accessToken string) (BackplaneApi.ClientWithResponsesInterface, error) {
103-
co := func(client *BackplaneApi.Client) error {
104-
client.RequestEditors = append(client.RequestEditors, func(ctx context.Context, req *http.Request) error {
105-
req.Header.Add("Authorization", "Bearer "+accessToken)
106-
req.Header.Set("User-Agent", "backplane-cli"+info.Version)
107-
return nil
108-
})
109-
return nil
110-
}
87+
co := makeClientOptions(accessToken)
11188

11289
return BackplaneApi.NewClientWithResponses(base, co)
11390
}
@@ -133,7 +110,15 @@ func (s *DefaultClientUtilsImpl) GetBackplaneClient(backplaneURL string, ocmToke
133110
}
134111

135112
logger.Debugln("Getting client")
136-
backplaneClient, err := makeRawBackplaneAPIClientWithAccessTokenCustomProxy(backplaneURL, ocmToken, proxyURL)
113+
114+
var proxyString string
115+
if proxyURL == nil {
116+
proxyString = ""
117+
} else {
118+
proxyString = *proxyURL
119+
}
120+
121+
backplaneClient, err := makeRawBackplaneAPIClientWithAccessTokenCustomProxy(backplaneURL, ocmToken, proxyString)
137122
if err != nil {
138123
return client, fmt.Errorf("unable to create backplane api client: %w", err)
139124
}

0 commit comments

Comments
 (0)