-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathbandwidth_group.go
66 lines (59 loc) · 2.09 KB
/
bandwidth_group.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
package transmissionrpc
import (
"context"
"errors"
"fmt"
"strings"
)
/*
Bandwidth Group Accessors
https://github.com/transmission/transmission/blob/4.0.3/docs/rpc-spec.md#48-bandwidth-groups
*/
// BandwidthGroup represents all possible fields of data for a bandwidth group.
type BandwidthGroup struct {
HonorSessionLimits bool `json:"honorSessionLimits"`
Name string `json:"name"`
SpeedLimitDownEnabled bool `json:"speed-limit-down-enabled"`
SpeedLimitDown int64 `json:"speed-limit-down"`
SpeedLimitUpEnabled bool `json:"speed-limit-up-enabled"`
SpeedLimitUp int64 `json:"speed-limit-up"`
}
// BandwidthGroupGet returns the given fields for each bandwidth group provided. If no group names are provided, returns all groups.
// As of Transmission 4.0.3 it seems that filtering with group names does not work as documented here:
// https://github.com/transmission/transmission/blob/4.0.3/docs/rpc-spec.md#482-bandwidth-group-accessor-group-get
// Testing always yield the full list of bandwidth groups, even when submitting a filter.
func (c *Client) BandwidthGroupGet(ctx context.Context, groups []string) (bandwidthGroups []BandwidthGroup, err error) {
var (
filter string
answer bandwidthGroupGetAnswer
)
if len(groups) > 0 {
filter = strings.Join(groups, ",")
}
if err = c.rpcCall(ctx, "group-get", &bandwidthGroupGetParams{
Group: filter,
}, &answer); err != nil {
err = fmt.Errorf("'group-get' rpc method failed: %w", err)
return
}
bandwidthGroups = answer.Group
return
}
type bandwidthGroupGetParams struct {
Group string `json:"group,omitempty"`
}
type bandwidthGroupGetAnswer struct {
Group []BandwidthGroup `json:"group"`
}
// BandwidthGroupSet applies a list of mutator(s) to a bandwidth group.
func (c *Client) BandwidthGroupSet(ctx context.Context, bwGroup BandwidthGroup) (err error) {
// Validate
if bwGroup.Name == "" {
return errors.New("Bandwidth group must have a name")
}
// Send payload
if err = c.rpcCall(ctx, "group-set", bwGroup, nil); err != nil {
err = fmt.Errorf("'group-set' rpc method failed: %w", err)
}
return
}