|
1 | 1 | package chromaprint
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "encoding/json" |
5 |
| - "errors" |
6 | 4 | "fmt"
|
7 |
| - "io/ioutil" |
8 |
| - "mime" |
9 |
| - "net/http" |
10 | 5 | "net/url"
|
11 |
| - "os" |
12 |
| - "path/filepath" |
13 | 6 | "time"
|
14 | 7 |
|
| 8 | + // Packages |
| 9 | + "github.com/mutablelogic/go-client" |
| 10 | + |
15 | 11 | // Namespace imports
|
16 | 12 | . "github.com/djthorpe/go-errors"
|
17 | 13 | )
|
18 | 14 |
|
19 |
| -//////////////////////////////////////////////////////////////////////////////// |
| 15 | +/////////////////////////////////////////////////////////////////////////////// |
20 | 16 | // TYPES
|
21 | 17 |
|
22 |
| -type Config struct { |
23 |
| - Key string `yaml:"key"` // AcuostId Web Service Key |
24 |
| - Timeout time.Duration `yaml:"timeout"` // AcoustId Client timeout |
25 |
| - Rate uint `yaml:"rate"` // Maximum requests per second |
26 |
| - Base string `yaml:"base"` // Base URL |
27 |
| -} |
28 |
| - |
29 | 18 | type Client struct {
|
30 |
| - Config |
31 |
| - *http.Client |
32 |
| - *url.URL |
33 |
| - last time.Time |
| 19 | + *client.Client |
| 20 | + key string |
34 | 21 | }
|
35 | 22 |
|
36 | 23 | ////////////////////////////////////////////////////////////////////////////////
|
37 | 24 | // GLOBALS
|
38 | 25 |
|
39 | 26 | const (
|
40 | 27 | // Register a clientId: https://acoustid.org/login
|
41 |
| - defaultClientId = "-YectPtndAM" |
42 |
| - |
43 |
| - // Timeout requests after 15 seconds |
44 |
| - defaultTimeout = 15 * time.Second |
| 28 | + defaultApiKey = "-YectPtndAM" |
45 | 29 |
|
46 | 30 | // The API endpoint
|
47 |
| - baseUrl = "https://api.acoustid.org/v2" |
| 31 | + endPoint = "https://api.acoustid.org/v2" |
48 | 32 |
|
49 | 33 | // defaultQps rate limits number of requests per second
|
50 | 34 | defaultQps = 3
|
51 | 35 | )
|
52 | 36 |
|
53 |
| -var ( |
54 |
| - ErrQueryRateExceeded = errors.New("query rate exceeded") |
55 |
| -) |
| 37 | +/////////////////////////////////////////////////////////////////////////////// |
| 38 | +// LIFECYCLE |
56 | 39 |
|
57 |
| -var ( |
58 |
| - DefaultConfig = Config{ |
59 |
| - Key: defaultClientId, |
60 |
| - Timeout: defaultTimeout, |
61 |
| - Rate: defaultQps, |
62 |
| - Base: baseUrl, |
| 40 | +// Create a new client |
| 41 | +func NewClient(ApiKey string, opts ...client.ClientOpt) (*Client, error) { |
| 42 | + // Check for missing API key |
| 43 | + if ApiKey == "" { |
| 44 | + ApiKey = defaultApiKey |
63 | 45 | }
|
64 |
| -) |
65 |
| - |
66 |
| -//////////////////////////////////////////////////////////////////////////////// |
67 |
| -// NEW |
68 |
| - |
69 |
| -func NewClient(key string) *Client { |
70 |
| - config := DefaultConfig |
71 |
| - if key != "" { |
72 |
| - config.Key = os.ExpandEnv(key) |
73 |
| - } |
74 |
| - if client, err := NewClientWithConfig(config); err != nil { |
75 |
| - return nil |
76 |
| - } else { |
77 |
| - return client |
78 |
| - } |
79 |
| -} |
80 |
| - |
81 |
| -func NewClientWithConfig(cfg Config) (*Client, error) { |
82 |
| - client := new(Client) |
83 |
| - client.Config = cfg |
84 | 46 |
|
85 |
| - // Set parameters |
86 |
| - if client.Config.Timeout == 0 { |
87 |
| - client.Config.Timeout = DefaultConfig.Timeout |
88 |
| - } |
89 |
| - if client.Key == "" { |
90 |
| - client.Key = os.ExpandEnv(DefaultConfig.Key) |
91 |
| - } |
92 |
| - if client.Base == "" { |
93 |
| - client.Base = DefaultConfig.Base |
94 |
| - } |
95 |
| - if client.Rate == 0 { |
96 |
| - client.Rate = DefaultConfig.Rate |
97 |
| - } |
98 |
| - |
99 |
| - // Create HTTP client |
100 |
| - client.Client = &http.Client{ |
101 |
| - Timeout: client.Config.Timeout, |
102 |
| - } |
103 |
| - url, err := url.Parse(client.Base) |
| 47 | + // Create client |
| 48 | + opts = append(opts, client.OptEndpoint(endPoint)) |
| 49 | + client, err := client.New(opts...) |
104 | 50 | if err != nil {
|
105 | 51 | return nil, err
|
106 |
| - } else { |
107 |
| - client.URL = url |
108 | 52 | }
|
109 |
| - // Fudge key into URL |
110 |
| - v := url.Query() |
111 |
| - v.Set("client", client.Key) |
112 |
| - url.RawQuery = v.Encode() |
113 | 53 |
|
114 |
| - // Return success |
115 |
| - return client, nil |
116 |
| -} |
117 |
| - |
118 |
| -//////////////////////////////////////////////////////////////////////////////// |
119 |
| -// STRINGIFY |
120 |
| - |
121 |
| -func (client *Client) String() string { |
122 |
| - str := "<chromaprint" |
123 |
| - if u := client.URL; u != nil { |
124 |
| - str += fmt.Sprintf(" url=%q", u.String()) |
125 |
| - } |
126 |
| - if rate := client.Rate; rate > 0 { |
127 |
| - str += fmt.Sprintf(" rate=%dops/s", rate) |
128 |
| - } |
129 |
| - if timeout := client.Client.Timeout; timeout > 0 { |
130 |
| - str += fmt.Sprintf(" timeout=%v", timeout) |
131 |
| - } |
132 |
| - return str + ">" |
| 54 | + // Return the client |
| 55 | + return &Client{ |
| 56 | + Client: client, |
| 57 | + key: ApiKey, |
| 58 | + }, nil |
133 | 59 | }
|
134 | 60 |
|
135 | 61 | ////////////////////////////////////////////////////////////////////////////////
|
136 | 62 | // LOOKUP
|
137 | 63 |
|
138 |
| -func (client *Client) Lookup(fingerprint string, duration time.Duration, flags Meta) ([]*ResponseMatch, error) { |
| 64 | +// Lookup a fingerprint with a duration and the metadata that needs to be |
| 65 | +// returned |
| 66 | +func (c *Client) Lookup(fingerprint string, duration time.Duration, flags Meta) ([]*ResponseMatch, error) { |
139 | 67 | // Check incoming parameters
|
140 | 68 | if fingerprint == "" || duration == 0 || flags == META_NONE {
|
141 | 69 | return nil, ErrBadParameter.With("Lookup")
|
142 | 70 | }
|
143 | 71 |
|
144 |
| - // Check Qps |
145 |
| - if client.last.IsZero() { |
146 |
| - if time.Since(client.last) < (time.Second / defaultQps) { |
147 |
| - return nil, ErrQueryRateExceeded |
148 |
| - } |
149 |
| - } |
150 |
| - |
151 | 72 | // Set URL parameters
|
152 | 73 | params := url.Values{}
|
| 74 | + params.Set("client", c.key) |
153 | 75 | params.Set("fingerprint", fingerprint)
|
154 | 76 | params.Set("duration", fmt.Sprint(duration.Truncate(time.Second).Seconds()))
|
155 | 77 | params.Set("meta", flags.String())
|
156 |
| - url := client.requestUrl("lookup", params) |
157 |
| - if url == nil { |
158 |
| - return nil, ErrBadParameter.With("Lookup") |
159 |
| - } |
160 | 78 |
|
161 |
| - //fmt.Println(url.String()) |
162 |
| - |
163 |
| - // Perform request |
164 |
| - now := time.Now() |
165 |
| - req, err := http.NewRequest(http.MethodGet, url.String(), nil) |
166 |
| - if err != nil { |
| 79 | + // Request -> Response |
| 80 | + var response Response |
| 81 | + if err := c.Do(nil, &response, client.OptPath("lookup"), client.OptQuery(params)); err != nil { |
167 | 82 | return nil, err
|
| 83 | + } else { |
| 84 | + return response.Results, nil |
168 | 85 | }
|
169 |
| - response, err := client.Client.Do(req) |
170 |
| - if err != nil { |
171 |
| - return nil, err |
172 |
| - } |
173 |
| - defer response.Body.Close() |
174 |
| - |
175 |
| - // Read response body |
176 |
| - body, err := ioutil.ReadAll(response.Body) |
177 |
| - if err != nil { |
178 |
| - return nil, err |
179 |
| - } |
180 |
| - |
181 |
| - // Decode response body |
182 |
| - var r Response |
183 |
| - if mimeType, _, err := mime.ParseMediaType(response.Header.Get("Content-type")); err != nil { |
184 |
| - return nil, ErrUnexpectedResponse.With(err) |
185 |
| - } else if mimeType != "application/json" { |
186 |
| - return nil, ErrUnexpectedResponse.With(mimeType) |
187 |
| - } else if err := json.Unmarshal(body, &r); err != nil { |
188 |
| - return nil, ErrUnexpectedResponse.With(err) |
189 |
| - } |
190 |
| - |
191 |
| - // Check for errors |
192 |
| - if r.Status != "ok" { |
193 |
| - return nil, ErrBadParameter.Withf("%v (code %v)", r.Error.Message, r.Error.Code) |
194 |
| - } else if response.StatusCode != http.StatusOK { |
195 |
| - return nil, ErrBadParameter.Withf("%v (code %v)", response.Status, response.StatusCode) |
196 |
| - } |
197 |
| - |
198 |
| - // Set response time for calculating qps |
199 |
| - client.last = now |
200 |
| - |
201 |
| - // Return success |
202 |
| - return r.Results, nil |
203 |
| -} |
204 |
| - |
205 |
| -//////////////////////////////////////////////////////////////////////////////// |
206 |
| -// PRIVATE METHODS |
207 |
| - |
208 |
| -func (client *Client) requestUrl(path string, v url.Values) *url.URL { |
209 |
| - url, err := url.Parse(client.URL.String()) |
210 |
| - if err != nil { |
211 |
| - return nil |
212 |
| - } |
213 |
| - // Copy params |
214 |
| - params := client.URL.Query() |
215 |
| - for k := range v { |
216 |
| - params[k] = v[k] |
217 |
| - } |
218 |
| - url.RawQuery = params.Encode() |
219 |
| - |
220 |
| - // Set path |
221 |
| - url.Path = filepath.Join(url.Path, path) |
222 |
| - |
223 |
| - // Return URL |
224 |
| - return url |
225 | 86 | }
|
0 commit comments