@@ -11,36 +11,27 @@ import (
1111 ctypes "scroll-tech/common/types"
1212 "scroll-tech/coordinator/internal/config"
1313 "scroll-tech/coordinator/internal/types"
14-
15- "github.com/mitchellh/mapstructure"
1614)
1715
18- type ClientHelper interface {
19- GenLoginParam (string ) (* types.LoginParameter , error )
20- OnResp (* upClient , * http.Response )
21- }
22-
2316// Client wraps an http client with a preset host for coordinator API calls
2417type upClient struct {
2518 httpClient * http.Client
2619 baseURL string
2720 loginToken string
28- helper ClientHelper
2921}
3022
3123// NewClient creates a new Client with the specified host
32- func newUpClient (cfg * config.UpStream , helper ClientHelper ) * upClient {
24+ func newUpClient (cfg * config.UpStream ) * upClient {
3325 return & upClient {
3426 httpClient : & http.Client {
3527 Timeout : time .Duration (cfg .ConnectionTimeoutSec ) * time .Second ,
3628 },
3729 baseURL : cfg .BaseUrl ,
38- helper : helper ,
3930 }
4031}
4132
4233// FullLogin performs the complete login process: get challenge then login
43- func (c * upClient ) Login (ctx context.Context ) (* types.LoginSchema , error ) {
34+ func (c * upClient ) Login (ctx context.Context , genLogin func ( string ) ( * types. LoginParameter , error ) ) (* types.LoginSchema , error ) {
4435 // Step 1: Get challenge
4536 url := fmt .Sprintf ("%s/coordinator/v1/challenge" , c .baseURL )
4637
@@ -68,7 +59,7 @@ func (c *upClient) Login(ctx context.Context) (*types.LoginSchema, error) {
6859 // Step 3: Use the token from challenge as Bearer token for login
6960 url = fmt .Sprintf ("%s/coordinator/v1/login" , c .baseURL )
7061
71- param , err := c . helper . GenLoginParam (loginSchema .Token )
62+ param , err := genLogin (loginSchema .Token )
7263 if err != nil {
7364 return nil , fmt .Errorf ("failed to setup login parameter: %w" , err )
7465 }
@@ -91,28 +82,38 @@ func (c *upClient) Login(ctx context.Context) (*types.LoginSchema, error) {
9182 return nil , fmt .Errorf ("failed to perform login request: %w" , err )
9283 }
9384
94- // Parse login response as LoginSchema and store the token
95- if loginResp .StatusCode == http .StatusOK {
85+ parsedResp , err := handleHttpResp (loginResp )
86+ if err != nil {
87+ return nil , err
88+ }
89+
90+ var loginResult types.LoginSchema
91+ err = parsedResp .DecodeData (& loginResult )
92+ if err != nil {
93+ return nil , fmt .Errorf ("login parsing data fail: %v" , err )
94+ }
95+ c .loginToken = loginResult .Token
96+ return & loginResult , nil
97+
98+ }
99+
100+ func handleHttpResp (resp * http.Response ) (* ctypes.Response , error ) {
101+ if resp .StatusCode == http .StatusOK || resp .StatusCode == http .StatusUnauthorized {
102+ defer resp .Body .Close ()
96103 var respWithData ctypes.Response
97104 // Note: Body is consumed after decoding, caller should not read it again
98- if err := json .NewDecoder (loginResp .Body ).Decode (& respWithData ); err == nil {
99- var loginResult types.LoginSchema
100- err = mapstructure .Decode (respWithData .Data , & loginResult )
101- if err != nil {
102- return nil , fmt .Errorf ("login parsing data fail, get %v" , respWithData .Data )
103- }
104- c .loginToken = loginResult .Token
105- return & loginResult , nil
105+ if err := json .NewDecoder (resp .Body ).Decode (& respWithData ); err == nil {
106+ return & respWithData , nil
106107 } else {
107- return nil , fmt .Errorf ("login parsing response failed: %v" , err )
108+ return nil , fmt .Errorf ("login parsing expected response failed: %v" , err )
108109 }
109- }
110110
111- return nil , fmt .Errorf ("login request failed with status: %d" , loginResp .StatusCode )
111+ }
112+ return nil , fmt .Errorf ("login request failed with status: %d" , resp .StatusCode )
112113}
113114
114115// ProxyLogin makes a POST request to /v1/proxy_login with LoginParameter
115- func (c * upClient ) ProxyLogin (ctx context.Context , param * types.LoginParameter ) (* types. LoginSchema , error ) {
116+ func (c * upClient ) ProxyLogin (ctx context.Context , param * types.LoginParameter ) (* ctypes. Response , error ) {
116117 url := fmt .Sprintf ("%s/coordinator/v1/proxy_login" , c .baseURL )
117118
118119 jsonData , err := json .Marshal (param )
@@ -132,26 +133,11 @@ func (c *upClient) ProxyLogin(ctx context.Context, param *types.LoginParameter)
132133 if err != nil {
133134 return nil , fmt .Errorf ("failed to perform proxy login request: %w" , err )
134135 }
135- defer proxyLoginResp .Body .Close ()
136-
137- // Call helper's OnResp method with the response
138- c .helper .OnResp (c , proxyLoginResp )
139-
140- // Parse proxy login response as LoginSchema
141- if proxyLoginResp .StatusCode == http .StatusOK {
142- var loginResult types.LoginSchema
143- if err := json .NewDecoder (proxyLoginResp .Body ).Decode (& loginResult ); err == nil {
144- return & loginResult , nil
145- }
146- // If parsing fails, still return success but with nil result
147- return nil , nil
148- }
149-
150- return nil , fmt .Errorf ("proxy login request failed with status: %d" , proxyLoginResp .StatusCode )
136+ return handleHttpResp (proxyLoginResp )
151137}
152138
153139// GetTask makes a POST request to /v1/get_task with GetTaskParameter
154- func (c * upClient ) GetTask (ctx context.Context , param * types.GetTaskParameter , token string ) (* http .Response , error ) {
140+ func (c * upClient ) GetTask (ctx context.Context , param * types.GetTaskParameter , token string ) (* ctypes .Response , error ) {
155141 url := fmt .Sprintf ("%s/coordinator/v1/get_task" , c .baseURL )
156142
157143 jsonData , err := json .Marshal (param )
@@ -169,11 +155,15 @@ func (c *upClient) GetTask(ctx context.Context, param *types.GetTaskParameter, t
169155 req .Header .Set ("Authorization" , "Bearer " + token )
170156 }
171157
172- return c .httpClient .Do (req )
158+ resp , err := c .httpClient .Do (req )
159+ if err != nil {
160+ return nil , err
161+ }
162+ return handleHttpResp (resp )
173163}
174164
175165// SubmitProof makes a POST request to /v1/submit_proof with SubmitProofParameter
176- func (c * upClient ) SubmitProof (ctx context.Context , param * types.SubmitProofParameter , token string ) (* http .Response , error ) {
166+ func (c * upClient ) SubmitProof (ctx context.Context , param * types.SubmitProofParameter , token string ) (* ctypes .Response , error ) {
177167 url := fmt .Sprintf ("%s/coordinator/v1/submit_proof" , c .baseURL )
178168
179169 jsonData , err := json .Marshal (param )
@@ -191,5 +181,9 @@ func (c *upClient) SubmitProof(ctx context.Context, param *types.SubmitProofPara
191181 req .Header .Set ("Authorization" , "Bearer " + token )
192182 }
193183
194- return c .httpClient .Do (req )
184+ resp , err := c .httpClient .Do (req )
185+ if err != nil {
186+ return nil , err
187+ }
188+ return handleHttpResp (resp )
195189}
0 commit comments