forked from NebulousLabs/Sia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renter.go
218 lines (191 loc) · 7.87 KB
/
renter.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
package client
import (
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/node/api"
)
// RenterContractsGet requests the /renter/contracts resource and returns
// Contracts and ActiveContracts
func (c *Client) RenterContractsGet() (rc api.RenterContracts, err error) {
err = c.get("/renter/contracts", &rc)
return
}
// RenterInactiveContractsGet requests the /renter/contracts resource with the
// inactive flag set to true
func (c *Client) RenterInactiveContractsGet() (rc api.RenterContracts, err error) {
query := fmt.Sprintf("?inactive=%v", true)
err = c.get("/renter/contracts"+query, &rc)
return
}
// RenterExpiredContractsGet requests the /renter/contracts resource with the
// expired flag set to true
func (c *Client) RenterExpiredContractsGet() (rc api.RenterContracts, err error) {
query := fmt.Sprintf("?expired=%v", true)
err = c.get("/renter/contracts"+query, &rc)
return
}
// RenterDeletePost uses the /renter/delete endpoint to delete a file.
func (c *Client) RenterDeletePost(siaPath string) (err error) {
siaPath = strings.TrimPrefix(siaPath, "/")
err = c.post(fmt.Sprintf("/renter/delete/%s", siaPath), "", nil)
return err
}
// RenterDownloadGet uses the /renter/download endpoint to download a file to a
// destination on disk.
func (c *Client) RenterDownloadGet(siaPath, destination string, offset, length uint64, async bool) (err error) {
siaPath = strings.TrimPrefix(siaPath, "/")
query := fmt.Sprintf("%s?destination=%s&offset=%d&length=%d&httpresp=false&async=%v",
siaPath, destination, offset, length, async)
err = c.get("/renter/download/"+query, nil)
return
}
// RenterDownloadFullGet uses the /renter/download endpoint to download a full
// file.
func (c *Client) RenterDownloadFullGet(siaPath, destination string, async bool) (err error) {
siaPath = strings.TrimPrefix(siaPath, "/")
query := fmt.Sprintf("%s?destination=%s&httpresp=false&async=%v",
siaPath, destination, async)
err = c.get("/renter/download/"+query, nil)
return
}
// RenterClearAllDownloadsPost requests the /renter/downloads/clear resource
// with no parameters
func (c *Client) RenterClearAllDownloadsPost() (err error) {
err = c.post("/renter/downloads/clear", "", nil)
return
}
// RenterClearDownloadsAfterPost requests the /renter/downloads/clear resource
// with only the after timestamp provided
func (c *Client) RenterClearDownloadsAfterPost(after time.Time) (err error) {
values := url.Values{}
values.Set("after", strconv.FormatInt(after.UnixNano(), 10))
err = c.post("/renter/downloads/clear", values.Encode(), nil)
return
}
// RenterClearDownloadsBeforePost requests the /renter/downloads/clear resource
// with only the before timestamp provided
func (c *Client) RenterClearDownloadsBeforePost(before time.Time) (err error) {
values := url.Values{}
values.Set("before", strconv.FormatInt(before.UnixNano(), 10))
err = c.post("/renter/downloads/clear", values.Encode(), nil)
return
}
// RenterClearDownloadsRangePost requests the /renter/downloads/clear resource
// with both before and after timestamps provided
func (c *Client) RenterClearDownloadsRangePost(after, before time.Time) (err error) {
values := url.Values{}
values.Set("before", strconv.FormatInt(before.UnixNano(), 10))
values.Set("after", strconv.FormatInt(after.UnixNano(), 10))
err = c.post("/renter/downloads/clear", values.Encode(), nil)
return
}
// RenterDownloadsGet requests the /renter/downloads resource
func (c *Client) RenterDownloadsGet() (rdq api.RenterDownloadQueue, err error) {
err = c.get("/renter/downloads", &rdq)
return
}
// RenterDownloadHTTPResponseGet uses the /renter/download endpoint to download
// a file and return its data.
func (c *Client) RenterDownloadHTTPResponseGet(siaPath string, offset, length uint64) (resp []byte, err error) {
siaPath = strings.TrimPrefix(siaPath, "/")
query := fmt.Sprintf("%s?offset=%d&length=%d&httpresp=true", siaPath, offset, length)
resp, err = c.getRawResponse("/renter/download/" + query)
return
}
// RenterFileGet uses the /renter/file/:siapath endpoint to query a file.
func (c *Client) RenterFileGet(siaPath string) (rf api.RenterFile, err error) {
siaPath = strings.TrimPrefix(siaPath, "/")
err = c.get("/renter/file/"+siaPath, &rf)
return
}
// RenterFilesGet requests the /renter/files resource.
func (c *Client) RenterFilesGet() (rf api.RenterFiles, err error) {
err = c.get("/renter/files", &rf)
return
}
// RenterGet requests the /renter resource.
func (c *Client) RenterGet() (rg api.RenterGET, err error) {
err = c.get("/renter", &rg)
return
}
// RenterPostAllowance uses the /renter endpoint to change the renter's allowance
func (c *Client) RenterPostAllowance(allowance modules.Allowance) (err error) {
values := url.Values{}
values.Set("funds", allowance.Funds.String())
values.Set("hosts", strconv.FormatUint(allowance.Hosts, 10))
values.Set("period", strconv.FormatUint(uint64(allowance.Period), 10))
values.Set("renewwindow", strconv.FormatUint(uint64(allowance.RenewWindow), 10))
err = c.post("/renter", values.Encode(), nil)
return
}
// RenterCancelAllowance uses the /renter endpoint to cancel the allowance.
func (c *Client) RenterCancelAllowance() (err error) {
err = c.RenterPostAllowance(modules.Allowance{})
return
}
// RenterPricesGet requests the /renter/prices endpoint's resources.
func (c *Client) RenterPricesGet() (rpg api.RenterPricesGET, err error) {
err = c.get("/renter/prices", &rpg)
return
}
// RenterPostRateLimit uses the /renter endpoint to change the renter's bandwidth rate
// limit.
func (c *Client) RenterPostRateLimit(readBPS, writeBPS int64) (err error) {
values := url.Values{}
values.Set("maxdownloadspeed", strconv.FormatInt(readBPS, 10))
values.Set("maxuploadspeed", strconv.FormatInt(writeBPS, 10))
err = c.post("/renter", values.Encode(), nil)
return
}
// RenterRenamePost uses the /renter/rename/:siapath endpoint to rename a file.
func (c *Client) RenterRenamePost(siaPathOld, siaPathNew string) (err error) {
siaPathOld = strings.TrimPrefix(siaPathOld, "/")
siaPathNew = strings.TrimPrefix(siaPathNew, "/")
err = c.post("/renter/rename/"+siaPathOld, "newsiapath=/"+siaPathNew, nil)
return
}
// RenterSetStreamCacheSizePost uses the /renter endpoint to change the renter's
// streamCacheSize for streaming
func (c *Client) RenterSetStreamCacheSizePost(cacheSize uint64) (err error) {
values := url.Values{}
values.Set("streamcachesize", strconv.FormatUint(cacheSize, 10))
err = c.post("/renter", values.Encode(), nil)
return
}
// RenterStreamGet uses the /renter/stream endpoint to download data as a
// stream.
func (c *Client) RenterStreamGet(siaPath string) (resp []byte, err error) {
siaPath = strings.TrimPrefix(siaPath, "/")
resp, err = c.getRawResponse("/renter/stream/" + siaPath)
return
}
// RenterStreamPartialGet uses the /renter/stream endpoint to download a part
// of data as a stream.
func (c *Client) RenterStreamPartialGet(siaPath string, start, end uint64) (resp []byte, err error) {
siaPath = strings.TrimPrefix(siaPath, "/")
resp, err = c.getRawPartialResponse("/renter/stream/"+siaPath, start, end)
return
}
// RenterUploadPost uses the /renter/upload endpoint to upload a file
func (c *Client) RenterUploadPost(path, siaPath string, dataPieces, parityPieces uint64) (err error) {
siaPath = strings.TrimPrefix(siaPath, "/")
values := url.Values{}
values.Set("source", path)
values.Set("datapieces", strconv.FormatUint(dataPieces, 10))
values.Set("paritypieces", strconv.FormatUint(parityPieces, 10))
err = c.post(fmt.Sprintf("/renter/upload/%v", siaPath), values.Encode(), nil)
return
}
// RenterUploadDefaultPost uses the /renter/upload endpoint with default
// redundancy settings to upload a file.
func (c *Client) RenterUploadDefaultPost(path, siaPath string) (err error) {
siaPath = strings.TrimPrefix(siaPath, "/")
values := url.Values{}
values.Set("source", path)
err = c.post(fmt.Sprintf("/renter/upload/%v", siaPath), values.Encode(), nil)
return
}