Skip to content

Code cleanups from #547 #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
4 changes: 0 additions & 4 deletions cmd/ocm-backplane/managedJob/getManagedJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ func newGetManagedJobCmd() *cobra.Command {
return err
}

if err != nil {
return err
}

if resp.StatusCode != http.StatusOK {
return utils.TryPrintAPIError(resp, rawFlag)
}
Expand Down
55 changes: 20 additions & 35 deletions pkg/backplaneapi/clientUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ var (
DefaultClientUtils ClientUtils = &DefaultClientUtilsImpl{}
)

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

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

// Update http proxy transport
if s.clientProxyURL != "" {
proxyURL, err := url.Parse(s.clientProxyURL)
if err != nil {
return nil, err
}
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}

logger.Debugf("Using backplane Proxy URL: %s\n", s.clientProxyURL)
}

return BackplaneApi.NewClient(base, co)
return makeRawBackplaneAPIClientWithAccessTokenCustomProxy(base, accessToken, s.clientProxyURL)
}

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

if proxyURL != nil {
proxy, err := url.Parse(*proxyURL)
if proxyURL != "" {
proxy, err := url.Parse(proxyURL)
if err != nil {
return nil, err
}
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxy)}
logger.Debugf("Using backplane Proxy URL: %s\n", *proxyURL)
logger.Debugf("Using backplane Proxy URL: %s\n", proxyURL)
}

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

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

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

logger.Debugln("Getting client")
backplaneClient, err := makeRawBackplaneAPIClientWithAccessTokenCustomProxy(backplaneURL, ocmToken, proxyURL)

var proxyString string
if proxyURL == nil {
proxyString = ""
} else {
proxyString = *proxyURL
}

backplaneClient, err := makeRawBackplaneAPIClientWithAccessTokenCustomProxy(backplaneURL, ocmToken, proxyString)
if err != nil {
return client, fmt.Errorf("unable to create backplane api client: %w", err)
}
Expand Down