-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbtsync_api.go
277 lines (207 loc) · 6.67 KB
/
btsync_api.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Provides a client for interacting with the Bittorrent Sync API.
package btsync_api
import (
// "fmt"
"log"
"os"
"strconv"
"strings"
)
const endpoint = "http://127.0.0.1:%d/api/?"
// Client used to connect to the Bittorrent Sync API.
type BTSyncAPI struct {
Username string
Password string
Port int
Endpoint string
Debug bool
Logger *log.Logger
}
// Instantiates a new BTSyncAPI.
func New(login string, password string, port int, debug bool) *BTSyncAPI {
logger := log.New(os.Stdout, "[BTSyncAPI] ", log.Ldate|log.Ltime)
return &BTSyncAPI{login, password, port, endpoint, debug, logger}
}
// Make a request to the API. Requires the method name and a map
// of query string parameters.
func (api *BTSyncAPI) Request(method string, args map[string]string) *Request {
return &Request{
API: api,
Method: method,
Args: args,
}
}
// Add a folder to Sync with the given secret.
func (api *BTSyncAPI) AddFolderWithSecret(folder string, secret string) (response *Response, err error) {
args := map[string]string{
"dir": folder,
}
if secret != "" {
args["secret"] = secret
}
request := api.Request("add_folder", args)
response = &Response{}
err = request.GetResponse(response)
return
}
// Add a new folder to Sync.
func (api *BTSyncAPI) AddFolder(folder string) (*Response, error) {
return api.AddFolderWithSecret(folder, "")
}
// Remove a folder from Sync using its secret.
func (api *BTSyncAPI) RemoveFolder(secret string) (response *Response, err error) {
request := api.Request("remove_folder", map[string]string{
"secret": secret,
})
response = &Response{}
err = request.GetResponse(response)
return
}
// Get information about a folder.
func (api *BTSyncAPI) GetFolder(secret string) (response *GetFoldersResponse, err error) {
args := map[string]string{}
if secret != "" {
args["secret"] = secret
}
request := api.Request("get_folders", args)
response = &GetFoldersResponse{}
err = request.GetResponse(response)
return
}
// Get a list of all folders.
func (api *BTSyncAPI) GetFolders() (*GetFoldersResponse, error) {
return api.GetFolder("")
}
// Get a list of all files for the given secret under the given path.
func (api *BTSyncAPI) GetFilesForPath(secret string, path string) (response *GetFilesResponse, err error) {
args := map[string]string{
"secret": secret,
}
if path != "" {
args["path"] = path
}
request := api.Request("get_files", args)
response = &GetFilesResponse{}
err = request.GetResponse(response)
return
}
// Get a list of all files for a folder.
func (api *BTSyncAPI) GetFiles(secret string) (*GetFilesResponse, error) {
return api.GetFilesForPath(secret, "")
}
// Set preferences for a file.
func (api *BTSyncAPI) SetFilePrefs(secret string, path string, download int) (response *SetFilePrefsResponse, err error) {
request := api.Request("set_file_prefs", map[string]string{
"secret": secret,
"path": path,
"download": strconv.Itoa(download),
})
response = &SetFilePrefsResponse{}
err = request.GetResponse(response)
return
}
// Get a list of all peers for a folder.
func (api *BTSyncAPI) GetFolderPeers(secret string) (response *GetFolderPeersResponse, err error) {
request := api.Request("get_folder_peers", map[string]string{
"secret": secret,
})
response = &GetFolderPeersResponse{}
err = request.GetResponse(response)
return
}
// Get the secrets for a folder.
func (api *BTSyncAPI) GetSecretsForSecret(secret string) (response *GetSecretsResponse, err error) {
request := api.Request("get_secrets", map[string]string{
"secret": secret,
})
response = &GetSecretsResponse{}
err = request.GetResponse(response)
return
}
// Generate new secrets.
func (api *BTSyncAPI) GetSecrets(encryption bool) (response *GetSecretsResponse, err error) {
args := map[string]string{}
if encryption {
args["type"] = "encryption"
}
request := api.Request("get_secrets", args)
response = &GetSecretsResponse{}
err = request.GetResponse(response)
return
}
// Get preferences for a folder.
func (api *BTSyncAPI) GetFolderPrefs(secret string) (response *GetFolderPrefsResponse, err error) {
request := api.Request("get_folder_prefs", map[string]string{
"secret": secret,
})
response = &GetFolderPrefsResponse{}
err = request.GetResponse(response)
return
}
// Set preferences for a folder.
func (api *BTSyncAPI) SetFolderPrefs(secret string, prefs *FolderPreferences) (response *SetFolderPrefsResponse, err error) {
args := structToMap(prefs)
args["secret"] = secret
request := api.Request("set_folder_prefs", args)
response = &SetFolderPrefsResponse{}
err = request.GetResponse(response)
return
}
// Get a list of hosts for a folder.
func (api *BTSyncAPI) GetFolderHosts(secret string) (response *GetFolderHostsResponse, err error) {
request := api.Request("get_folder_hosts", map[string]string{
"secret": secret,
})
response = &GetFolderHostsResponse{}
err = request.GetResponse(response)
return
}
// Set the list of hosts for a folder.
func (api *BTSyncAPI) SetFolderHosts(secret string, hosts []string) (response *Response, err error) {
request := api.Request("set_folder_hosts", map[string]string{
"secret": secret,
"hosts": strings.Join(hosts, ","),
})
response = &Response{}
err = request.GetResponse(response)
return
}
// Get Sync preferences.
func (api *BTSyncAPI) GetPreferences() (response *GetPreferencesResponse, err error) {
request := api.Request("get_prefs", map[string]string{})
response = &GetPreferencesResponse{}
err = request.GetResponse(response)
return
}
// Set Sync preferences.
func (api *BTSyncAPI) SetPreferences(prefs Preferences) (response *Response, err error) {
request := api.Request("set_prefs", map[string]string{})
prefsMap := structToMap(prefs)
for key, value := range prefsMap {
request.Args[key] = string(value)
}
response = &Response{}
err = request.GetResponse(response)
return
}
// Get name of OS.
func (api *BTSyncAPI) GetOS() (response *GetOSResponse, err error) {
request := api.Request("get_os", map[string]string{})
response = &GetOSResponse{}
err = request.GetResponse(response)
return
}
// Get Bittorrent Sync version.
func (api *BTSyncAPI) GetVersion() (response *GetVersionResponse, err error) {
request := api.Request("get_version", map[string]string{})
response = &GetVersionResponse{}
err = request.GetResponse(response)
return
}
// Get current upload and download speed.
func (api *BTSyncAPI) GetSpeed() (response *GetSpeedResponse, err error) {
request := api.Request("get_speed", map[string]string{})
response = &GetSpeedResponse{}
err = request.GetResponse(response)
return
}