-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
219 lines (192 loc) · 4.95 KB
/
client.go
File metadata and controls
219 lines (192 loc) · 4.95 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package go_itunes_api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
const (
APIPath = "https://itunes.apple.com"
SearchPath = "/search"
US = "US"
)
const DefaultHTTPTimeout = 60 * time.Second
type SearchRequest struct {
// The text string you want to search for. For example: jack+johnson.
//
// Required.
Term string
// The two-letter country code for the store you want to search. The search uses
// the default store front for the specified country. For example: US. The
// default is US. See http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 for a list
// of ISO Country Codes.
//
// Required.
Country string
// The media type you want to search for. For example: movie. The default is all.
//
// Optional.
Media MediaType
// The type of results you want returned, relative to the specified media type.
// For example: movieArtist for a movie media type search. The default is the
// track entity associated with the specified media type.
//
// Optional.
Entity Entity
// The attribute you want to search for in the stores, relative to the specified
// media type. For example, if you want to search for an artist by name specify
// entity=allArtist&attribute=allArtistTerm. In this example, if you search for
// term=maroon, iTunes returns “Maroon 5” in the search results, instead of all
// artists who have ever recorded a song with the word “maroon” in the title. The
// default is all attributes associated with the specified media type.
//
// Optional.
Attribute Attribute
// The name of the Javascript callback function you want to use when returning
// search results to your website.
//
// Required for cross-site searches.
Callback string
// The number of search results you want the iTunes Store to return. For example:
// 25. The default is 50.
//
// Optional.
Limit int
// The language, English or Japanese, you want to use when returning search
// results. Specify the language using the five-letter codename. For example:
// en_us. The default is en_us (English).
//
// Optional.
Lang string
// The search result key version you want to receive back from your search. The
// default is 2.
//
// Optional.
Version string
// A flag indicating whether or not you want to include explicit content in your
// search results. The default is Yes.
//
// Optional.
Explicit bool
}
func (r *SearchRequest) Validate() error {
if r.Term == "" {
return fmt.Errorf("term is required")
}
if r.Country == "" {
return fmt.Errorf("country is required")
}
return nil
}
func (r SearchRequest) GetURLValues() url.Values {
v := url.Values{}
v.Set("term", r.Term)
v.Set("country", r.Country)
if r.Media != "" {
v.Set("media", string(r.Media))
}
if r.Entity != "" {
v.Set("entity", string(r.Entity))
}
if r.Attribute != "" {
v.Set("attribute", string(r.Attribute))
}
if r.Callback != "" {
v.Set("callback", r.Callback)
}
if r.Limit != 0 {
v.Set("limit", strconv.Itoa(r.Limit))
}
if r.Lang != "" {
v.Set("lang", r.Lang)
}
if r.Version != "" {
v.Set("version", r.Version)
}
if r.Explicit {
v.Set("explicit", "Yes")
} else {
v.Set("explicit", "No")
}
return v
}
type SearchResponse struct {
Results Results
}
type LookupRequest struct{}
type LookupResponse struct {
Results Results
}
type API interface {
Search(ctx context.Context, r SearchRequest) (SearchResponse, error)
Lookup(ctx context.Context, r LookupRequest) (LookupResponse, error)
}
type ClientOption struct {
Host string
Timeout time.Duration
}
type Client struct {
client *http.Client
host string
}
func NewClient(opt ClientOption) (*Client, error) {
if opt.Host == "" {
opt.Host = APIPath
}
if opt.Timeout == 0 {
opt.Timeout = DefaultHTTPTimeout
}
return &Client{
client: &http.Client{
Transport: new(http.Transport),
Timeout: opt.Timeout,
},
host: opt.Host,
}, nil
}
func (c *Client) Search(ctx context.Context, r SearchRequest) (SearchResponse, error) {
err := r.Validate()
if err != nil {
return SearchResponse{}, err
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprint(c.host, SearchPath, "?", r.GetURLValues().Encode()),
nil,
)
if err != nil {
return SearchResponse{}, err
}
return c.do(req)
}
func (c *Client) Lookup(ctx context.Context, r LookupRequest) (LookupResponse, error) {
//TODO implement me
panic("implement me")
}
func (c *Client) do(req *http.Request) (SearchResponse, error) {
resp, err := c.client.Do(req)
if err != nil {
return SearchResponse{}, err
}
defer func() { _ = resp.Body.Close() }()
err = checkResponse(resp)
if err != nil {
return SearchResponse{}, err
}
var res Results
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return SearchResponse{}, err
}
return SearchResponse{Results: res}, nil
}
func checkResponse(resp *http.Response) error {
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return nil
}
return fmt.Errorf("unexpected response: %s", resp.Status)
}