@@ -34,7 +34,7 @@ func GetState(account *model.Account) *State {
3434 SetHeaders (map [string ]string {
3535 "Accept" : "application/json;charset=UTF-8" ,
3636 "User-Agent" : base .UserAgent ,
37- }),
37+ }). SetTimeout ( base . DefaultTimeout ) ,
3838 }
3939 userStateCache .States [account .Username ] = state
4040 return state
@@ -198,7 +198,7 @@ func (s *State) refreshSession(account *model.Account) error {
198198 "accessToken" : s .AccessToken ,
199199 }).
200200 SetHeader ("X-Request-ID" , uuid .NewString ()).
201- Get ("https://api.cloud.189.cn /getSessionForPC.action" )
201+ Get (API_URL + " /getSessionForPC.action" )
202202 if err != nil {
203203 return err
204204 }
@@ -223,10 +223,8 @@ func (s *State) refreshSession(account *model.Account) error {
223223 return nil
224224}
225225
226- func (s * State ) IsLogin () bool {
227- _ , err := s .Request ("GET" , API_URL + "/getUserInfo.action" , nil , func (r * resty.Request ) {
228- r .SetQueryParams (clientSuffix ())
229- }, nil )
226+ func (s * State ) IsLogin (account * model.Account ) bool {
227+ _ , err := s .Request (http .MethodGet , API_URL + "/getUserInfo.action" , nil , func (r * resty.Request ) { r .SetQueryParams (clientSuffix ()) }, account )
230228 return err == nil
231229}
232230
@@ -242,12 +240,12 @@ func (s *State) RefreshSession(account *model.Account) error {
242240 return s .refreshSession (account )
243241}
244242
245- func (s * State ) Request (method string , fullUrl string , params url. Values , callback func (* resty.Request ), account * model.Account ) (* resty.Response , error ) {
243+ func (s * State ) Request (method string , fullUrl string , params Params , callback func (* resty.Request ), account * model.Account ) (* resty.Response , error ) {
246244 s .Lock ()
247245 dateOfGmt := getHttpDateStr ()
248246 sessionKey := s .SessionKey
249247 sessionSecret := s .SessionSecret
250- if account != nil && isFamily (account ) {
248+ if isFamily (account ) {
251249 sessionKey = s .FamilySessionKey
252250 sessionSecret = s .FamilySessionSecret
253251 }
@@ -267,25 +265,12 @@ func (s *State) Request(method string, fullUrl string, params url.Values, callba
267265 }
268266 req .SetHeader ("Signature" , signatureOfHmac (sessionSecret , sessionKey , method , fullUrl , dateOfGmt , paramsData ))
269267
270- callback (req )
268+ if callback != nil {
269+ callback (req )
270+ }
271271 s .Unlock ()
272272
273- var err error
274- var res * resty.Response
275- switch method {
276- case "GET" :
277- res , err = req .Get (fullUrl )
278- case "POST" :
279- res , err = req .Post (fullUrl )
280- case "DELETE" :
281- res , err = req .Delete (fullUrl )
282- case "PATCH" :
283- res , err = req .Patch (fullUrl )
284- case "PUT" :
285- res , err = req .Put (fullUrl )
286- default :
287- return nil , base .ErrNotSupport
288- }
273+ res , err := req .Execute (method , fullUrl )
289274 if err != nil {
290275 return nil , err
291276 }
@@ -298,6 +283,9 @@ func (s *State) Request(method string, fullUrl string, params url.Values, callba
298283 }
299284 if erron .Code != "" && erron .Code != "SUCCESS" {
300285 if erron .Msg == "" {
286+ if erron .Message == "" {
287+ return nil , fmt .Errorf (res .String ())
288+ }
301289 return nil , fmt .Errorf (erron .Message )
302290 }
303291 return nil , fmt .Errorf (erron .Msg )
@@ -306,25 +294,18 @@ func (s *State) Request(method string, fullUrl string, params url.Values, callba
306294 return nil , fmt .Errorf (erron .ErrorMsg )
307295 }
308296
309- if account != nil {
310- switch utils .Json .Get (res .Body (), "res_code" ).ToInt64 () {
311- case 11 , 18 :
312- if err := s .RefreshSession (account ); err != nil {
313- return nil , err
314- }
315- return s .Request (method , fullUrl , params , callback , account )
316- case 0 :
317- if res .StatusCode () == http .StatusOK {
318- return res , nil
319- }
320- fallthrough
321- default :
322- return nil , fmt .Errorf (res .String ())
297+ switch utils .Json .Get (res .Body (), "res_code" ).ToInt64 () {
298+ case 11 , 18 :
299+ if err := s .RefreshSession (account ); err != nil {
300+ return nil , err
323301 }
302+ return s .Request (method , fullUrl , params , callback , account )
303+ case 0 :
304+ if res .StatusCode () == http .StatusOK {
305+ return res , nil
306+ }
307+ return nil , fmt .Errorf (res .String ())
308+ default :
309+ return nil , fmt .Errorf (utils .Json .Get (res .Body (), "res_message" ).ToString ())
324310 }
325-
326- if utils .Json .Get (res .Body (), "res_code" ).ToInt64 () != 0 {
327- return res , fmt .Errorf (utils .Json .Get (res .Body (), "res_message" ).ToString ())
328- }
329- return res , nil
330311}
0 commit comments