|
3 | 3 | package listennotes
|
4 | 4 |
|
5 | 5 | import (
|
6 |
| - "encoding/json" |
7 |
| - "fmt" |
8 | 6 | "net/http"
|
9 | 7 | )
|
10 | 8 |
|
11 |
| -// Base urls for access the available api endpoints |
12 |
| -const ( |
13 |
| - BaseURLProduction = "https://listen-api.listennotes.com/api/v2" |
14 |
| - BaseURLTest = "https://listen-api-test.listennotes.com/api/v2" |
15 |
| -) |
16 |
| - |
17 |
| -// Request header keys |
18 |
| -const ( |
19 |
| - RequestHeaderKeyAPI = "X-ListenAPI-Key" |
20 |
| -) |
21 |
| - |
22 |
| -// Reponse header keys |
23 |
| -const ( |
24 |
| - ResponseHeaderKeyFreeQuota = "X-ListenAPI-FreeQuota" |
25 |
| - ResponseHeaderKeyUsage = "X-ListenAPI-Usage" |
26 |
| - ResponseHeaderKeyLatencySeconds = "X-listenAPI-Latency-Seconds" |
27 |
| - ResponseHeaderKeyNextBillingDate = "X-Listenapi-NextBillingDate" |
28 |
| -) |
29 |
| - |
30 |
| -// TimeFormat is the string format of all response times |
31 |
| -const TimeFormat = "2006-01-02T15:04:05.000000+07:00" |
32 |
| - |
33 |
| -var defaultHTTPClient *http.Client = &http.Client{} |
34 |
| - |
35 |
| -// Response is the standard response for all client functions |
36 |
| -type Response struct { |
37 |
| - Stats ResponseStatistics |
38 |
| - Data map[string]interface{} |
39 |
| -} |
40 |
| - |
41 | 9 | // HTTPClient is the client interface
|
42 | 10 | type HTTPClient interface {
|
43 | 11 | Search(args map[string]string) (*Response, error)
|
@@ -74,44 +42,5 @@ func NewClient(apiKey string, opts ...ClientOption) HTTPClient {
|
74 | 42 | }
|
75 | 43 |
|
76 | 44 | func (c *standardHTTPClient) Search(args map[string]string) (*Response, error) {
|
77 |
| - |
78 |
| - // TODO: move all this common stuff to a function |
79 |
| - url := fmt.Sprintf("%s/search", c.baseURL) |
80 |
| - |
81 |
| - req, err := http.NewRequest("GET", url, nil) |
82 |
| - if err != nil { |
83 |
| - return nil, fmt.Errorf("failed to create request: %w", err) |
84 |
| - } |
85 |
| - req.Header.Add(RequestHeaderKeyAPI, c.apiKey) |
86 |
| - |
87 |
| - q := req.URL.Query() |
88 |
| - for k, v := range args { |
89 |
| - q.Add(k, v) |
90 |
| - } |
91 |
| - req.URL.RawQuery = q.Encode() |
92 |
| - |
93 |
| - resp, err := c.httpClient.Do(req) |
94 |
| - if err != nil { |
95 |
| - return nil, fmt.Errorf("failed to executing request: %w", err) |
96 |
| - } |
97 |
| - defer resp.Body.Close() |
98 |
| - |
99 |
| - // map any generic status code errors |
100 |
| - if mappedError, ok := errMap[resp.StatusCode]; ok && mappedError != nil { |
101 |
| - return nil, mappedError |
102 |
| - } |
103 |
| - |
104 |
| - // generic body parsing |
105 |
| - var genericJSON map[string]interface{} |
106 |
| - if err := json.NewDecoder(resp.Body).Decode(&genericJSON); err != nil { |
107 |
| - return nil, fmt.Errorf("failed parsing the response: %w", err) |
108 |
| - } |
109 |
| - |
110 |
| - // gather the header statistics |
111 |
| - stats := parseStats(resp) |
112 |
| - |
113 |
| - return &Response{ |
114 |
| - Stats: stats, |
115 |
| - Data: genericJSON, |
116 |
| - }, nil |
| 45 | + return c.execute(args, "search") |
117 | 46 | }
|
0 commit comments