feat(dropbox): add root_namespace_id to access teams folder (#5929)

* feat(dropbox): add root_namespace_id to access teams folder

* fix(dropbox): get_current_account API request

* feat(dropbox): extract root_namespace_id properly

* style: format code
pull/5957/head
shouko 2024-01-24 18:03:50 +09:00 committed by GitHub
parent 9222510d8d
commit aef952ae68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 4 deletions

View File

@ -45,7 +45,25 @@ func (d *Dropbox) Init(ctx context.Context) error {
if result != query { if result != query {
return fmt.Errorf("failed to check user: %s", string(res)) return fmt.Errorf("failed to check user: %s", string(res))
} }
return nil d.RootNamespaceId, err = d.GetRootNamespaceId(ctx)
return err
}
func (d *Dropbox) GetRootNamespaceId(ctx context.Context) (string, error) {
res, err := d.request("/2/users/get_current_account", http.MethodPost, func(req *resty.Request) {
req.SetBody(nil)
})
if err != nil {
return "", err
}
var currentAccountResp CurrentAccountResp
err = utils.Json.Unmarshal(res, &currentAccountResp)
if err != nil {
return "", err
}
rootNamespaceId := currentAccountResp.RootInfo.RootNamespaceId
return rootNamespaceId, nil
} }
func (d *Dropbox) Drop(ctx context.Context) error { func (d *Dropbox) Drop(ctx context.Context) error {

View File

@ -18,6 +18,7 @@ type Addition struct {
ClientSecret string `json:"client_secret" required:"false" help:"Keep it empty if you don't have one"` ClientSecret string `json:"client_secret" required:"false" help:"Keep it empty if you don't have one"`
AccessToken string AccessToken string
RootNamespaceId string
} }
var config = driver.Config{ var config = driver.Config{

View File

@ -23,6 +23,13 @@ type RefreshTokenErrorResp struct {
ErrorDescription string `json:"error_description"` ErrorDescription string `json:"error_description"`
} }
type CurrentAccountResp struct {
RootInfo struct {
RootNamespaceId string `json:"root_namespace_id"`
HomeNamespaceId string `json:"home_namespace_id"`
} `json:"root_info"`
}
type File struct { type File struct {
Tag string `json:".tag"` Tag string `json:".tag"`
Name string `json:"name"` Name string `json:"name"`

View File

@ -46,12 +46,22 @@ func (d *Dropbox) refreshToken() error {
func (d *Dropbox) request(uri, method string, callback base.ReqCallback, retry ...bool) ([]byte, error) { func (d *Dropbox) request(uri, method string, callback base.ReqCallback, retry ...bool) ([]byte, error) {
req := base.RestyClient.R() req := base.RestyClient.R()
req.SetHeader("Authorization", "Bearer "+d.AccessToken) req.SetHeader("Authorization", "Bearer "+d.AccessToken)
if method == http.MethodPost { if d.RootNamespaceId != "" {
req.SetHeader("Content-Type", "application/json") apiPathRootJson, err := utils.Json.MarshalToString(map[string]interface{}{
".tag": "root",
"root": d.RootNamespaceId,
})
if err != nil {
return nil, err
}
req.SetHeader("Dropbox-API-Path-Root", apiPathRootJson)
} }
if callback != nil { if callback != nil {
callback(req) callback(req)
} }
if method == http.MethodPost && req.Body != nil {
req.SetHeader("Content-Type", "application/json")
}
var e ErrorResp var e ErrorResp
req.SetError(&e) req.SetError(&e)
res, err := req.Execute(method, d.base+uri) res, err := req.Execute(method, d.base+uri)