From 9c6e2ddb4afbfdc9a7bbfa9c05b753c441c7fca3 Mon Sep 17 00:00:00 2001 From: martylukyy <35452459+martylukyy@users.noreply.github.com> Date: Thu, 16 May 2024 19:17:31 +0200 Subject: [PATCH 1/5] feat: GetFreeSpaceonDisk --- domain.go | 8 ++++++++ methods.go | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/domain.go b/domain.go index 7b83a5f..baec663 100644 --- a/domain.go +++ b/domain.go @@ -565,3 +565,11 @@ type AppPreferences struct { WebUIUseCustomHTTPHeadersEnabled bool `json:"web_ui_use_custom_http_headers_enabled"` WebUIUsername string `json:"web_ui_username"` } + +type MainData struct { + ServerState ServerState `json:"server_state"` +} + +type ServerState struct { + FreeSpaceOnDisk uint64 `json:"free_space_on_disk"` +} diff --git a/methods.go b/methods.go index 028cff9..d667e41 100644 --- a/methods.go +++ b/methods.go @@ -1290,3 +1290,26 @@ func isUnregistered(msg string) bool { return false } + +func (c *Client) GetFreeSpaceonDisk() (uint64, error) { + return c.GetFreeSpaceonDiskCtx(context.Background()) +} + +func (c *Client) GetFreeSpaceonDiskCtx(ctx context.Context) (uint64, error) { + resp, err := c.getCtx(ctx, "/sync/maindata", nil) + if err != nil { + return 0, errors.Wrap(err, "could not get maindata") + } + + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + + var info MainData + if err := json.Unmarshal(body, &info); err != nil { + return 0, errors.Wrap(err, "could not unmarshal body") + } + + return info.ServerState.FreeSpaceOnDisk, nil + +} From 329d459265264e3b117671626746f124b060b82e Mon Sep 17 00:00:00 2001 From: martylukyy <35452459+martylukyy@users.noreply.github.com> Date: Thu, 16 May 2024 21:43:44 +0200 Subject: [PATCH 2/5] optimize memory usage --- methods.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/methods.go b/methods.go index d667e41..2b7736c 100644 --- a/methods.go +++ b/methods.go @@ -1303,10 +1303,8 @@ func (c *Client) GetFreeSpaceonDiskCtx(ctx context.Context) (uint64, error) { defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - var info MainData - if err := json.Unmarshal(body, &info); err != nil { + if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { return 0, errors.Wrap(err, "could not unmarshal body") } From 1eb1d2de26bafe01549119cb38ae03545cf0e16d Mon Sep 17 00:00:00 2001 From: martylukyy <35452459+martylukyy@users.noreply.github.com> Date: Thu, 16 May 2024 21:45:06 +0200 Subject: [PATCH 3/5] optimize code further --- methods.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/methods.go b/methods.go index 2b7736c..7d6a6f4 100644 --- a/methods.go +++ b/methods.go @@ -1301,8 +1301,6 @@ func (c *Client) GetFreeSpaceonDiskCtx(ctx context.Context) (uint64, error) { return 0, errors.Wrap(err, "could not get maindata") } - defer resp.Body.Close() - var info MainData if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { return 0, errors.Wrap(err, "could not unmarshal body") From c334215f90304712658a4c99d7afb3f92e1d7f41 Mon Sep 17 00:00:00 2001 From: martylukyy <35452459+martylukyy@users.noreply.github.com> Date: Thu, 16 May 2024 22:35:50 +0200 Subject: [PATCH 4/5] feat: add full maindata method incl. ServerState object --- domain.go | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/domain.go b/domain.go index baec663..eadf1a5 100644 --- a/domain.go +++ b/domain.go @@ -567,9 +567,38 @@ type AppPreferences struct { } type MainData struct { - ServerState ServerState `json:"server_state"` + Rid int `json:"rid"` + FullUpdate bool `json:"full_update"` + Torrents struct{} `json:"torrents"` + Categories struct{} `json:"categories"` + Tags []interface{} `json:"tags"` + Trackers struct{} `json:"trackers"` + ServerState ServerState `json:"server_state"` } type ServerState struct { - FreeSpaceOnDisk uint64 `json:"free_space_on_disk"` + AlltimeDl int64 `json:"alltime_dl"` + AlltimeUl int64 `json:"alltime_ul"` + AverageTimeQueue int `json:"average_time_queue"` + ConnectionStatus string `json:"connection_status"` + DhtNodes int `json:"dht_nodes"` + DlInfoData int64 `json:"dl_info_data"` + DlInfoSpeed int `json:"dl_info_speed"` + DlRateLimit int `json:"dl_rate_limit"` + FreeSpaceOnDisk uint64 `json:"free_space_on_disk"` + GlobalRatio string `json:"global_ratio"` + QueuedIoJobs int `json:"queued_io_jobs"` + Queueing bool `json:"queueing"` + ReadCacheHits string `json:"read_cache_hits"` + ReadCacheOverload string `json:"read_cache_overload"` + RefreshInterval int `json:"refresh_interval"` + TotalBuffersSize int `json:"total_buffers_size"` + TotalPeerConnections int `json:"total_peer_connections"` + TotalQueuedSize int `json:"total_queued_size"` + TotalWastedSession int64 `json:"total_wasted_session"` + UpInfoData int64 `json:"up_info_data"` + UpInfoSpeed int `json:"up_info_speed"` + UpRateLimit int `json:"up_rate_limit"` + UseAltSpeedLimits bool `json:"use_alt_speed_limits"` + WriteCacheOverload string `json:"write_cache_overload"` } From f5d6b94a97de82a457ff6d2a151a052bb39a5aa7 Mon Sep 17 00:00:00 2001 From: ze0s Date: Tue, 5 Nov 2024 21:02:58 +0100 Subject: [PATCH 5/5] feat(methods): add SyncMainData --- domain.go | 17 ++++++++++------- methods.go | 54 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/domain.go b/domain.go index 3c9289e..a70866c 100644 --- a/domain.go +++ b/domain.go @@ -588,13 +588,16 @@ type AppPreferences struct { } type MainData struct { - Rid int `json:"rid"` - FullUpdate bool `json:"full_update"` - Torrents struct{} `json:"torrents"` - Categories struct{} `json:"categories"` - Tags []interface{} `json:"tags"` - Trackers struct{} `json:"trackers"` - ServerState ServerState `json:"server_state"` + Rid int `json:"rid"` + FullUpdate bool `json:"full_update"` + Torrents map[string]Torrent `json:"torrents"` + TorrentsRemoved []string `json:"torrents_removed"` + Categories map[string]Category `json:"categories"` + CategoriesRemoved []string `json:"categories_removed"` + Tags []string `json:"tags"` + TagsRemoved []string `json:"tags_removed"` + Trackers map[string][]string `json:"trackers"` + ServerState ServerState `json:"server_state"` } type ServerState struct { diff --git a/methods.go b/methods.go index b2bd5e1..c3ce628 100644 --- a/methods.go +++ b/methods.go @@ -468,6 +468,27 @@ func (c *Client) GetTransferInfoCtx(ctx context.Context) (*TransferInfo, error) return &info, nil } +// SyncMainDataCtx Sync API implements requests for obtaining changes since the last request. +// 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) +func (c *Client) SyncMainDataCtx(ctx context.Context, rid int64) (*MainData, error) { + opts := map[string]string{ + "rid": strconv.FormatInt(rid, 10), + } + + resp, err := c.getCtx(ctx, "/sync/maindata", opts) + if err != nil { + return nil, errors.Wrap(err, "could not get main data") + } + + var info MainData + if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { + return nil, errors.Wrap(err, "could not unmarshal body") + } + + return &info, nil + +} + func (c *Client) Pause(hashes []string) error { return c.PauseCtx(context.Background(), hashes) } @@ -1267,6 +1288,20 @@ func (c *Client) GetWebAPIVersionCtx(ctx context.Context) (string, error) { return string(body), nil } +func (c *Client) GetFreeSpaceOnDisk() (uint64, error) { + return c.GetFreeSpaceOnDiskCtx(context.Background()) +} + +// GetFreeSpaceOnDiskCtx get free space on disk for default download dir. Expensive call +func (c *Client) GetFreeSpaceOnDiskCtx(ctx context.Context) (uint64, error) { + info, err := c.SyncMainDataCtx(ctx, 0) + if err != nil { + return 0, errors.Wrap(err, "could not get maindata") + } + + return info.ServerState.FreeSpaceOnDisk, nil +} + const ( ReannounceMaxAttempts = 50 ReannounceInterval = 7 // interval in seconds @@ -1388,22 +1423,3 @@ func isUnregistered(msg string) bool { return false } - -func (c *Client) GetFreeSpaceonDisk() (uint64, error) { - return c.GetFreeSpaceonDiskCtx(context.Background()) -} - -func (c *Client) GetFreeSpaceonDiskCtx(ctx context.Context) (uint64, error) { - resp, err := c.getCtx(ctx, "/sync/maindata", nil) - if err != nil { - return 0, errors.Wrap(err, "could not get maindata") - } - - var info MainData - if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { - return 0, errors.Wrap(err, "could not unmarshal body") - } - - return info.ServerState.FreeSpaceOnDisk, nil - -}