forked from viewscreen/viewscreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfriend.go
More file actions
51 lines (42 loc) · 812 Bytes
/
friend.go
File metadata and controls
51 lines (42 loc) · 812 Bytes
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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"time"
)
type Friend struct {
ID string
Error error
}
type FriendDownload struct {
ID string
Size int64
}
type FriendFile struct {
ID string
Size int64
}
func (f *Friend) Downloads() []FriendDownload {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
endpoint := fmt.Sprintf("https://%s/watcher/v1/downloads?friend=%s", f.ID, httpHost)
res, err := GET(ctx, endpoint)
if err != nil {
f.Error = err
return nil
}
b, err := ioutil.ReadAll(io.LimitReader(res.Body, httpReadLimit))
if err != nil {
f.Error = err
return nil
}
var downloads []FriendDownload
if err := json.Unmarshal(b, &downloads); err != nil {
f.Error = err
return nil
}
return downloads
}