Skip to content

Commit

Permalink
feat(dropbox): add root_namespace_id to access teams folder (#5929)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
shouko authored Jan 24, 2024
1 parent 9222510 commit aef952a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
20 changes: 19 additions & 1 deletion drivers/dropbox/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,25 @@ func (d *Dropbox) Init(ctx context.Context) error {
if result != query {
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 {
Expand Down
3 changes: 2 additions & 1 deletion drivers/dropbox/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Addition struct {
ClientID string `json:"client_id" 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{
Expand Down
7 changes: 7 additions & 0 deletions drivers/dropbox/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ type RefreshTokenErrorResp struct {
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 {
Tag string `json:".tag"`
Name string `json:"name"`
Expand Down
14 changes: 12 additions & 2 deletions drivers/dropbox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,22 @@ func (d *Dropbox) refreshToken() error {
func (d *Dropbox) request(uri, method string, callback base.ReqCallback, retry ...bool) ([]byte, error) {
req := base.RestyClient.R()
req.SetHeader("Authorization", "Bearer "+d.AccessToken)
if method == http.MethodPost {
req.SetHeader("Content-Type", "application/json")
if d.RootNamespaceId != "" {
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 {
callback(req)
}
if method == http.MethodPost && req.Body != nil {
req.SetHeader("Content-Type", "application/json")
}
var e ErrorResp
req.SetError(&e)
res, err := req.Execute(method, d.base+uri)
Expand Down

0 comments on commit aef952a

Please sign in to comment.