Skip to content

Commit 96f0789

Browse files
committed
Update binding RichClient initialization
Signed-off-by: Marek Aufart <[email protected]>
1 parent ef59abc commit 96f0789

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

binding/richclient.go

+22-33
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package binding
22

33
import (
4-
"fmt"
5-
64
"github.com/konveyor/controller/pkg/logging"
75
"github.com/konveyor/tackle2-hub/api"
86
"github.com/konveyor/tackle2-hub/settings"
@@ -13,24 +11,15 @@ var (
1311
Log = logging.WithName("binding")
1412
)
1513

16-
var (
17-
// Addon An addon richClient configured for a task execution.
18-
RichClient *Adapter
19-
// Client
20-
//client *Client
21-
)
22-
2314
func init() {
24-
err := Settings.Load()
25-
if err != nil {
26-
panic(err)
27-
}
28-
29-
RichClient = newRichClient()
15+
err := Settings.Load()
16+
if err != nil {
17+
panic(err)
18+
}
3019
}
3120

3221
// The RichClient provides API integration.
33-
type Adapter struct {
22+
type RichClient struct {
3423
// // Task API.
3524
// Task
3625
// // Settings API
@@ -54,32 +43,19 @@ type Adapter struct {
5443
}
5544

5645
// Client provides the REST client.
57-
func (h *Adapter) Client() *Client {
46+
func (h *RichClient) Client() *Client {
5847
return h.client
5948
}
6049

61-
// newRichClient builds a new Addon RichClient object.
62-
func newRichClient() (richClient *Adapter) {
50+
// newRichClient builds a new RichClient object.
51+
func New(baseUrl string) (r *RichClient) {
6352
//
6453
// Build REST client.
65-
baseUrl := settings.Settings.Addon.Hub.URL
66-
login := api.Login{User: settings.Settings.Auth.Keycloak.Admin.User, Password: settings.Settings.Auth.Keycloak.Admin.Pass}
6754
client := NewClient(baseUrl, "")
6855

69-
// Disable HTTP requests retry for network-related errors to fail quickly.
70-
// TODO: parametrize (only for tests)
71-
client.Retry = 0
72-
73-
// Login.
74-
err := client.Post(api.AuthLoginRoot, &login)
75-
if err != nil {
76-
panic(fmt.Sprintf("Cannot login to API: %v.", err.Error()))
77-
}
78-
client.SetToken(login.Token)
79-
8056
//
8157
// Build RichClient.
82-
richClient = &Adapter{
58+
r = &RichClient{
8359
// Task: Task{
8460
// client: client,
8561
// },
@@ -115,3 +91,16 @@ func newRichClient() (richClient *Adapter) {
11591
return
11692
}
11793

94+
func (r *RichClient) Login(user, password string) (err error) {
95+
//
96+
// Build REST client.
97+
login := api.Login{User: user, Password: password}
98+
99+
// Login.
100+
err = r.client.Post(api.AuthLoginRoot, &login)
101+
if err != nil {
102+
return
103+
}
104+
r.client.SetToken(login.Token)
105+
return
106+
}

test/api/application/pkg.go

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
package application
22

3-
import "github.com/konveyor/tackle2-hub/binding"
3+
import (
4+
"fmt"
45

6+
"github.com/konveyor/tackle2-hub/binding"
7+
"github.com/konveyor/tackle2-hub/settings"
8+
)
59

6-
var Application = binding.RichClient.Application
7-
var Client = binding.RichClient.Client()
10+
var (
11+
Client *binding.Client
12+
RichClient *binding.RichClient
13+
Application binding.Application
14+
)
15+
16+
17+
func init() {
18+
// Prepare RichClient and login to Hub API
19+
RichClient = binding.New(settings.Settings.Addon.Hub.URL)
20+
err := RichClient.Login(settings.Settings.Auth.Keycloak.Admin.User, settings.Settings.Auth.Keycloak.Admin.Pass)
21+
if err != nil {
22+
panic(fmt.Sprintf("Cannot login to API: %v.", err.Error()))
23+
}
24+
25+
// Access REST client directly (some test API call need it)
26+
Client = RichClient.Client()
27+
28+
// Disable HTTP requests retry for network-related errors to fail quickly.
29+
Client.Retry = 0
30+
31+
// Shortcut for Application-related RichClient methods.
32+
Application = RichClient.Application
33+
34+
}

0 commit comments

Comments
 (0)