github.com/ZuluSpl0it/Sia@v1.3.7/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  	"github.com/NebulousLabs/Sia/modules"
    11  	"github.com/NebulousLabs/Sia/node/api"
    12  )
    13  
    14  // RenterContractsGet requests the /renter/contracts resource and returns
    15  // Contracts and ActiveContracts
    16  func (c *Client) RenterContractsGet() (rc api.RenterContracts, err error) {
    17  	err = c.get("/renter/contracts", &rc)
    18  	return
    19  }
    20  
    21  // RenterInactiveContractsGet requests the /renter/contracts resource with the
    22  // inactive flag set to true
    23  func (c *Client) RenterInactiveContractsGet() (rc api.RenterContracts, err error) {
    24  	query := fmt.Sprintf("?inactive=%v", true)
    25  	err = c.get("/renter/contracts"+query, &rc)
    26  	return
    27  }
    28  
    29  // RenterExpiredContractsGet requests the /renter/contracts resource with the
    30  // expired flag set to true
    31  func (c *Client) RenterExpiredContractsGet() (rc api.RenterContracts, err error) {
    32  	query := fmt.Sprintf("?expired=%v", true)
    33  	err = c.get("/renter/contracts"+query, &rc)
    34  	return
    35  }
    36  
    37  // RenterDeletePost uses the /renter/delete endpoint to delete a file.
    38  func (c *Client) RenterDeletePost(siaPath string) (err error) {
    39  	siaPath = strings.TrimPrefix(siaPath, "/")
    40  	err = c.post(fmt.Sprintf("/renter/delete/%s", siaPath), "", nil)
    41  	return err
    42  }
    43  
    44  // RenterDownloadGet uses the /renter/download endpoint to download a file to a
    45  // destination on disk.
    46  func (c *Client) RenterDownloadGet(siaPath, destination string, offset, length uint64, async bool) (err error) {
    47  	siaPath = strings.TrimPrefix(siaPath, "/")
    48  	query := fmt.Sprintf("%s?destination=%s&offset=%d&length=%d&httpresp=false&async=%v",
    49  		siaPath, destination, offset, length, async)
    50  	err = c.get("/renter/download/"+query, nil)
    51  	return
    52  }
    53  
    54  // RenterDownloadFullGet uses the /renter/download endpoint to download a full
    55  // file.
    56  func (c *Client) RenterDownloadFullGet(siaPath, destination string, async bool) (err error) {
    57  	siaPath = strings.TrimPrefix(siaPath, "/")
    58  	query := fmt.Sprintf("%s?destination=%s&httpresp=false&async=%v",
    59  		siaPath, destination, async)
    60  	err = c.get("/renter/download/"+query, nil)
    61  	return
    62  }
    63  
    64  // RenterClearAllDownloadsPost requests the /renter/downloads/clear resource
    65  // with no parameters
    66  func (c *Client) RenterClearAllDownloadsPost() (err error) {
    67  	err = c.post("/renter/downloads/clear", "", nil)
    68  	return
    69  }
    70  
    71  // RenterClearDownloadsAfterPost requests the /renter/downloads/clear resource
    72  // with only the after timestamp provided
    73  func (c *Client) RenterClearDownloadsAfterPost(after time.Time) (err error) {
    74  	values := url.Values{}
    75  	values.Set("after", strconv.FormatInt(after.UnixNano(), 10))
    76  	err = c.post("/renter/downloads/clear", values.Encode(), nil)
    77  	return
    78  }
    79  
    80  // RenterClearDownloadsBeforePost requests the /renter/downloads/clear resource
    81  // with only the before timestamp provided
    82  func (c *Client) RenterClearDownloadsBeforePost(before time.Time) (err error) {
    83  	values := url.Values{}
    84  	values.Set("before", strconv.FormatInt(before.UnixNano(), 10))
    85  	err = c.post("/renter/downloads/clear", values.Encode(), nil)
    86  	return
    87  }
    88  
    89  // RenterClearDownloadsRangePost requests the /renter/downloads/clear resource
    90  // with both before and after timestamps provided
    91  func (c *Client) RenterClearDownloadsRangePost(after, before time.Time) (err error) {
    92  	values := url.Values{}
    93  	values.Set("before", strconv.FormatInt(before.UnixNano(), 10))
    94  	values.Set("after", strconv.FormatInt(after.UnixNano(), 10))
    95  	err = c.post("/renter/downloads/clear", values.Encode(), nil)
    96  	return
    97  }
    98  
    99  // RenterDownloadsGet requests the /renter/downloads resource
   100  func (c *Client) RenterDownloadsGet() (rdq api.RenterDownloadQueue, err error) {
   101  	err = c.get("/renter/downloads", &rdq)
   102  	return
   103  }
   104  
   105  // RenterDownloadHTTPResponseGet uses the /renter/download endpoint to download
   106  // a file and return its data.
   107  func (c *Client) RenterDownloadHTTPResponseGet(siaPath string, offset, length uint64) (resp []byte, err error) {
   108  	siaPath = strings.TrimPrefix(siaPath, "/")
   109  	query := fmt.Sprintf("%s?offset=%d&length=%d&httpresp=true", siaPath, offset, length)
   110  	resp, err = c.getRawResponse("/renter/download/" + query)
   111  	return
   112  }
   113  
   114  // RenterFileGet uses the /renter/file/:siapath endpoint to query a file.
   115  func (c *Client) RenterFileGet(siaPath string) (rf api.RenterFile, err error) {
   116  	siaPath = strings.TrimPrefix(siaPath, "/")
   117  	err = c.get("/renter/file/"+siaPath, &rf)
   118  	return
   119  }
   120  
   121  // RenterFilesGet requests the /renter/files resource.
   122  func (c *Client) RenterFilesGet() (rf api.RenterFiles, err error) {
   123  	err = c.get("/renter/files", &rf)
   124  	return
   125  }
   126  
   127  // RenterGet requests the /renter resource.
   128  func (c *Client) RenterGet() (rg api.RenterGET, err error) {
   129  	err = c.get("/renter", &rg)
   130  	return
   131  }
   132  
   133  // RenterPostAllowance uses the /renter endpoint to change the renter's allowance
   134  func (c *Client) RenterPostAllowance(allowance modules.Allowance) (err error) {
   135  	values := url.Values{}
   136  	values.Set("funds", allowance.Funds.String())
   137  	values.Set("hosts", strconv.FormatUint(allowance.Hosts, 10))
   138  	values.Set("period", strconv.FormatUint(uint64(allowance.Period), 10))
   139  	values.Set("renewwindow", strconv.FormatUint(uint64(allowance.RenewWindow), 10))
   140  	err = c.post("/renter", values.Encode(), nil)
   141  	return
   142  }
   143  
   144  // RenterCancelAllowance uses the /renter endpoint to cancel the allowance.
   145  func (c *Client) RenterCancelAllowance() (err error) {
   146  	err = c.RenterPostAllowance(modules.Allowance{})
   147  	return
   148  }
   149  
   150  // RenterPricesGet requests the /renter/prices endpoint's resources.
   151  func (c *Client) RenterPricesGet() (rpg api.RenterPricesGET, err error) {
   152  	err = c.get("/renter/prices", &rpg)
   153  	return
   154  }
   155  
   156  // RenterPostRateLimit uses the /renter endpoint to change the renter's bandwidth rate
   157  // limit.
   158  func (c *Client) RenterPostRateLimit(readBPS, writeBPS int64) (err error) {
   159  	values := url.Values{}
   160  	values.Set("maxdownloadspeed", strconv.FormatInt(readBPS, 10))
   161  	values.Set("maxuploadspeed", strconv.FormatInt(writeBPS, 10))
   162  	err = c.post("/renter", values.Encode(), nil)
   163  	return
   164  }
   165  
   166  // RenterRenamePost uses the /renter/rename/:siapath endpoint to rename a file.
   167  func (c *Client) RenterRenamePost(siaPathOld, siaPathNew string) (err error) {
   168  	siaPathOld = strings.TrimPrefix(siaPathOld, "/")
   169  	siaPathNew = strings.TrimPrefix(siaPathNew, "/")
   170  	err = c.post("/renter/rename/"+siaPathOld, "newsiapath=/"+siaPathNew, nil)
   171  	return
   172  }
   173  
   174  // RenterSetStreamCacheSizePost uses the /renter endpoint to change the renter's
   175  // streamCacheSize for streaming
   176  func (c *Client) RenterSetStreamCacheSizePost(cacheSize uint64) (err error) {
   177  	values := url.Values{}
   178  	values.Set("streamcachesize", strconv.FormatUint(cacheSize, 10))
   179  	err = c.post("/renter", values.Encode(), nil)
   180  	return
   181  }
   182  
   183  // RenterStreamGet uses the /renter/stream endpoint to download data as a
   184  // stream.
   185  func (c *Client) RenterStreamGet(siaPath string) (resp []byte, err error) {
   186  	siaPath = strings.TrimPrefix(siaPath, "/")
   187  	resp, err = c.getRawResponse("/renter/stream/" + siaPath)
   188  	return
   189  }
   190  
   191  // RenterStreamPartialGet uses the /renter/stream endpoint to download a part
   192  // of data as a stream.
   193  func (c *Client) RenterStreamPartialGet(siaPath string, start, end uint64) (resp []byte, err error) {
   194  	siaPath = strings.TrimPrefix(siaPath, "/")
   195  	resp, err = c.getRawPartialResponse("/renter/stream/"+siaPath, start, end)
   196  	return
   197  }
   198  
   199  // RenterUploadPost uses the /renter/upload endpoint to upload a file
   200  func (c *Client) RenterUploadPost(path, siaPath string, dataPieces, parityPieces uint64) (err error) {
   201  	siaPath = strings.TrimPrefix(siaPath, "/")
   202  	values := url.Values{}
   203  	values.Set("source", path)
   204  	values.Set("datapieces", strconv.FormatUint(dataPieces, 10))
   205  	values.Set("paritypieces", strconv.FormatUint(parityPieces, 10))
   206  	err = c.post(fmt.Sprintf("/renter/upload/%v", siaPath), values.Encode(), nil)
   207  	return
   208  }
   209  
   210  // RenterUploadDefaultPost uses the /renter/upload endpoint with default
   211  // redundancy settings to upload a file.
   212  func (c *Client) RenterUploadDefaultPost(path, siaPath string) (err error) {
   213  	siaPath = strings.TrimPrefix(siaPath, "/")
   214  	values := url.Values{}
   215  	values.Set("source", path)
   216  	err = c.post(fmt.Sprintf("/renter/upload/%v", siaPath), values.Encode(), nil)
   217  	return
   218  }