forked from gilbertchen/go-dropbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
148 lines (130 loc) · 3.48 KB
/
Copy pathclient.go
File metadata and controls
148 lines (130 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package dropbox
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"log"
)
// Client implements a Dropbox client. You may use the Files and Users
// clients directly if preferred, however Client exposes them both.
type Client struct {
*Config
Users *Users
Files *Files
Sharing *Sharing
}
// New client.
func New(config *Config) *Client {
c := &Client{Config: config}
c.Users = &Users{c}
c.Files = &Files{c}
c.Sharing = &Sharing{c}
return c
}
// call rpc style endpoint.
func (c *Client) call(path string, in interface{}) (io.ReadCloser, error) {
url := "https://api.dropboxapi.com/2" + path
body, err := json.Marshal(in)
if err != nil {
return nil, err
}
reader := bytes.NewReader(body)
req, err := http.NewRequest("POST", url, reader)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.AccessToken)
req.Header.Set("Content-Type", "application/json")
r, _, err := c.do(req, reader)
return r, err
}
// download style endpoint.
func (c *Client) download(path string, in interface{}, r io.ReadSeeker, contentLength int64) (io.ReadCloser, int64, error) {
url := "https://content.dropboxapi.com/2" + path
body, err := json.Marshal(in)
if err != nil {
return nil, 0, err
}
req, err := http.NewRequest("POST", url, r)
if err != nil {
return nil, 0, err
}
req.Header.Set("Authorization", "Bearer "+c.AccessToken)
req.Header.Set("Dropbox-API-Arg", string(body))
if contentLength != 0 {
req.ContentLength = contentLength
}
if r != nil {
req.Header.Set("Content-Type", "application/octet-stream")
}
return c.do(req, r)
}
// perform the request.
func (c *Client) do(req *http.Request, seeker io.Seeker) (io.ReadCloser, int64, error) {
var err error
var res *http.Response
error_retry_time := 0.5
request_loop:
for error_retry_time < 300 {
res, err = c.HTTPClient.Do(req)
if err != nil {
log.Printf("[DROPBOX_RETRY] %v; retrying after %.2f seconds", err, error_retry_time)
time.Sleep(time.Duration(error_retry_time) * time.Second)
error_retry_time *= 1.5
if seeker != nil {
seeker.Seek(0, io.SeekStart)
}
continue
}
switch {
case res.StatusCode == 429:
log.Printf("[DROPBOX_RETRY] %s %s returned %d; retrying after %.2f seconds", req.Method, req.URL, res.StatusCode, error_retry_time)
sleep_time, conv_e := strconv.Atoi(res.Header.Get("Retry-After"))
if conv_e != nil {
sleep_time = 60
}
time.Sleep(time.Duration(sleep_time) * time.Second)
if seeker != nil {
seeker.Seek(0, io.SeekStart)
}
case res.StatusCode >= 500: // Retry on 5xx
log.Printf("[DROPBOX_RETRY] %s %s returned %d; retrying after %.2f seconds", req.Method, req.URL, res.StatusCode, error_retry_time)
time.Sleep(time.Duration(error_retry_time) * time.Second)
error_retry_time *= 1.5
if seeker != nil {
seeker.Seek(0, io.SeekStart)
}
default:
break request_loop
}
}
if err != nil {
return nil, 0, err
}
if res.StatusCode < 400 {
return res.Body, res.ContentLength, err
}
defer res.Body.Close()
e := &Error{
Status: http.StatusText(res.StatusCode),
StatusCode: res.StatusCode,
}
kind := res.Header.Get("Content-Type")
if strings.Contains(kind, "text/plain") {
if b, err := ioutil.ReadAll(res.Body); err == nil {
e.Summary = string(b)
return nil, 0, e
} else {
return nil, 0, err
}
}
if err := json.NewDecoder(res.Body).Decode(e); err != nil {
return nil, 0, err
}
return nil, 0, e
}