Skip to content

Commit

Permalink
Merge pull request #181 from simbou2000/allow-custom-http-client
Browse files Browse the repository at this point in the history
Add another method to create a REST Client using a configurable http.client object
  • Loading branch information
elenz97 authored Nov 14, 2023
2 parents 69162b1 + 8acc785 commit bf1598d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
19 changes: 19 additions & 0 deletions apiv2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package apiv2

import (
"context"
"net/http"
"net/url"
"strings"

Expand Down Expand Up @@ -154,6 +155,24 @@ func NewRESTClientForHost(u, username, password string, opts *config.Options) (*
return NewRESTClient(v2SwaggerClient, opts, authInfo), nil
}

// NewRESTClientForHostWithClient constructs a new REST client containing a swagger API client using the defined
// host string and basePath, the additional Harbor v2 API suffix as well as basic auth info while using provided http client.
func NewRESTClientForHostWithClient(u, username, password string, opts *config.Options, client *http.Client) (*RESTClient, error) {
if !strings.HasSuffix(u, v2URLSuffix) {
u += v2URLSuffix
}

harborURL, err := url.Parse(u)
if err != nil {
return nil, err
}

v2SwaggerClient := v2client.New(runtimeclient.NewWithClient(harborURL.Host, harborURL.Path, []string{harborURL.Scheme}, client), strfmt.Default)
authInfo := runtimeclient.BasicAuth(username, password)

return NewRESTClient(v2SwaggerClient, opts, authInfo), nil
}

// NewRESTClientWithAuthFunc constructs a new REST client containing a swagger API client using the defined
// host string and basePath, the additional Harbor v2 API suffix as well as a custom auth func, e.g. basic auth or token auth.
func NewRESTClientWithAuthFunc(u string, authFunc runtime.ClientAuthInfoWriterFunc, opts *config.Options) (*RESTClient, error) {
Expand Down
36 changes: 36 additions & 0 deletions apiv2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,42 @@ func ExampleNewRESTClient() {
}
}

func ExampleNewRESTClientWithHttpClient() {
// This example constructs a new (goharbor) REST client
// and create an example project.
ctx := context.Background()
apiURL := "harbor.mydomain.com/api"
username := "user"
password := "password"

var optsTLS runtimeclient.TLSClientOptions
// optsTLS.Certificate = "/path/to/client.crt"
// optsTLS.Key = "/path/to/client.Key"
// optsTLS.CA = "/path/to/rootca.cert.pem"
client, err := runtimeclient.TLSClient(optsTLS)
if err != nil {
return nil, err
}

harborURL, err := url.Parse(apiURL)
if err != nil {
panic(err)
}

v2SwaggerClient := v2client.New(runtimeclient.NewWithClient(harborURL.Host, harborURL.Path, []string{harborURL.Scheme}), strfmt.Default)
authInfo := runtimeclient.BasicAuth(username, password)

harborClient := NewRESTClient(v2SwaggerClient, nil, authInfo)

err = harborClient.NewProject(ctx, &model.ProjectReq{
ProjectName: "my-project",
})

if err != nil {
panic(err)
}
}

func ExampleNewRESTClient_withOptions() {
// This example constructs a new (goharbor) REST client using the provided 'options',
// and lists all projects matching the 'options' configuration.
Expand Down

0 comments on commit bf1598d

Please sign in to comment.