@@ -13,7 +13,7 @@ import (
13
13
)
14
14
15
15
const (
16
- APIVersion = "api/v0.1"
16
+ defaultAPIDir = "api/v0.1"
17
17
)
18
18
19
19
//Client is http.Client for Aozora API Server
@@ -32,7 +32,7 @@ func (c *Client) SearchBooksRaw(opts ...SearchBooksParamsFunc) ([]byte, error) {
32
32
for _ , opt := range opts {
33
33
opt (params )
34
34
}
35
- b , err := c .get (c .MakeSearchCommand (TargetBooks , params ))
35
+ b , err := c .get (c .makeSearchCommand (TargetBooks , params ))
36
36
return b , errs .Wrap (err , "" )
37
37
}
38
38
@@ -109,7 +109,7 @@ func (c *Client) SearchPersonsRaw(opts ...SearchPersonsParamsFunc) ([]byte, erro
109
109
for _ , opt := range opts {
110
110
opt (params )
111
111
}
112
- b , err := c .get (c .MakeSearchCommand (TargetPersons , params ))
112
+ b , err := c .get (c .makeSearchCommand (TargetPersons , params ))
113
113
return b , errs .Wrap (err , "" )
114
114
}
115
115
@@ -141,7 +141,7 @@ func (c *Client) SearchWorkersRaw(opts ...SearchWorkersParamsFunc) ([]byte, erro
141
141
for _ , opt := range opts {
142
142
opt (params )
143
143
}
144
- b , err := c .get (c .MakeSearchCommand (TargetWorkers , params ))
144
+ b , err := c .get (c .makeSearchCommand (TargetWorkers , params ))
145
145
return b , errs .Wrap (err , "" )
146
146
}
147
147
@@ -166,7 +166,7 @@ func WithWorkerName(name string) SearchWorkersParamsFunc {
166
166
167
167
//LookupBookRaw gets book data (raw data)
168
168
func (c * Client ) LookupBookRaw (id int ) ([]byte , error ) {
169
- b , err := c .get (c .MakeLookupCommand (TargetBooks , id ))
169
+ b , err := c .get (c .makeLookupCommand (TargetBooks , id ))
170
170
return b , errs .Wrap (err , "" )
171
171
}
172
172
@@ -182,19 +182,19 @@ func (c *Client) LookupBook(id int) (*Book, error) {
182
182
183
183
//LookupBookCardRaw gets book card info (HTML page data)
184
184
func (c * Client ) LookupBookCardRaw (id int ) ([]byte , error ) {
185
- b , err := c .get (c .MakeCardCommand (id ))
185
+ b , err := c .get (c .makeCardCommand (id ))
186
186
return b , errs .Wrap (err , "" )
187
187
}
188
188
189
189
//LookupBookContentRaw gets book content (plain or HTML formatted text data)
190
190
func (c * Client ) LookupBookContentRaw (id int , f Format ) ([]byte , error ) {
191
- b , err := c .get (c .MakeContentCommand (id , f ))
191
+ b , err := c .get (c .makeContentCommand (id , f ))
192
192
return b , errs .Wrap (err , "" )
193
193
}
194
194
195
195
//LookupPersonRaw gets person data (raw data)
196
196
func (c * Client ) LookupPersonRaw (id int ) ([]byte , error ) {
197
- b , err := c .get (c .MakeLookupCommand (TargetPersons , id ))
197
+ b , err := c .get (c .makeLookupCommand (TargetPersons , id ))
198
198
return b , errs .Wrap (err , "" )
199
199
}
200
200
@@ -210,7 +210,7 @@ func (c *Client) LookupPerson(id int) (*Person, error) {
210
210
211
211
//LookupWorker gets worker data (raw data)
212
212
func (c * Client ) LookupWorkerRaw (id int ) ([]byte , error ) {
213
- b , err := c .get (c .MakeLookupCommand (TargetWorkers , id ))
213
+ b , err := c .get (c .makeLookupCommand (TargetWorkers , id ))
214
214
return b , errs .Wrap (err , "" )
215
215
}
216
216
@@ -226,7 +226,7 @@ func (c *Client) LookupWorker(id int) (*Worker, error) {
226
226
227
227
//RankingRaw gets ranking data (raw data)
228
228
func (c * Client ) RankingRaw (tm time.Time ) ([]byte , error ) {
229
- b , err := c .get (c .MakeRankingCommand (tm ))
229
+ b , err := c .get (c .makeRankingCommand (tm ))
230
230
return b , errs .Wrap (err , "" )
231
231
}
232
232
@@ -240,60 +240,59 @@ func (c *Client) Ranking(tm time.Time) (Ranking, error) {
240
240
return ranking , errs .Wrap (err , "" )
241
241
}
242
242
243
- //MakeSearchCommand returns URI for search command
244
- func (c * Client ) MakeSearchCommand (t Target , v url.Values ) * url.URL {
243
+ func (c * Client ) makeSearchCommand (t Target , v url.Values ) * url.URL {
245
244
u := c .server .URL ()
246
- u .Path = fmt .Sprintf ("/%v/%v" , APIVersion , t )
245
+ u .Path = fmt .Sprintf ("/%v/%v" , c . apiDir () , t )
247
246
u .RawQuery = v .Encode ()
248
247
return u
249
248
}
250
249
251
- //MakeLookupCommand returns URI for lookup command
252
- func (c * Client ) MakeLookupCommand (t Target , id int ) * url.URL {
250
+ func (c * Client ) makeLookupCommand (t Target , id int ) * url.URL {
253
251
u := c .server .URL ()
254
- u .Path = fmt .Sprintf ("/%v/%v/%v" , APIVersion , t , strconv .Itoa (id ))
252
+ u .Path = fmt .Sprintf ("/%v/%v/%v" , c . apiDir () , t , strconv .Itoa (id ))
255
253
return u
256
254
}
257
255
258
- //MakeLookupCommand returns URI for lookup command
259
- func (c * Client ) MakeCardCommand (id int ) * url.URL {
260
- u := c .MakeLookupCommand (TargetBooks , id )
256
+ func (c * Client ) makeCardCommand (id int ) * url.URL {
257
+ u := c .makeLookupCommand (TargetBooks , id )
261
258
u .Path = u .Path + "/card"
262
259
return u
263
260
}
264
261
265
- //MakeLookupCommand returns URI for lookup command
266
- func (c * Client ) MakeContentCommand (id int , f Format ) * url.URL {
267
- u := c .MakeLookupCommand (TargetBooks , id )
262
+ func (c * Client ) makeContentCommand (id int , f Format ) * url.URL {
263
+ u := c .makeLookupCommand (TargetBooks , id )
268
264
u .Path = u .Path + "/content"
269
265
u .RawQuery = (url.Values {"format" : {f .String ()}}).Encode ()
270
266
return u
271
267
}
272
268
273
- //MakeLookupCommand returns URI for lookup ranking info command
274
- func (c * Client ) MakeRankingCommand (tm time.Time ) * url.URL {
269
+ func (c * Client ) makeRankingCommand (tm time.Time ) * url.URL {
275
270
u := c .server .URL ()
276
- u .Path = fmt .Sprintf ("/%v/%v/%v/%v" , APIVersion , TargetRanking , "xhtml" , tm .Format ("2006/01" ))
271
+ u .Path = fmt .Sprintf ("/%v/%v/%v/%v" , c . apiDir () , TargetRanking , "xhtml" , tm .Format ("2006/01" ))
277
272
return u
278
273
}
279
274
275
+ func (c * Client ) apiDir () string {
276
+ return defaultAPIDir
277
+ }
278
+
280
279
func (c * Client ) get (u * url.URL ) ([]byte , error ) {
281
280
req , err := http .NewRequestWithContext (c .ctx , "GET" , u .String (), nil )
282
281
if err != nil {
283
- return nil , errs .Wrap (err , "" , errs .WithParam ("url" , u .String ()))
282
+ return nil , errs .Wrap (err , "" , errs .WithContext ("url" , u .String ()))
284
283
}
285
284
resp , err := c .client .Do (req )
286
285
if err != nil {
287
- return nil , errs .Wrap (err , "" , errs .WithParam ("url" , u .String ()))
286
+ return nil , errs .Wrap (err , "" , errs .WithContext ("url" , u .String ()))
288
287
}
289
288
defer resp .Body .Close ()
290
289
291
290
if ! (resp .StatusCode != 0 && resp .StatusCode < http .StatusBadRequest ) {
292
- return nil , errs .Wrap (ErrHTTPStatus , "" , errs .WithParam ("url" , u .String ()), errs .WithParam ("status" , resp .Status ))
291
+ return nil , errs .Wrap (ErrHTTPStatus , "" , errs .WithContext ("url" , u .String ()), errs .WithContext ("status" , resp .Status ))
293
292
}
294
293
body , err := ioutil .ReadAll (resp .Body )
295
294
if err != nil {
296
- return body , errs .Wrap (err , "" , errs .WithParam ("url" , u .String ()))
295
+ return body , errs .Wrap (err , "" , errs .WithContext ("url" , u .String ()))
297
296
}
298
297
return body , nil
299
298
}
0 commit comments