@@ -13,7 +13,7 @@ import (
1313)
1414
1515const (
16- APIVersion = "api/v0.1"
16+ defaultAPIDir = "api/v0.1"
1717)
1818
1919//Client is http.Client for Aozora API Server
@@ -32,7 +32,7 @@ func (c *Client) SearchBooksRaw(opts ...SearchBooksParamsFunc) ([]byte, error) {
3232 for _ , opt := range opts {
3333 opt (params )
3434 }
35- b , err := c .get (c .MakeSearchCommand (TargetBooks , params ))
35+ b , err := c .get (c .makeSearchCommand (TargetBooks , params ))
3636 return b , errs .Wrap (err , "" )
3737}
3838
@@ -109,7 +109,7 @@ func (c *Client) SearchPersonsRaw(opts ...SearchPersonsParamsFunc) ([]byte, erro
109109 for _ , opt := range opts {
110110 opt (params )
111111 }
112- b , err := c .get (c .MakeSearchCommand (TargetPersons , params ))
112+ b , err := c .get (c .makeSearchCommand (TargetPersons , params ))
113113 return b , errs .Wrap (err , "" )
114114}
115115
@@ -141,7 +141,7 @@ func (c *Client) SearchWorkersRaw(opts ...SearchWorkersParamsFunc) ([]byte, erro
141141 for _ , opt := range opts {
142142 opt (params )
143143 }
144- b , err := c .get (c .MakeSearchCommand (TargetWorkers , params ))
144+ b , err := c .get (c .makeSearchCommand (TargetWorkers , params ))
145145 return b , errs .Wrap (err , "" )
146146}
147147
@@ -166,7 +166,7 @@ func WithWorkerName(name string) SearchWorkersParamsFunc {
166166
167167//LookupBookRaw gets book data (raw data)
168168func (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 ))
170170 return b , errs .Wrap (err , "" )
171171}
172172
@@ -182,19 +182,19 @@ func (c *Client) LookupBook(id int) (*Book, error) {
182182
183183//LookupBookCardRaw gets book card info (HTML page data)
184184func (c * Client ) LookupBookCardRaw (id int ) ([]byte , error ) {
185- b , err := c .get (c .MakeCardCommand (id ))
185+ b , err := c .get (c .makeCardCommand (id ))
186186 return b , errs .Wrap (err , "" )
187187}
188188
189189//LookupBookContentRaw gets book content (plain or HTML formatted text data)
190190func (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 ))
192192 return b , errs .Wrap (err , "" )
193193}
194194
195195//LookupPersonRaw gets person data (raw data)
196196func (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 ))
198198 return b , errs .Wrap (err , "" )
199199}
200200
@@ -210,7 +210,7 @@ func (c *Client) LookupPerson(id int) (*Person, error) {
210210
211211//LookupWorker gets worker data (raw data)
212212func (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 ))
214214 return b , errs .Wrap (err , "" )
215215}
216216
@@ -226,7 +226,7 @@ func (c *Client) LookupWorker(id int) (*Worker, error) {
226226
227227//RankingRaw gets ranking data (raw data)
228228func (c * Client ) RankingRaw (tm time.Time ) ([]byte , error ) {
229- b , err := c .get (c .MakeRankingCommand (tm ))
229+ b , err := c .get (c .makeRankingCommand (tm ))
230230 return b , errs .Wrap (err , "" )
231231}
232232
@@ -240,60 +240,59 @@ func (c *Client) Ranking(tm time.Time) (Ranking, error) {
240240 return ranking , errs .Wrap (err , "" )
241241}
242242
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 {
245244 u := c .server .URL ()
246- u .Path = fmt .Sprintf ("/%v/%v" , APIVersion , t )
245+ u .Path = fmt .Sprintf ("/%v/%v" , c . apiDir () , t )
247246 u .RawQuery = v .Encode ()
248247 return u
249248}
250249
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 {
253251 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 ))
255253 return u
256254}
257255
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 )
261258 u .Path = u .Path + "/card"
262259 return u
263260}
264261
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 )
268264 u .Path = u .Path + "/content"
269265 u .RawQuery = (url.Values {"format" : {f .String ()}}).Encode ()
270266 return u
271267}
272268
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 {
275270 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" ))
277272 return u
278273}
279274
275+ func (c * Client ) apiDir () string {
276+ return defaultAPIDir
277+ }
278+
280279func (c * Client ) get (u * url.URL ) ([]byte , error ) {
281280 req , err := http .NewRequestWithContext (c .ctx , "GET" , u .String (), nil )
282281 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 ()))
284283 }
285284 resp , err := c .client .Do (req )
286285 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 ()))
288287 }
289288 defer resp .Body .Close ()
290289
291290 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 ))
293292 }
294293 body , err := ioutil .ReadAll (resp .Body )
295294 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 ()))
297296 }
298297 return body , nil
299298}
0 commit comments