Skip to content

Commit 404e9ea

Browse files
authored
Merge pull request #9454 from okatu-loli/fix/139-work-issue
fix(139): recover personal host for personal_new requests
2 parents c20da5a + 35832d0 commit 404e9ea

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

drivers/139/driver.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,12 @@ func (d *Yun139) Init(ctx context.Context) error {
4747
if err != nil {
4848
return err
4949
}
50-
51-
// Query Route Policy
52-
var resp QueryRoutePolicyResp
53-
_, err = d.requestRoute(base.Json{
54-
"userInfo": base.Json{
55-
"userType": 1,
56-
"accountType": 1,
57-
"accountName": d.Account},
58-
"modAddrType": 1,
59-
}, &resp)
60-
if err != nil {
61-
return err
62-
}
63-
for _, policyItem := range resp.Data.RoutePolicyList {
64-
if policyItem.ModName == "personal" {
65-
d.PersonalCloudHost = policyItem.HttpsUrl
66-
break
50+
if d.Addition.Type == MetaPersonalNew {
51+
err = d.ensurePersonalCloudHost()
52+
if err != nil {
53+
return err
6754
}
6855
}
69-
if len(d.PersonalCloudHost) == 0 {
70-
return fmt.Errorf("PersonalCloudHost is empty")
71-
}
7256

7357
d.cron = cron.NewCron(time.Hour * 12)
7458
d.cron.Do(func() {

drivers/139/util.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,46 @@ func (d *Yun139) requestRoute(data interface{}, resp interface{}) ([]byte, error
215215
return res.Body(), nil
216216
}
217217

218+
func (d *Yun139) ensurePersonalCloudHost() error {
219+
if d.ref != nil {
220+
return d.ref.ensurePersonalCloudHost()
221+
}
222+
if d.PersonalCloudHost != "" {
223+
return nil
224+
}
225+
if len(d.Authorization) == 0 {
226+
return fmt.Errorf("authorization is empty")
227+
}
228+
if d.Account == "" {
229+
if err := d.refreshToken(); err != nil {
230+
return err
231+
}
232+
}
233+
234+
var resp QueryRoutePolicyResp
235+
_, err := d.requestRoute(base.Json{
236+
"userInfo": base.Json{
237+
"userType": 1,
238+
"accountType": 1,
239+
"accountName": d.Account,
240+
},
241+
"modAddrType": 1,
242+
}, &resp)
243+
if err != nil {
244+
return err
245+
}
246+
for _, policyItem := range resp.Data.RoutePolicyList {
247+
if policyItem.ModName == "personal" && policyItem.HttpsUrl != "" {
248+
d.PersonalCloudHost = strings.TrimRight(policyItem.HttpsUrl, "/")
249+
break
250+
}
251+
}
252+
if d.PersonalCloudHost == "" {
253+
return fmt.Errorf("personal cloud host is empty")
254+
}
255+
return nil
256+
}
257+
218258
func (d *Yun139) post(pathname string, data interface{}, resp interface{}) ([]byte, error) {
219259
return d.request(pathname, http.MethodPost, func(req *resty.Request) {
220260
req.SetBody(data)
@@ -449,6 +489,9 @@ func unicode(str string) string {
449489
}
450490

451491
func (d *Yun139) personalRequest(pathname string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
492+
if err := d.ensurePersonalCloudHost(); err != nil {
493+
return nil, err
494+
}
452495
url := d.getPersonalCloudHost() + pathname
453496
req := base.RestyClient.R()
454497
randStr := random.String(16)

0 commit comments

Comments
 (0)