From 113ba79265cf5a3fed1584180649310e8c5b56d5 Mon Sep 17 00:00:00 2001 From: Artiom Di Date: Thu, 18 Dec 2014 16:42:59 +0200 Subject: [PATCH] Added (*Dropbox).LatestCursor() call and tests See: https://www.dropbox.com/developers/core/docs#delta-latest-cursor --- dropbox.go | 28 +++++++++++++++++++--- dropbox_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/dropbox.go b/dropbox.go index 021eea5..63d77a2 100644 --- a/dropbox.go +++ b/dropbox.go @@ -68,7 +68,7 @@ type CopyRef struct { type DeltaPage struct { Reset bool // if true the local state must be cleared. HasMore bool // if true an other call to delta should be made. - Cursor string // Tag of the current state. + Cursor // Tag of the current state. Entries []DeltaEntry // List of changed entries. } @@ -91,6 +91,10 @@ type ChunkUploadResponse struct { Expires DBTime `json:"expires"` // Expiration time of this upload. } +type Cursor struct { + Cursor string `json:"cursor"` +} + // Format of reply when http error code is not 200. // Format may be: // {"error": "reason"} @@ -684,8 +688,8 @@ func (db *Dropbox) Delta(cursor, pathPrefix string) (*DeltaPage, error) { type deltaPageParser struct { Reset bool `json:"reset"` // if true the local state must be cleared. HasMore bool `json:"has_more"` // if true an other call to delta should be made. - Cursor string `json:"cursor"` // Tag of the current state. - Entries [][]json.RawMessage `json:"entries"` // List of changed entries. + Cursor // Tag of the current state. + Entries [][]json.RawMessage `json:"entries"` // List of changed entries. } var dpp deltaPageParser @@ -854,3 +858,21 @@ func (db *Dropbox) Move(src, dst string) (*Entry, error) { "to_path": {dst}}, &rv) return &rv, err } + +func (db *Dropbox) LatestCursor(prefix string, mediaInfo bool) (*Cursor, error) { + var ( + params = &url.Values{} + cur Cursor + ) + + if prefix != "" { + params.Set("path_prefix", prefix) + } + + if mediaInfo { + params.Set("include_media_info", "true") + } + + err := db.doRequest("POST", "delta/latest_cursor", params, &cur) + return &cur, err +} diff --git a/dropbox_test.go b/dropbox_test.go index 68607b0..e78dfa1 100644 --- a/dropbox_test.go +++ b/dropbox_test.go @@ -719,3 +719,65 @@ func TestShares(t *testing.T) { t.Errorf("got %#v expected %#v", *received, expected) } } + +func TestLatestCursor(t *testing.T) { + tab := []struct { + prefix string + mediaInfo bool + }{ + { + prefix: "", + mediaInfo: false, + }, + { + prefix: "/some", + mediaInfo: false, + }, + { + prefix: "", + mediaInfo: true, + }, + { + prefix: "/some", + mediaInfo: true, + }, + } + + expected := Cursor{Cursor: "some"} + cur, err := json.Marshal(expected) + if err != nil { + t.Fatal("Failed to JSON encode Cursor") + } + + for _, testCase := range tab { + db := newDropbox(t) + fake := FakeHTTP{ + Method: "POST", + Host: "api.dropbox.com", + Path: "/1/delta/latest_cursor", + t: t, + Params: map[string]string{ + "locale": "en", + }, + ResponseData: cur, + } + + if testCase.prefix != "" { + fake.Params["path_prefix"] = testCase.prefix + } + + if testCase.mediaInfo { + fake.Params["include_media_info"] = "true" + } + + http.DefaultClient = &http.Client{ + Transport: fake, + } + + if received, err := db.LatestCursor(testCase.prefix, testCase.mediaInfo); err != nil { + t.Errorf("API error: %s", err) + } else if !reflect.DeepEqual(expected, *received) { + t.Errorf("got %#v expected %#v", *received, expected) + } + } +}