-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
138 lines (125 loc) · 2.98 KB
/
api.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
130
131
132
133
134
135
136
137
138
package api
import (
"crypto/md5"
"crypto/tls"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/go-resty/resty/v2"
"github.com/redis/go-redis/v9"
"gopkg.in/yaml.v3"
)
type Config struct {
Key string `json:"key" yaml:"key"`
SecretKey string `json:"secretKey" yaml:"secretKey"`
Location string `json:"location" yaml:"location"` // default: time.Local
Debug bool `json:"debug" yaml:"debug"`
Timeout time.Duration `json:"timeout" yaml:"timeout"` // A Timeout of zero means no timeout
EnableCache bool `json:"enableCache" yaml:"enableCache"`
NullDataExpire time.Duration `json:"nullDataExpire" yaml:"nullDataExpire"`
CachePrefix string `json:"cachePrefix" yaml:"cachePrefix"` // default: `qcc:`
}
type Api struct {
cfg *Config
cli *resty.Client
loc *time.Location
cache *redis.Client
}
func New(cfg *Config) *Api {
var (
cli = resty.New()
a = Api{
cfg: cfg,
loc: time.Local,
}
)
cli.SetDebug(cfg.Debug)
cli.SetTimeout(cfg.Timeout)
cli.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
if cfg.Location != "" {
loc, err := time.LoadLocation(cfg.Location)
if err != nil {
panic(err)
}
a.loc = loc
}
return &a
}
func NewClient(cfg *Config, cli *resty.Client) *Api {
var a = Api{
cfg: cfg,
loc: time.Local,
cli: cli,
}
a.cli.SetDebug(cfg.Debug)
a.cli.SetTimeout(cfg.Timeout)
if cfg.Location != "" {
loc, err := time.LoadLocation(cfg.Location)
if err != nil {
panic(err)
}
a.loc = loc
}
return &a
}
func NewFromFile(filename string) *Api {
cfg, err := LoadConfig(filename)
if err != nil {
panic(fmt.Sprintf("load config from file [%s] error: %v", filename, err))
}
return New(cfg)
}
func (a *Api) GetClient() *resty.Client {
return a.cli
}
func (a *Api) auth() (string, string, error) {
return GenToken(a.cfg.Key, a.cfg.SecretKey, a.loc)
}
// LoadConfig load config from file
func LoadConfig(filename string) (*Config, error) {
if filename == "" {
return nil, errors.New("filename is empty")
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var cfg Config
switch filepath.Ext(filename) {
case ".yaml", ".yml":
err = yaml.NewDecoder(file).Decode(&cfg)
case ".json":
err = json.NewDecoder(file).Decode(&cfg)
default:
return nil, errors.New("unsupported file extension types")
}
return &cfg, nil
}
// GenToken https://openapi.qcc.com/services/after/code
// return:
// - token
// - timestamp(秒级)
// - error
func GenToken(key, secretKey string, loc ...*time.Location) (string, string, error) {
var l = time.Local
if len(loc) > 0 {
l = loc[0]
}
var (
unix = time.Now().In(l).Unix()
t = fmt.Sprintf("%s%d%s", key, unix, secretKey)
m = md5.New()
)
if _, err := io.WriteString(m, t); err != nil {
return "", "", err
}
token := strings.ToUpper(hex.EncodeToString(m.Sum(nil)))
return token, fmt.Sprintf("%d", unix), nil
}