Archived
1
0

Update to support new oauth2 API

This commit is contained in:
Arnaud Ysmal 2014-12-06 16:13:45 +01:00
parent a07cde5996
commit c30fa0c6e4
3 changed files with 209 additions and 70 deletions

View File

@ -13,7 +13,7 @@ Installation
------------ ------------
This library depends on the oauth2 package, it can be installed with the go get command: This library depends on the oauth2 package, it can be installed with the go get command:
$ go get github.com/golang/oauth2 $ go get golang.org/x/oauth2
This package can be installed with the go get command: This package can be installed with the go get command:

View File

@ -39,7 +39,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/stacktic/oauth2" "golang.org/x/oauth2"
) )
// ErrNotAuth is the error returned when the OAuth token is not provided // ErrNotAuth is the error returned when the OAuth token is not provided
@ -183,10 +183,20 @@ type Dropbox struct {
APIURL string // Normal API URL. APIURL string // Normal API URL.
APIContentURL string // URL for transferring files. APIContentURL string // URL for transferring files.
APINotifyURL string // URL for realtime notification. APINotifyURL string // URL for realtime notification.
config *oauth2.Config config *oauth2.Options
token *oauth2.Token token *oauth2.Token
} }
// ReadToken returns an existing token (oauth2.TokenStore interface)
func (db *Dropbox) ReadToken() (*oauth2.Token, error) {
return db.token, nil
}
// WriteToken updates the token (oauth2.TokenStore interface)
func (db *Dropbox) WriteToken(t *oauth2.Token) {
db.token = t
}
// NewDropbox returns a new Dropbox configured. // NewDropbox returns a new Dropbox configured.
func NewDropbox() *Dropbox { func NewDropbox() *Dropbox {
db := &Dropbox{ db := &Dropbox{
@ -203,15 +213,23 @@ func NewDropbox() *Dropbox {
// You have to register an application on https://www.dropbox.com/developers/apps. // You have to register an application on https://www.dropbox.com/developers/apps.
func (db *Dropbox) SetAppInfo(clientid, clientsecret string) error { func (db *Dropbox) SetAppInfo(clientid, clientsecret string) error {
var err error var err error
var authURL, tokenURL *url.URL
db.config, err = oauth2.NewConfig( if authURL, err = url.Parse("https://www.dropbox.com/1/oauth2/authorize"); err != nil {
&oauth2.Options{ return err
ClientID: clientid, }
ClientSecret: clientsecret, if tokenURL, err = url.Parse("https://api.dropbox.com/1/oauth2/token"); err != nil {
}, return err
"https://www.dropbox.com/1/oauth2/authorize", }
"https://api.dropbox.com/1/oauth2/token")
return err db.config = &oauth2.Options{
ClientID: clientid,
ClientSecret: clientsecret,
AuthURL: authURL,
TokenURL: tokenURL,
TokenStore: db,
}
return nil
} }
// SetAccessToken sets access token to avoid calling Auth method. // SetAccessToken sets access token to avoid calling Auth method.
@ -226,9 +244,11 @@ func (db *Dropbox) AccessToken() string {
func (db *Dropbox) client() *http.Client { func (db *Dropbox) client() *http.Client {
var t *oauth2.Transport var t *oauth2.Transport
var err error
t = db.config.NewTransport() if t, err = db.config.NewTransportFromTokenStore(db); err != nil {
t.SetToken(db.token) return nil
}
return &http.Client{Transport: t} return &http.Client{Transport: t}
} }
@ -241,7 +261,7 @@ func (db *Dropbox) Auth() error {
fmt.Printf("Please visit:\n%s\nEnter the code: ", fmt.Printf("Please visit:\n%s\nEnter the code: ",
db.config.AuthCodeURL("", "", "")) db.config.AuthCodeURL("", "", ""))
fmt.Scanln(&code) fmt.Scanln(&code)
if t, err = db.config.NewTransportWithCode(code); err != nil { if t, err = db.config.NewTransportFromCode(code); err != nil {
return err return err
} }
db.token = t.Token() db.token = t.Token()

View File

@ -132,9 +132,16 @@ func TestAccountInfo(t *testing.T) {
t.Fatalf("could not run test marshalling issue") t.Fatalf("could not run test marshalling issue")
} }
db.config.Transport = FakeHTTP{t: t, Method: "GET", Host: "api.dropbox.com", Path: "/1/account/info", db.config.Client = &http.Client{
Params: map[string]string{"locale": "en"}, Transport: FakeHTTP{
ResponseData: js} t: t,
Method: "GET",
Host: "api.dropbox.com",
Path: "/1/account/info",
Params: map[string]string{"locale": "en"},
ResponseData: js,
},
}
if received, err = db.GetAccountInfo(); err != nil { if received, err = db.GetAccountInfo(); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
@ -160,12 +167,24 @@ func TestCopy(t *testing.T) {
t.Fatalf("could not run test marshalling issue") t.Fatalf("could not run test marshalling issue")
} }
fake = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/copy", fake = FakeHTTP{
Params: map[string]string{"root": "dropbox", "from_path": from, "to_path": to, "locale": "en"}, t: t,
ResponseData: js} Method: "POST",
Host: "api.dropbox.com",
Path: "/1/fileops/copy",
Params: map[string]string{
"root": "dropbox",
"from_path": from,
"to_path": to,
"locale": "en",
},
ResponseData: js,
}
db = newDropbox(t) db = newDropbox(t)
db.config.Transport = fake db.config.Client = &http.Client{
Transport: fake,
}
if received, err = db.Copy(from, to, false); err != nil { if received, err = db.Copy(from, to, false); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -174,7 +193,6 @@ func TestCopy(t *testing.T) {
delete(fake.Params, "from_path") delete(fake.Params, "from_path")
fake.Params["from_copy_ref"] = from fake.Params["from_copy_ref"] = from
db.config.Transport = fake
if received, err = db.Copy(from, to, true); err != nil { if received, err = db.Copy(from, to, true); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -197,8 +215,16 @@ func TestCopyRef(t *testing.T) {
t.Fatalf("could not run test due to marshalling issue") t.Fatalf("could not run test due to marshalling issue")
} }
db.config.Transport = FakeHTTP{Method: "GET", Host: "api.dropbox.com", Path: "/1/copy_ref/dropbox/" + filename, t: t, db.config.Client = &http.Client{
Params: map[string]string{"locale": "en"}, ResponseData: js} Transport: FakeHTTP{
Method: "GET",
Host: "api.dropbox.com",
Path: "/1/copy_ref/dropbox/" + filename,
t: t,
Params: map[string]string{"locale": "en"},
ResponseData: js,
},
}
if received, err = db.CopyRef(filename); err != nil { if received, err = db.CopyRef(filename); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -221,8 +247,20 @@ func TestCreateFolder(t *testing.T) {
} }
db = newDropbox(t) db = newDropbox(t)
db.config.Transport = FakeHTTP{Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/create_folder", db.config.Client = &http.Client{
Params: map[string]string{"root": "dropbox", "path": foldername, "locale": "en"}, t: t, ResponseData: js} Transport: FakeHTTP{
Method: "POST",
Host: "api.dropbox.com",
Path: "/1/fileops/create_folder",
Params: map[string]string{
"root": "dropbox",
"path": foldername,
"locale": "en",
},
t: t,
ResponseData: js,
},
}
if received, err = db.CreateFolder(foldername); err != nil { if received, err = db.CreateFolder(foldername); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -246,9 +284,20 @@ func TestDelete(t *testing.T) {
} }
db = newDropbox(t) db = newDropbox(t)
db.config.Transport = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/delete", db.config.Client = &http.Client{
Params: map[string]string{"root": "dropbox", "path": path, "locale": "en"}, Transport: FakeHTTP{
ResponseData: js} t: t,
Method: "POST",
Host: "api.dropbox.com",
Path: "/1/fileops/delete",
Params: map[string]string{
"root": "dropbox",
"path": path,
"locale": "en",
},
ResponseData: js,
},
}
if received, err = db.Delete(path); err != nil { if received, err = db.Delete(path); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -276,13 +325,24 @@ func TestFilesPut(t *testing.T) {
t.Fatalf("could not run test marshalling issue") t.Fatalf("could not run test marshalling issue")
} }
fake = FakeHTTP{t: t, Method: "PUT", Host: "api-content.dropbox.com", Path: "/1/files_put/dropbox/" + filename, fake = FakeHTTP{
Params: map[string]string{"locale": "en", "overwrite": "false"}, t: t,
ResponseData: js, RequestData: content} Method: "PUT",
Host: "api-content.dropbox.com",
Path: "/1/files_put/dropbox/" + filename,
Params: map[string]string{
"locale": "en",
"overwrite": "false",
},
ResponseData: js,
RequestData: content,
}
db = newDropbox(t) db = newDropbox(t)
db.config.Client = &http.Client{
Transport: fake,
}
db.config.Transport = fake
received, err = db.FilesPut(ioutil.NopCloser(bytes.NewBuffer(content)), int64(len(content)), filename, false, "") received, err = db.FilesPut(ioutil.NopCloser(bytes.NewBuffer(content)), int64(len(content)), filename, false, "")
if err != nil { if err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
@ -291,7 +351,6 @@ func TestFilesPut(t *testing.T) {
} }
fake.Params["parent_rev"] = "12345" fake.Params["parent_rev"] = "12345"
db.config.Transport = fake
received, err = db.FilesPut(ioutil.NopCloser(bytes.NewBuffer(content)), int64(len(content)), filename, false, "12345") received, err = db.FilesPut(ioutil.NopCloser(bytes.NewBuffer(content)), int64(len(content)), filename, false, "12345")
if err != nil { if err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
@ -300,7 +359,6 @@ func TestFilesPut(t *testing.T) {
} }
fake.Params["overwrite"] = "true" fake.Params["overwrite"] = "true"
db.config.Transport = fake
received, err = db.FilesPut(ioutil.NopCloser(bytes.NewBuffer(content)), int64(len(content)), filename, true, "12345") received, err = db.FilesPut(ioutil.NopCloser(bytes.NewBuffer(content)), int64(len(content)), filename, true, "12345")
if err != nil { if err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
@ -329,8 +387,16 @@ func TestMedia(t *testing.T) {
t.Fatalf("could not run test due to marshalling issue: %s", err) t.Fatalf("could not run test due to marshalling issue: %s", err)
} }
db.config.Transport = FakeHTTP{Method: "POST", Host: "api.dropbox.com", Path: "/1/media/dropbox/" + filename, db.config.Client = &http.Client{
Params: map[string]string{"locale": "en"}, t: t, ResponseData: js} Transport: FakeHTTP{
Method: "POST",
Host: "api.dropbox.com",
Path: "/1/media/dropbox/" + filename,
Params: map[string]string{"locale": "en"},
t: t,
ResponseData: js,
},
}
if received, err = db.Media(filename); err != nil { if received, err = db.Media(filename); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -353,13 +419,24 @@ func TestMetadata(t *testing.T) {
t.Fatalf("could not run test marshalling issue") t.Fatalf("could not run test marshalling issue")
} }
fake = FakeHTTP{t: t, Method: "GET", Host: "api.dropbox.com", Path: "/1/metadata/dropbox/" + path, fake = FakeHTTP{
Params: map[string]string{"list": "false", "include_deleted": "false", "file_limit": "10", "locale": "en"}, t: t,
ResponseData: js} Method: "GET",
Host: "api.dropbox.com",
Path: "/1/metadata/dropbox/" + path,
Params: map[string]string{
"list": "false",
"include_deleted": "false",
"file_limit": "10",
"locale": "en",
},
ResponseData: js,
}
db = newDropbox(t) db = newDropbox(t)
db.config.Client = &http.Client{
Transport: fake,
}
db.config.Transport = fake
if received, err = db.Metadata(path, false, false, "", "", 10); err != nil { if received, err = db.Metadata(path, false, false, "", "", 10); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -367,7 +444,6 @@ func TestMetadata(t *testing.T) {
} }
fake.Params["list"] = "true" fake.Params["list"] = "true"
db.config.Transport = fake
if received, err = db.Metadata(path, true, false, "", "", 10); err != nil { if received, err = db.Metadata(path, true, false, "", "", 10); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -375,7 +451,6 @@ func TestMetadata(t *testing.T) {
} }
fake.Params["include_deleted"] = "true" fake.Params["include_deleted"] = "true"
db.config.Transport = fake
if received, err = db.Metadata(path, true, true, "", "", 10); err != nil { if received, err = db.Metadata(path, true, true, "", "", 10); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -383,7 +458,6 @@ func TestMetadata(t *testing.T) {
} }
fake.Params["file_limit"] = "20" fake.Params["file_limit"] = "20"
db.config.Transport = fake
if received, err = db.Metadata(path, true, true, "", "", 20); err != nil { if received, err = db.Metadata(path, true, true, "", "", 20); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -391,7 +465,6 @@ func TestMetadata(t *testing.T) {
} }
fake.Params["rev"] = "12345" fake.Params["rev"] = "12345"
db.config.Transport = fake
if received, err = db.Metadata(path, true, true, "", "12345", 20); err != nil { if received, err = db.Metadata(path, true, true, "", "12345", 20); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -399,7 +472,6 @@ func TestMetadata(t *testing.T) {
} }
fake.Params["hash"] = "6789" fake.Params["hash"] = "6789"
db.config.Transport = fake
if received, err = db.Metadata(path, true, true, "6789", "12345", 20); err != nil { if received, err = db.Metadata(path, true, true, "6789", "12345", 20); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -407,7 +479,6 @@ func TestMetadata(t *testing.T) {
} }
fake.Params["file_limit"] = "10000" fake.Params["file_limit"] = "10000"
db.config.Transport = fake
if received, err = db.Metadata(path, true, true, "6789", "12345", 0); err != nil { if received, err = db.Metadata(path, true, true, "6789", "12345", 0); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -415,7 +486,6 @@ func TestMetadata(t *testing.T) {
} }
fake.Params["file_limit"] = strconv.FormatInt(int64(MetadataLimitMax), 10) fake.Params["file_limit"] = strconv.FormatInt(int64(MetadataLimitMax), 10)
db.config.Transport = fake
if received, err = db.Metadata(path, true, true, "6789", "12345", MetadataLimitMax+1); err != nil { if received, err = db.Metadata(path, true, true, "6789", "12345", MetadataLimitMax+1); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -440,9 +510,21 @@ func TestMove(t *testing.T) {
} }
db = newDropbox(t) db = newDropbox(t)
db.config.Transport = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/move", db.config.Client = &http.Client{
Params: map[string]string{"root": "dropbox", "from_path": from, "to_path": to, "locale": "en"}, Transport: FakeHTTP{
ResponseData: js} t: t,
Method: "POST",
Host: "api.dropbox.com",
Path: "/1/fileops/move",
Params: map[string]string{
"root": "dropbox",
"from_path": from,
"to_path": to,
"locale": "en",
},
ResponseData: js,
},
}
if received, err = db.Move(from, to); err != nil { if received, err = db.Move(from, to); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -465,9 +547,19 @@ func TestRestore(t *testing.T) {
} }
db = newDropbox(t) db = newDropbox(t)
db.config.Transport = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/restore/dropbox/" + path, db.config.Client = &http.Client{
Params: map[string]string{"rev": expected.Revision, "locale": "en"}, Transport: FakeHTTP{
ResponseData: js} t: t,
Method: "POST",
Host: "api.dropbox.com",
Path: "/1/restore/dropbox/" + path,
Params: map[string]string{
"rev": expected.Revision,
"locale": "en",
},
ResponseData: js,
},
}
if received, err = db.Restore(path, expected.Revision); err != nil { if received, err = db.Restore(path, expected.Revision); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -490,13 +582,22 @@ func TestRevisions(t *testing.T) {
t.Fatalf("could not run test marshalling issue") t.Fatalf("could not run test marshalling issue")
} }
fake = FakeHTTP{t: t, Method: "GET", Host: "api.dropbox.com", Path: "/1/revisions/dropbox/" + path, fake = FakeHTTP{
Params: map[string]string{"rev_limit": "10", "locale": "en"}, t: t,
ResponseData: js} Method: "GET",
Host: "api.dropbox.com",
Path: "/1/revisions/dropbox/" + path,
Params: map[string]string{
"rev_limit": "10",
"locale": "en",
},
ResponseData: js,
}
db = newDropbox(t) db = newDropbox(t)
db.config.Client = &http.Client{
Transport: fake,
}
db.config.Transport = fake
if received, err = db.Revisions(path, 10); err != nil { if received, err = db.Revisions(path, 10); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, received) { } else if !reflect.DeepEqual(expected, received) {
@ -535,10 +636,23 @@ func TestSearch(t *testing.T) {
t.Fatalf("could not run test due to marshalling issue") t.Fatalf("could not run test due to marshalling issue")
} }
fake := FakeHTTP{Method: "GET", Host: "api.dropbox.com", Path: "/1/search/dropbox/" + dirname, t: t, fake := FakeHTTP{
Params: map[string]string{"locale": "en", "query": "foo bar", "file_limit": "10", "include_deleted": "false"}, ResponseData: js} Method: "GET",
Host: "api.dropbox.com",
Path: "/1/search/dropbox/" + dirname,
t: t,
Params: map[string]string{
"locale": "en",
"query": "foo bar",
"file_limit": "10",
"include_deleted": "false",
},
ResponseData: js,
}
db.config.Client = &http.Client{
Transport: fake,
}
db.config.Transport = fake
if received, err = db.Search(dirname, "foo bar", 10, false); err != nil { if received, err = db.Search(dirname, "foo bar", 10, false); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, received) { } else if !reflect.DeepEqual(expected, received) {
@ -546,7 +660,6 @@ func TestSearch(t *testing.T) {
} }
fake.Params["include_deleted"] = "true" fake.Params["include_deleted"] = "true"
db.config.Transport = fake
if received, err = db.Search(dirname, "foo bar", 10, true); err != nil { if received, err = db.Search(dirname, "foo bar", 10, true); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, received) { } else if !reflect.DeepEqual(expected, received) {
@ -554,7 +667,6 @@ func TestSearch(t *testing.T) {
} }
fake.Params["file_limit"] = strconv.FormatInt(int64(SearchLimitDefault), 10) fake.Params["file_limit"] = strconv.FormatInt(int64(SearchLimitDefault), 10)
db.config.Transport = fake
if received, err = db.Search(dirname, "foo bar", 0, true); err != nil { if received, err = db.Search(dirname, "foo bar", 0, true); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, received) { } else if !reflect.DeepEqual(expected, received) {
@ -582,10 +694,18 @@ func TestShares(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("could not run test due to marshalling issue") t.Fatalf("could not run test due to marshalling issue")
} }
fake := FakeHTTP{Method: "POST", Host: "api.dropbox.com", Path: "/1/shares/dropbox/" + filename, fake := FakeHTTP{
Params: map[string]string{"locale": "en"}, t: t, ResponseData: js} Method: "POST",
Host: "api.dropbox.com",
Path: "/1/shares/dropbox/" + filename,
Params: map[string]string{"locale": "en"},
t: t,
ResponseData: js,
}
db.config.Client = &http.Client{
Transport: fake,
}
db.config.Transport = fake
if received, err = db.Shares(filename, false); err != nil { if received, err = db.Shares(filename, false); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {
@ -593,7 +713,6 @@ func TestShares(t *testing.T) {
} }
fake.Params["short_url"] = "true" fake.Params["short_url"] = "true"
db.config.Transport = fake
if received, err = db.Shares(filename, true); err != nil { if received, err = db.Shares(filename, true); err != nil {
t.Errorf("API error: %s", err) t.Errorf("API error: %s", err)
} else if !reflect.DeepEqual(expected, *received) { } else if !reflect.DeepEqual(expected, *received) {