github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/node/api/client/renter.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "net/url" 6 "strconv" 7 "strings" 8 "time" 9 10 "SiaPrime/modules" 11 "SiaPrime/node/api" 12 "SiaPrime/types" 13 ) 14 15 // escapeSiaPath escapes the siapath to make it safe to use within a URL. This 16 // should only be used on SiaPaths which are used as part of the URL path. 17 // Paths within the query have to be escaped with net.QueryEscape. 18 func escapeSiaPath(siaPath string) string { 19 pathSegments := strings.Split(siaPath, "/") 20 21 escapedSegments := make([]string, 0, len(pathSegments)) 22 for _, segment := range pathSegments { 23 escapedSegments = append(escapedSegments, url.PathEscape(segment)) 24 } 25 return strings.Join(escapedSegments, "/") 26 } 27 28 // trimSiaPath trims potential leading slashes within the siaPath. 29 func trimSiaPath(siaPath string) string { 30 return strings.TrimPrefix(siaPath, "/") 31 } 32 33 // RenterContractCancelPost uses the /renter/contract/cancel endpoint to cancel 34 // a contract 35 func (c *Client) RenterContractCancelPost(id types.FileContractID) error { 36 values := url.Values{} 37 values.Set("id", id.String()) 38 err := c.post("/renter/contract/cancel", values.Encode(), nil) 39 return err 40 } 41 42 // RenterContractsGet requests the /renter/contracts resource and returns 43 // Contracts and ActiveContracts 44 func (c *Client) RenterContractsGet() (rc api.RenterContracts, err error) { 45 err = c.get("/renter/contracts", &rc) 46 return 47 } 48 49 // RenterInactiveContractsGet requests the /renter/contracts resource with the 50 // inactive flag set to true 51 func (c *Client) RenterInactiveContractsGet() (rc api.RenterContracts, err error) { 52 values := url.Values{} 53 values.Set("inactive", fmt.Sprint(true)) 54 err = c.get("/renter/contracts?"+values.Encode(), &rc) 55 return 56 } 57 58 // RenterExpiredContractsGet requests the /renter/contracts resource with the 59 // expired flag set to true 60 func (c *Client) RenterExpiredContractsGet() (rc api.RenterContracts, err error) { 61 values := url.Values{} 62 values.Set("expired", fmt.Sprint(true)) 63 err = c.get("/renter/contracts?"+values.Encode(), &rc) 64 return 65 } 66 67 // RenterDeletePost uses the /renter/delete endpoint to delete a file. 68 func (c *Client) RenterDeletePost(siaPath string) (err error) { 69 siaPath = escapeSiaPath(trimSiaPath(siaPath)) 70 err = c.post(fmt.Sprintf("/renter/delete/%s", siaPath), "", nil) 71 return err 72 } 73 74 // RenterDownloadGet uses the /renter/download endpoint to download a file to a 75 // destination on disk. 76 func (c *Client) RenterDownloadGet(siaPath, destination string, offset, length uint64, async bool) (err error) { 77 siaPath = escapeSiaPath(trimSiaPath(siaPath)) 78 values := url.Values{} 79 values.Set("destination", url.QueryEscape(destination)) 80 values.Set("offset", fmt.Sprint(offset)) 81 values.Set("length", fmt.Sprint(length)) 82 values.Set("async", fmt.Sprint(async)) 83 err = c.get(fmt.Sprintf("/renter/download/%s?%s", siaPath, values.Encode()), nil) 84 return 85 } 86 87 // RenterDownloadFullGet uses the /renter/download endpoint to download a full 88 // file. 89 func (c *Client) RenterDownloadFullGet(siaPath, destination string, async bool) (err error) { 90 siaPath = escapeSiaPath(trimSiaPath(siaPath)) 91 values := url.Values{} 92 values.Set("destination", url.QueryEscape(destination)) 93 values.Set("httpresp", fmt.Sprint(false)) 94 values.Set("async", fmt.Sprint(async)) 95 err = c.get(fmt.Sprintf("/renter/download/%s?%s", siaPath, values.Encode()), nil) 96 return 97 } 98 99 // RenterClearAllDownloadsPost requests the /renter/downloads/clear resource 100 // with no parameters 101 func (c *Client) RenterClearAllDownloadsPost() (err error) { 102 err = c.post("/renter/downloads/clear", "", nil) 103 return 104 } 105 106 // RenterClearDownloadsAfterPost requests the /renter/downloads/clear resource 107 // with only the after timestamp provided 108 func (c *Client) RenterClearDownloadsAfterPost(after time.Time) (err error) { 109 values := url.Values{} 110 values.Set("after", strconv.FormatInt(after.UnixNano(), 10)) 111 err = c.post("/renter/downloads/clear", values.Encode(), nil) 112 return 113 } 114 115 // RenterClearDownloadsBeforePost requests the /renter/downloads/clear resource 116 // with only the before timestamp provided 117 func (c *Client) RenterClearDownloadsBeforePost(before time.Time) (err error) { 118 values := url.Values{} 119 values.Set("before", strconv.FormatInt(before.UnixNano(), 10)) 120 err = c.post("/renter/downloads/clear", values.Encode(), nil) 121 return 122 } 123 124 // RenterClearDownloadsRangePost requests the /renter/downloads/clear resource 125 // with both before and after timestamps provided 126 func (c *Client) RenterClearDownloadsRangePost(after, before time.Time) (err error) { 127 values := url.Values{} 128 values.Set("before", strconv.FormatInt(before.UnixNano(), 10)) 129 values.Set("after", strconv.FormatInt(after.UnixNano(), 10)) 130 err = c.post("/renter/downloads/clear", values.Encode(), nil) 131 return 132 } 133 134 // RenterDownloadsGet requests the /renter/downloads resource 135 func (c *Client) RenterDownloadsGet() (rdq api.RenterDownloadQueue, err error) { 136 err = c.get("/renter/downloads", &rdq) 137 return 138 } 139 140 // RenterDownloadHTTPResponseGet uses the /renter/download endpoint to download 141 // a file and return its data. 142 func (c *Client) RenterDownloadHTTPResponseGet(siaPath string, offset, length uint64) (resp []byte, err error) { 143 siaPath = escapeSiaPath(trimSiaPath(siaPath)) 144 values := url.Values{} 145 values.Set("offset", fmt.Sprint(offset)) 146 values.Set("length", fmt.Sprint(length)) 147 values.Set("httpresp", fmt.Sprint(true)) 148 resp, err = c.getRawResponse(fmt.Sprintf("/renter/download/%s?%s", siaPath, values.Encode())) 149 return 150 } 151 152 // RenterFileGet uses the /renter/file/:siapath endpoint to query a file. 153 func (c *Client) RenterFileGet(siaPath string) (rf api.RenterFile, err error) { 154 siaPath = escapeSiaPath(trimSiaPath(siaPath)) 155 err = c.get("/renter/file/"+siaPath, &rf) 156 return 157 } 158 159 // RenterFilesGet requests the /renter/files resource. 160 func (c *Client) RenterFilesGet() (rf api.RenterFiles, err error) { 161 err = c.get("/renter/files", &rf) 162 return 163 } 164 165 // RenterGet requests the /renter resource. 166 func (c *Client) RenterGet() (rg api.RenterGET, err error) { 167 err = c.get("/renter", &rg) 168 return 169 } 170 171 // RenterPostAllowance uses the /renter endpoint to change the renter's allowance 172 func (c *Client) RenterPostAllowance(allowance modules.Allowance) (err error) { 173 values := url.Values{} 174 values.Set("funds", allowance.Funds.String()) 175 values.Set("hosts", fmt.Sprint(allowance.Hosts)) 176 values.Set("period", fmt.Sprint(uint64(allowance.Period))) 177 values.Set("renewwindow", fmt.Sprint(uint64(allowance.RenewWindow))) 178 err = c.post("/renter", values.Encode(), nil) 179 return 180 } 181 182 // RenterCancelAllowance uses the /renter endpoint to cancel the allowance. 183 func (c *Client) RenterCancelAllowance() (err error) { 184 err = c.RenterPostAllowance(modules.Allowance{}) 185 return 186 } 187 188 // RenterPricesGet requests the /renter/prices endpoint's resources. 189 func (c *Client) RenterPricesGet(allowance modules.Allowance) (rpg api.RenterPricesGET, err error) { 190 query := fmt.Sprintf("?funds=%v&hosts=%v&period=%v&renewwindow=%v", 191 allowance.Funds, allowance.Hosts, allowance.Period, allowance.RenewWindow) 192 err = c.get("/renter/prices"+query, &rpg) 193 return 194 } 195 196 // RenterPostRateLimit uses the /renter endpoint to change the renter's bandwidth rate 197 // limit. 198 func (c *Client) RenterPostRateLimit(readBPS, writeBPS int64) (err error) { 199 values := url.Values{} 200 values.Set("maxdownloadspeed", strconv.FormatInt(readBPS, 10)) 201 values.Set("maxuploadspeed", strconv.FormatInt(writeBPS, 10)) 202 err = c.post("/renter", values.Encode(), nil) 203 return 204 } 205 206 // RenterRenamePost uses the /renter/rename/:siapath endpoint to rename a file. 207 func (c *Client) RenterRenamePost(siaPathOld, siaPathNew string) (err error) { 208 siaPathOld = escapeSiaPath(trimSiaPath(siaPathOld)) 209 values := url.Values{} 210 values.Set("newsiapath", fmt.Sprintf("/%s", trimSiaPath(siaPathNew))) 211 err = c.post(fmt.Sprintf("/renter/rename/%s", siaPathOld), values.Encode(), nil) 212 return 213 } 214 215 // RenterSetStreamCacheSizePost uses the /renter endpoint to change the renter's 216 // streamCacheSize for streaming 217 func (c *Client) RenterSetStreamCacheSizePost(cacheSize uint64) (err error) { 218 values := url.Values{} 219 values.Set("streamcachesize", fmt.Sprint(cacheSize)) 220 err = c.post("/renter", values.Encode(), nil) 221 return 222 } 223 224 // RenterStreamGet uses the /renter/stream endpoint to download data as a 225 // stream. 226 func (c *Client) RenterStreamGet(siaPath string) (resp []byte, err error) { 227 siaPath = escapeSiaPath(trimSiaPath(siaPath)) 228 resp, err = c.getRawResponse(fmt.Sprintf("/renter/stream/%s", siaPath)) 229 return 230 } 231 232 // RenterStreamPartialGet uses the /renter/stream endpoint to download a part 233 // of data as a stream. 234 func (c *Client) RenterStreamPartialGet(siaPath string, start, end uint64) (resp []byte, err error) { 235 siaPath = escapeSiaPath(trimSiaPath(siaPath)) 236 resp, err = c.getRawPartialResponse(fmt.Sprintf("/renter/stream/%s", siaPath), start, end) 237 return 238 } 239 240 // RenterSetRepairPathPost uses the /renter/tracking endpoint to set the repair 241 // path of a file to a new location. The file at newPath must exists. 242 func (c *Client) RenterSetRepairPathPost(siaPath, newPath string) (err error) { 243 values := url.Values{} 244 values.Set("trackingpath", newPath) 245 err = c.post("/renter/file/"+siaPath, values.Encode(), nil) 246 return 247 } 248 249 // RenterUploadPost uses the /renter/upload endpoint to upload a file 250 func (c *Client) RenterUploadPost(path, siaPath string, dataPieces, parityPieces uint64) (err error) { 251 siaPath = escapeSiaPath(trimSiaPath(siaPath)) 252 values := url.Values{} 253 values.Set("source", path) 254 values.Set("datapieces", strconv.FormatUint(dataPieces, 10)) 255 values.Set("paritypieces", strconv.FormatUint(parityPieces, 10)) 256 err = c.post(fmt.Sprintf("/renter/upload/%s", siaPath), values.Encode(), nil) 257 return 258 } 259 260 // RenterUploadDefaultPost uses the /renter/upload endpoint with default 261 // redundancy settings to upload a file. 262 func (c *Client) RenterUploadDefaultPost(path, siaPath string) (err error) { 263 siaPath = escapeSiaPath(trimSiaPath(siaPath)) 264 values := url.Values{} 265 values.Set("source", path) 266 err = c.post(fmt.Sprintf("/renter/upload/%s", siaPath), values.Encode(), nil) 267 return 268 }