Skip to content

Commit 0d238d7

Browse files
committed
WIP: the structure of client manager
1 parent 76ecdf0 commit 0d238d7

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

coordinator/internal/controller/proxy/client.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,32 @@ import (
1414
"scroll-tech/coordinator/internal/types"
1515
)
1616

17+
type ClientHelper interface {
18+
GenLoginParam(string) (*types.LoginParameter, error)
19+
OnError(isUnauth bool)
20+
}
21+
1722
// Client wraps an http client with a preset host for coordinator API calls
1823
type upClient struct {
1924
httpClient *http.Client
2025
baseURL string
2126
loginToken string
27+
helper ClientHelper
2228
}
2329

2430
// NewClient creates a new Client with the specified host
25-
func newUpClient(cfg *config.UpStream) *upClient {
31+
func newUpClient(cfg *config.UpStream, helper ClientHelper) *upClient {
2632
return &upClient{
2733
httpClient: &http.Client{
2834
Timeout: time.Duration(cfg.ConnectionTimeoutSec) * time.Second,
2935
},
3036
baseURL: cfg.BaseUrl,
37+
helper: helper,
3138
}
3239
}
3340

3441
// FullLogin performs the complete login process: get challenge then login
35-
func (c *upClient) Login(ctx context.Context, param types.LoginParameter) (*types.LoginSchema, error) {
42+
func (c *upClient) Login(ctx context.Context) (*types.LoginSchema, error) {
3643
// Step 1: Get challenge
3744
url := fmt.Sprintf("%s/coordinator/v1/challenge", c.baseURL)
3845

@@ -60,6 +67,11 @@ func (c *upClient) Login(ctx context.Context, param types.LoginParameter) (*type
6067
// Step 3: Use the token from challenge as Bearer token for login
6168
url = fmt.Sprintf("%s/coordinator/v1/login", c.baseURL)
6269

70+
param, err := c.helper.GenLoginParam(loginSchema.Token)
71+
if err != nil {
72+
return nil, fmt.Errorf("failed to setup login parameter: %w", err)
73+
}
74+
6375
jsonData, err := json.Marshal(param)
6476
if err != nil {
6577
return nil, fmt.Errorf("failed to marshal login parameter: %w", err)

coordinator/internal/controller/proxy/client_manager.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ func NewClientManager(cliCfg *config.ProxyClient, cfg *config.UpStream) (*Client
5656
}
5757

5858
func (cliMgr *ClientManager) Client(ctx context.Context) *upClient {
59-
return newUpClient(cliMgr.cfg)
59+
return newUpClient(cliMgr.cfg, cliMgr)
6060
}
6161

62-
func (cliMgr *ClientManager) generateLoginParameter(privKey []byte, challenge string) (*types.LoginParameter, error) {
62+
func (cliMgr *ClientManager) OnError(isUnauth bool) {
63+
64+
}
65+
66+
func (cliMgr *ClientManager) GenLoginParam(challenge string) (*types.LoginParameter, error) {
6367

6468
// Generate public key string
6569
publicKeyHex := common.Bytes2Hex(crypto.CompressPubkey(&cliMgr.privKey.PublicKey))

0 commit comments

Comments
 (0)