-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
129 lines (111 loc) · 2.86 KB
/
client.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
type Config struct {
Debug bool `json:"debug"`
AppID int `json:"appID"`
AppSecret string `json:"appSecret"`
BaseAPI string `json:"baseAPI"`
CallbackURL string `json:"callbackURL"`
RedisDB struct {
Addr string `json:"address"`
Password string `json:"password"`
Db int `json:"db"`
} `json:"redisDB"`
DNS struct {
Port int `json:"port"`
Protocol string `json:"proto"`
Domain string `json:"domain"`
} `json:"dns"`
}
type DeezerClient struct {
config Config
HTTPClient *http.Client
RedisClient *redis.Client
}
func readConf(cPath string) Config {
confFile, err := ioutil.ReadFile(cPath)
checkErr(err)
conf := Config{}
err = json.Unmarshal([]byte(confFile), &conf)
log.Infof("[APP] Loaded config at %s", cPath)
return conf
}
func newDeezerClient(cPath string) *DeezerClient {
c := readConf(cPath)
return &DeezerClient{
config: c,
HTTPClient: &http.Client{
Timeout: time.Minute,
},
RedisClient: redis.NewClient(&redis.Options{
Addr: c.RedisDB.Addr,
Password: c.RedisDB.Password, // no password set
DB: c.RedisDB.Db, // use default DB
}),
}
}
func (c *DeezerClient) getQuery(q string, t string) []byte {
url := fmt.Sprintf("%s/%s?access_token=%s", c.config.BaseAPI, q, t)
req, err := http.NewRequest(http.MethodGet, url, nil)
checkErr(err)
log.WithFields(log.Fields{
"url": strings.Replace(url, t, "", 1),
}).Debug("[API] getQuery")
req.Header.Set("User-Agent", "1035-deezer-cli")
res, err := c.HTTPClient.Do(req)
checkErr(err)
if res.Body != nil {
defer res.Body.Close()
}
body, err := ioutil.ReadAll(res.Body)
checkErr(err)
return body
}
func (c *DeezerClient) getClient(token string) deezerAccount {
q := c.getQuery("/user/me/", token)
u := deezerAccount{}
err := json.Unmarshal(q, &u)
checkErr(err)
log.WithFields(log.Fields{
"uid": u.ID,
}).Debug("[API] getClient")
return u
}
func (c *DeezerClient) getHistory(uid string) deezerHistory {
log.WithFields(log.Fields{
"uid": uid,
}).Debug("[API] getHistory")
t, err := c.RedisClient.Get(ctx, uid).Result()
if err == redis.Nil {
log.WithFields(log.Fields{
"uid": uid,
}).Debug("[REDIS] getHistory - Empty REDIS")
return deezerHistory{}
} else {
q := c.getQuery("/user/me/history", t)
hist := deezerHistory{}
err := json.Unmarshal(q, &hist)
checkErr(err)
return hist
}
}
func (c *DeezerClient) lastTitle(uid string) deezerTitle {
hist := c.getHistory(uid).Data
if len(hist) < 1 {
return deezerTitle{}
} else {
lastMusic := hist[0]
return lastMusic
}
}