Skip to content

Commit 3f67cde

Browse files
martylukyyzze0s
andauthored
feat(methods): add SyncMainData and GetFreeSpaceOnDisk (#26)
* feat: GetFreeSpaceonDisk * optimize memory usage * optimize code further * feat: add full maindata method incl. ServerState object * feat(methods): add SyncMainData --------- Co-authored-by: ze0s <[email protected]>
1 parent 54dcf32 commit 3f67cde

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

domain.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,3 +586,43 @@ type AppPreferences struct {
586586
WebUIUseCustomHTTPHeadersEnabled bool `json:"web_ui_use_custom_http_headers_enabled"`
587587
WebUIUsername string `json:"web_ui_username"`
588588
}
589+
590+
type MainData struct {
591+
Rid int `json:"rid"`
592+
FullUpdate bool `json:"full_update"`
593+
Torrents map[string]Torrent `json:"torrents"`
594+
TorrentsRemoved []string `json:"torrents_removed"`
595+
Categories map[string]Category `json:"categories"`
596+
CategoriesRemoved []string `json:"categories_removed"`
597+
Tags []string `json:"tags"`
598+
TagsRemoved []string `json:"tags_removed"`
599+
Trackers map[string][]string `json:"trackers"`
600+
ServerState ServerState `json:"server_state"`
601+
}
602+
603+
type ServerState struct {
604+
AlltimeDl int64 `json:"alltime_dl"`
605+
AlltimeUl int64 `json:"alltime_ul"`
606+
AverageTimeQueue int `json:"average_time_queue"`
607+
ConnectionStatus string `json:"connection_status"`
608+
DhtNodes int `json:"dht_nodes"`
609+
DlInfoData int64 `json:"dl_info_data"`
610+
DlInfoSpeed int `json:"dl_info_speed"`
611+
DlRateLimit int `json:"dl_rate_limit"`
612+
FreeSpaceOnDisk uint64 `json:"free_space_on_disk"`
613+
GlobalRatio string `json:"global_ratio"`
614+
QueuedIoJobs int `json:"queued_io_jobs"`
615+
Queueing bool `json:"queueing"`
616+
ReadCacheHits string `json:"read_cache_hits"`
617+
ReadCacheOverload string `json:"read_cache_overload"`
618+
RefreshInterval int `json:"refresh_interval"`
619+
TotalBuffersSize int `json:"total_buffers_size"`
620+
TotalPeerConnections int `json:"total_peer_connections"`
621+
TotalQueuedSize int `json:"total_queued_size"`
622+
TotalWastedSession int64 `json:"total_wasted_session"`
623+
UpInfoData int64 `json:"up_info_data"`
624+
UpInfoSpeed int `json:"up_info_speed"`
625+
UpRateLimit int `json:"up_rate_limit"`
626+
UseAltSpeedLimits bool `json:"use_alt_speed_limits"`
627+
WriteCacheOverload string `json:"write_cache_overload"`
628+
}

methods.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,27 @@ func (c *Client) GetTransferInfoCtx(ctx context.Context) (*TransferInfo, error)
468468
return &info, nil
469469
}
470470

471+
// SyncMainDataCtx Sync API implements requests for obtaining changes since the last request.
472+
// Response ID. If not provided, rid=0 will be assumed. If the given rid is different from the one of last server reply, full_update will be true (see the server reply details for more info)
473+
func (c *Client) SyncMainDataCtx(ctx context.Context, rid int64) (*MainData, error) {
474+
opts := map[string]string{
475+
"rid": strconv.FormatInt(rid, 10),
476+
}
477+
478+
resp, err := c.getCtx(ctx, "/sync/maindata", opts)
479+
if err != nil {
480+
return nil, errors.Wrap(err, "could not get main data")
481+
}
482+
483+
var info MainData
484+
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
485+
return nil, errors.Wrap(err, "could not unmarshal body")
486+
}
487+
488+
return &info, nil
489+
490+
}
491+
471492
func (c *Client) Pause(hashes []string) error {
472493
return c.PauseCtx(context.Background(), hashes)
473494
}
@@ -1267,6 +1288,20 @@ func (c *Client) GetWebAPIVersionCtx(ctx context.Context) (string, error) {
12671288
return string(body), nil
12681289
}
12691290

1291+
func (c *Client) GetFreeSpaceOnDisk() (uint64, error) {
1292+
return c.GetFreeSpaceOnDiskCtx(context.Background())
1293+
}
1294+
1295+
// GetFreeSpaceOnDiskCtx get free space on disk for default download dir. Expensive call
1296+
func (c *Client) GetFreeSpaceOnDiskCtx(ctx context.Context) (uint64, error) {
1297+
info, err := c.SyncMainDataCtx(ctx, 0)
1298+
if err != nil {
1299+
return 0, errors.Wrap(err, "could not get maindata")
1300+
}
1301+
1302+
return info.ServerState.FreeSpaceOnDisk, nil
1303+
}
1304+
12701305
const (
12711306
ReannounceMaxAttempts = 50
12721307
ReannounceInterval = 7 // interval in seconds

0 commit comments

Comments
 (0)