From 69df1f32d4edcaf0caa44d1525a6b44b4a17f68b Mon Sep 17 00:00:00 2001 From: Eike David Lenz Date: Tue, 14 Jul 2020 15:45:45 +0200 Subject: [PATCH 1/2] introduce NewRESTClientWithSwaggerClient introduce NewRESTClientWithSwaggerClient constructor method, returning a REST client containing a swagger client --- client.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 1dea31f5..22631875 100644 --- a/client.go +++ b/client.go @@ -3,6 +3,9 @@ package goharborclient import ( "context" + runtimeclient "github.com/go-openapi/runtime/client" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/runtime" "github.com/mittwald/goharbor-client/internal/api/v1_10_0/client" @@ -35,14 +38,28 @@ type RESTClient struct { // NewRESTClient constructs a new REST client containing each sub client. func NewRESTClient(cl *client.Harbor, authInfo runtime.ClientAuthInfoWriter) *RESTClient { - c := &RESTClient{ + return &RESTClient{ user: user.NewClient(cl, authInfo), project: project.NewClient(cl, authInfo), registry: registry.NewClient(cl, authInfo), replication: replication.NewClient(cl, authInfo), system: system.NewClient(cl, authInfo), } - return c +} + +// NewRESTClientWithSwaggerClient constructs a new REST client containing a swagger +// API client using the defined host string + basePath as well as basic auth info. +func NewRESTClientWithSwaggerClient(host, basePath, username, password string) *RESTClient { + swaggerClient := client.New(runtimeclient.New(host, basePath, []string{"http"}), strfmt.Default) + authInfo := runtimeclient.BasicAuth(username, password) + + return &RESTClient{ + user: user.NewClient(swaggerClient, authInfo), + project: project.NewClient(swaggerClient, authInfo), + registry: registry.NewClient(swaggerClient, authInfo), + replication: replication.NewClient(swaggerClient, authInfo), + system: system.NewClient(swaggerClient, authInfo), + } } // User Client From 89774aa51a3975e954767e014b15e61124a48185 Mon Sep 17 00:00:00 2001 From: Eike David Lenz Date: Tue, 14 Jul 2020 16:32:30 +0200 Subject: [PATCH 2/2] rename to NewRESTClientForHost, reduce method overhead --- client.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 22631875..79d63f33 100644 --- a/client.go +++ b/client.go @@ -47,19 +47,13 @@ func NewRESTClient(cl *client.Harbor, authInfo runtime.ClientAuthInfoWriter) *RE } } -// NewRESTClientWithSwaggerClient constructs a new REST client containing a swagger +// NewRESTClientForHost constructs a new REST client containing a swagger // API client using the defined host string + basePath as well as basic auth info. -func NewRESTClientWithSwaggerClient(host, basePath, username, password string) *RESTClient { +func NewRESTClientForHost(host, basePath, username, password string) *RESTClient { swaggerClient := client.New(runtimeclient.New(host, basePath, []string{"http"}), strfmt.Default) authInfo := runtimeclient.BasicAuth(username, password) - return &RESTClient{ - user: user.NewClient(swaggerClient, authInfo), - project: project.NewClient(swaggerClient, authInfo), - registry: registry.NewClient(swaggerClient, authInfo), - replication: replication.NewClient(swaggerClient, authInfo), - system: system.NewClient(swaggerClient, authInfo), - } + return NewRESTClient(swaggerClient, authInfo) } // User Client