Skip to content

Commit 6613abe

Browse files
author
Diwaker Gupta
committed
Add handling for Retry-After header.
Handles both plain-text and JSON responses. Include unit tests. Fixes #33 Also updated API spec.
1 parent 5ea6cfa commit 6613abe

File tree

18 files changed

+1924
-481
lines changed

18 files changed

+1924
-481
lines changed

dropbox/auth/client.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (dbx *apiImpl) TokenFromOauth1(arg *TokenFromOAuth1Arg) (res *TokenFromOAut
8080
return
8181
}
8282

83-
dbx.Config.LogDebug("body: %v", body)
83+
dbx.Config.LogDebug("body: %s", body)
8484
if resp.StatusCode == http.StatusOK {
8585
err = json.Unmarshal(body, &res)
8686
if err != nil {
@@ -98,7 +98,11 @@ func (dbx *apiImpl) TokenFromOauth1(arg *TokenFromOAuth1Arg) (res *TokenFromOAut
9898
err = apiError
9999
return
100100
}
101-
err = dropbox.HandleCommonAPIErrors(resp, body)
101+
err = HandleCommonAuthErrors(dbx.Config, resp, body)
102+
if err != nil {
103+
return
104+
}
105+
err = dropbox.HandleCommonAPIErrors(dbx.Config, resp, body)
102106
return
103107
}
104108

@@ -134,7 +138,7 @@ func (dbx *apiImpl) TokenRevoke() (err error) {
134138
return
135139
}
136140

137-
dbx.Config.LogDebug("body: %v", body)
141+
dbx.Config.LogDebug("body: %s", body)
138142
if resp.StatusCode == http.StatusOK {
139143
return
140144
}
@@ -147,7 +151,11 @@ func (dbx *apiImpl) TokenRevoke() (err error) {
147151
err = apiError
148152
return
149153
}
150-
err = dropbox.HandleCommonAPIErrors(resp, body)
154+
err = HandleCommonAuthErrors(dbx.Config, resp, body)
155+
if err != nil {
156+
return
157+
}
158+
err = dropbox.HandleCommonAPIErrors(dbx.Config, resp, body)
151159
return
152160
}
153161

dropbox/auth/sdk.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package auth
2+
3+
import (
4+
"encoding/json"
5+
"mime"
6+
"net/http"
7+
"strconv"
8+
9+
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox"
10+
)
11+
12+
type RateLimitAPIError struct {
13+
dropbox.APIError
14+
RateLimitError *RateLimitError `json:"error"`
15+
}
16+
17+
// HandleCommonAuthErrors handles common authentication errors
18+
func HandleCommonAuthErrors(c dropbox.Config, resp *http.Response, body []byte) error {
19+
if resp.StatusCode == http.StatusTooManyRequests {
20+
var apiError RateLimitAPIError
21+
// Check content-type
22+
contentType, _, _ := mime.ParseMediaType(resp.Header.Get("content-type"))
23+
if contentType == "application/json" {
24+
if err := json.Unmarshal(body, &apiError); err != nil {
25+
c.LogDebug("Error unmarshaling '%s' into JSON", body)
26+
return err
27+
}
28+
} else { // assume plain text
29+
apiError.ErrorSummary = string(body)
30+
reason := RateLimitReason{dropbox.Tagged{Tag: RateLimitReasonTooManyRequests}}
31+
apiError.RateLimitError = NewRateLimitError(&reason)
32+
timeout, _ := strconv.ParseInt(resp.Header.Get("retry-after"), 10, 64)
33+
apiError.RateLimitError.RetryAfter = uint64(timeout)
34+
}
35+
return apiError
36+
}
37+
return nil
38+
}

dropbox/contacts/client.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"net/http"
2828

2929
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox"
30+
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox/auth"
3031
)
3132

3233
// Client interface describes all routes in this namespace
@@ -74,7 +75,7 @@ func (dbx *apiImpl) DeleteManualContacts() (err error) {
7475
return
7576
}
7677

77-
dbx.Config.LogDebug("body: %v", body)
78+
dbx.Config.LogDebug("body: %s", body)
7879
if resp.StatusCode == http.StatusOK {
7980
return
8081
}
@@ -87,7 +88,11 @@ func (dbx *apiImpl) DeleteManualContacts() (err error) {
8788
err = apiError
8889
return
8990
}
90-
err = dropbox.HandleCommonAPIErrors(resp, body)
91+
err = auth.HandleCommonAuthErrors(dbx.Config, resp, body)
92+
if err != nil {
93+
return
94+
}
95+
err = dropbox.HandleCommonAPIErrors(dbx.Config, resp, body)
9196
return
9297
}
9398

@@ -131,7 +136,7 @@ func (dbx *apiImpl) DeleteManualContactsBatch(arg *DeleteManualContactsArg) (err
131136
return
132137
}
133138

134-
dbx.Config.LogDebug("body: %v", body)
139+
dbx.Config.LogDebug("body: %s", body)
135140
if resp.StatusCode == http.StatusOK {
136141
return
137142
}
@@ -144,7 +149,11 @@ func (dbx *apiImpl) DeleteManualContactsBatch(arg *DeleteManualContactsArg) (err
144149
err = apiError
145150
return
146151
}
147-
err = dropbox.HandleCommonAPIErrors(resp, body)
152+
err = auth.HandleCommonAuthErrors(dbx.Config, resp, body)
153+
if err != nil {
154+
return
155+
}
156+
err = dropbox.HandleCommonAPIErrors(dbx.Config, resp, body)
148157
return
149158
}
150159

0 commit comments

Comments
 (0)