Skip to content

Commit bd2bb90

Browse files
committed
fix: json parsing with torrents list and set defaults for getting torrents and downloads list
1 parent e97db04 commit bd2bb90

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

examples/torrent/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,27 @@ func getTorrentInfo(rd *realdebrid.RealDebridClient, id string) {
9797
}
9898
}
9999

100+
func getLatest10Torrents(rd *realdebrid.RealDebridClient) {
101+
// === GET LATEST 10 TORRENTS
102+
res, err := rd.GetTorrents(&realdebrid.GetTorrentsRequest{
103+
Limit: 10,
104+
})
105+
if err != nil {
106+
log.Println(err)
107+
return
108+
}
109+
110+
for _, torrent := range res {
111+
fmt.Printf("Torrent ID: %s, Name: %s, Status: %s, Progress: %.2f%%\n", torrent.ID, torrent.Filename, torrent.Status, torrent.Progress)
112+
}
113+
}
114+
100115
// UNCOMMENT SOME OF THE FUNCTIONS BELOW TO TEST THEM
101116
func main() {
102117
rd := realdebrid.NewClient(os.Getenv("REALDEBRID_API_KEY"))
103118

119+
getLatest10Torrents(rd)
120+
104121
getTorrentInfo(rd, "W6Z5SYI5JG3ZA")
105122

106123
getAvailableHosts(rd)

realdebrid/api_downloads.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@ type GetDownloadRequest struct {
2121
}
2222

2323
// GetDownloads gets the user's downloads list.
24+
// Defaults: offset=0, limit=10, page=1
2425
// `GET /downloads`
2526
func (c *RealDebridClient) GetDownloads(req *GetDownloadRequest) ([]Download, error) {
27+
params := map[string]string{
28+
"offset": "0",
29+
"limit": "10",
30+
"page": "1",
31+
}
2632

27-
params := make(map[string]string)
28-
if req.Offset != 0 {
33+
if req != nil && req.Offset != 0 {
2934
params["offset"] = fmt.Sprint(req.Offset)
3035
}
31-
if req.Limit != 0 {
36+
if req != nil && req.Limit != 0 {
3237
params["limit"] = fmt.Sprint(req.Limit)
3338
}
34-
if req.Page != 0 {
39+
if req != nil && req.Page != 0 {
3540
params["page"] = fmt.Sprint(req.Page)
3641
}
3742

realdebrid/api_torrents.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Torrent struct {
1515
Bytes int64 `json:"bytes"`
1616
Host string `json:"host"`
1717
Split int64 `json:"split"`
18-
Progress int64 `json:"progress"`
18+
Progress float64 `json:"progress"`
1919
Status string `json:"status"`
2020
Added string `json:"added"`
2121
Links []string `json:"links"`
@@ -44,21 +44,26 @@ type GetTorrentsRequest struct {
4444
}
4545

4646
// GetTorrents retrieves the list of torrents of the user.
47+
// Defaults: filter=active, offset=0, limit=10, page=1
4748
// `GET /torrents`
4849
func (c *RealDebridClient) GetTorrents(req *GetTorrentsRequest) ([]Torrent, error) {
4950
params := map[string]string{
5051
"filter": "active",
52+
"offset": "0",
53+
"limit": "10",
54+
"page": "1",
5155
}
52-
if req.Offset != 0 {
56+
57+
if req != nil && req.Offset != 0 {
5358
params["offset"] = fmt.Sprint(req.Offset)
5459
}
55-
if req.Limit != 0 {
60+
if req != nil && req.Limit != 0 {
5661
params["limit"] = fmt.Sprint(req.Limit)
5762
}
58-
if req.Page != 0 {
63+
if req != nil && req.Page != 0 {
5964
params["page"] = fmt.Sprint(req.Page)
6065
}
61-
if req.Filter != "" {
66+
if req != nil && req.Filter != "" {
6267
params["filter"] = req.Filter
6368
}
6469

0 commit comments

Comments
 (0)