Skip to content

Commit

Permalink
fix(123): optimize error messages (#2415)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Nov 19, 2022
1 parent a02d9c8 commit 518487e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
11 changes: 6 additions & 5 deletions drivers/123/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (d *Pan123) List(ctx context.Context, dir model.Obj, args model.ListArgs) (

func (d *Pan123) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
if f, ok := file.(File); ok {
var resp DownResp
//var resp DownResp
var headers map[string]string
if !utils.IsLocalIPAddr(args.IP) {
headers = map[string]string{
Expand All @@ -87,13 +87,14 @@ func (d *Pan123) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
"size": f.Size,
"type": f.Type,
}
_, err := d.request("https://www.123pan.com/api/file/download_info", http.MethodPost, func(req *resty.Request) {
resp, err := d.request("https://www.123pan.com/api/file/download_info", http.MethodPost, func(req *resty.Request) {
req.SetBody(data).SetHeaders(headers)
}, &resp)
}, nil)
if err != nil {
return nil, err
}
u, err := url.Parse(resp.Data.DownloadUrl)
downloadUrl := utils.Json.Get(resp, "data", "DownloadUrl").ToString()
u, err := url.Parse(downloadUrl)
if err != nil {
return nil, err
}
Expand All @@ -112,7 +113,7 @@ func (d *Pan123) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
}
log.Debug(res.String())
link := model.Link{
URL: resp.Data.DownloadUrl,
URL: downloadUrl,
}
log.Debugln("res code: ", res.StatusCode())
if res.StatusCode() == 302 {
Expand Down
26 changes: 13 additions & 13 deletions drivers/123/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/alist-org/alist/v3/internal/model"
)

type BaseResp struct {
Code int `json:"code"`
Message string `json:"message"`
}
//type BaseResp struct {
// Code interface{} `json:"code"`
// Message string `json:"message"`
//}

type TokenResp struct {
BaseResp
//BaseResp
Data struct {
Token string `json:"token"`
} `json:"data"`
Expand Down Expand Up @@ -62,22 +62,22 @@ var _ model.Obj = (*File)(nil)
//var _ model.Thumb = (*File)(nil)

type Files struct {
BaseResp
//BaseResp
Data struct {
InfoList []File `json:"InfoList"`
Next string `json:"Next"`
} `json:"data"`
}

type DownResp struct {
BaseResp
Data struct {
DownloadUrl string `json:"DownloadUrl"`
} `json:"data"`
}
//type DownResp struct {
// //BaseResp
// Data struct {
// DownloadUrl string `json:"DownloadUrl"`
// } `json:"data"`
//}

type UploadResp struct {
BaseResp
//BaseResp
Data struct {
AccessKeyId string `json:"AccessKeyId"`
Bucket string `json:"Bucket"`
Expand Down
8 changes: 4 additions & 4 deletions drivers/123/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func (d *Pan123) login() error {
}
}
var resp TokenResp
_, err := base.RestyClient.R().
res, err := base.RestyClient.R().
SetResult(&resp).
SetBody(body).Post(url)
if err != nil {
return err
}
if resp.Code != 200 {
err = fmt.Errorf(resp.Message)
if utils.Json.Get(res.Body(), "code").ToInt() != 200 {
err = fmt.Errorf(utils.Json.Get(res.Body(), "message").ToString())
} else {
d.AccessToken = resp.Data.Token
}
Expand All @@ -62,7 +62,7 @@ func (d *Pan123) request(url string, method string, callback base.ReqCallback, r
return nil, err
}
body := res.Body()
code := jsoniter.Get(body, "code").ToInt()
code := utils.Json.Get(body, "code").ToInt()
if code != 0 {
if code == 401 {
err := d.login()
Expand Down

0 comments on commit 518487e

Please sign in to comment.