Skip to content

Commit bf1598d

Browse files
authored
Merge pull request #181 from simbou2000/allow-custom-http-client
Add another method to create a REST Client using a configurable http.client object
2 parents 69162b1 + 8acc785 commit bf1598d

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

apiv2/client.go

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package apiv2
22

33
import (
44
"context"
5+
"net/http"
56
"net/url"
67
"strings"
78

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

158+
// NewRESTClientForHostWithClient constructs a new REST client containing a swagger API client using the defined
159+
// host string and basePath, the additional Harbor v2 API suffix as well as basic auth info while using provided http client.
160+
func NewRESTClientForHostWithClient(u, username, password string, opts *config.Options, client *http.Client) (*RESTClient, error) {
161+
if !strings.HasSuffix(u, v2URLSuffix) {
162+
u += v2URLSuffix
163+
}
164+
165+
harborURL, err := url.Parse(u)
166+
if err != nil {
167+
return nil, err
168+
}
169+
170+
v2SwaggerClient := v2client.New(runtimeclient.NewWithClient(harborURL.Host, harborURL.Path, []string{harborURL.Scheme}, client), strfmt.Default)
171+
authInfo := runtimeclient.BasicAuth(username, password)
172+
173+
return NewRESTClient(v2SwaggerClient, opts, authInfo), nil
174+
}
175+
157176
// NewRESTClientWithAuthFunc constructs a new REST client containing a swagger API client using the defined
158177
// host string and basePath, the additional Harbor v2 API suffix as well as a custom auth func, e.g. basic auth or token auth.
159178
func NewRESTClientWithAuthFunc(u string, authFunc runtime.ClientAuthInfoWriterFunc, opts *config.Options) (*RESTClient, error) {

apiv2/client_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,42 @@ func ExampleNewRESTClient() {
7171
}
7272
}
7373

74+
func ExampleNewRESTClientWithHttpClient() {
75+
// This example constructs a new (goharbor) REST client
76+
// and create an example project.
77+
ctx := context.Background()
78+
apiURL := "harbor.mydomain.com/api"
79+
username := "user"
80+
password := "password"
81+
82+
var optsTLS runtimeclient.TLSClientOptions
83+
// optsTLS.Certificate = "/path/to/client.crt"
84+
// optsTLS.Key = "/path/to/client.Key"
85+
// optsTLS.CA = "/path/to/rootca.cert.pem"
86+
client, err := runtimeclient.TLSClient(optsTLS)
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
harborURL, err := url.Parse(apiURL)
92+
if err != nil {
93+
panic(err)
94+
}
95+
96+
v2SwaggerClient := v2client.New(runtimeclient.NewWithClient(harborURL.Host, harborURL.Path, []string{harborURL.Scheme}), strfmt.Default)
97+
authInfo := runtimeclient.BasicAuth(username, password)
98+
99+
harborClient := NewRESTClient(v2SwaggerClient, nil, authInfo)
100+
101+
err = harborClient.NewProject(ctx, &model.ProjectReq{
102+
ProjectName: "my-project",
103+
})
104+
105+
if err != nil {
106+
panic(err)
107+
}
108+
}
109+
74110
func ExampleNewRESTClient_withOptions() {
75111
// This example constructs a new (goharbor) REST client using the provided 'options',
76112
// and lists all projects matching the 'options' configuration.

0 commit comments

Comments
 (0)