From f0c5dc61d3f09877f58dbe153fa528d53d194314 Mon Sep 17 00:00:00 2001 From: Spiegel Date: Mon, 14 Oct 2019 12:17:41 +0900 Subject: [PATCH] Refactoring --- client.go | 57 +++++++++++++++++++++++++------------------------- client_test.go | 12 +++++------ go.mod | 2 +- go.sum | 4 ++-- server.go | 5 +++-- server_test.go | 4 ++-- values-date.go | 2 +- 7 files changed, 43 insertions(+), 43 deletions(-) diff --git a/client.go b/client.go index e2d7caa..497d89a 100644 --- a/client.go +++ b/client.go @@ -13,7 +13,7 @@ import ( ) const ( - APIVersion = "api/v0.1" + defaultAPIDir = "api/v0.1" ) //Client is http.Client for Aozora API Server @@ -32,7 +32,7 @@ func (c *Client) SearchBooksRaw(opts ...SearchBooksParamsFunc) ([]byte, error) { for _, opt := range opts { opt(params) } - b, err := c.get(c.MakeSearchCommand(TargetBooks, params)) + b, err := c.get(c.makeSearchCommand(TargetBooks, params)) return b, errs.Wrap(err, "") } @@ -109,7 +109,7 @@ func (c *Client) SearchPersonsRaw(opts ...SearchPersonsParamsFunc) ([]byte, erro for _, opt := range opts { opt(params) } - b, err := c.get(c.MakeSearchCommand(TargetPersons, params)) + b, err := c.get(c.makeSearchCommand(TargetPersons, params)) return b, errs.Wrap(err, "") } @@ -141,7 +141,7 @@ func (c *Client) SearchWorkersRaw(opts ...SearchWorkersParamsFunc) ([]byte, erro for _, opt := range opts { opt(params) } - b, err := c.get(c.MakeSearchCommand(TargetWorkers, params)) + b, err := c.get(c.makeSearchCommand(TargetWorkers, params)) return b, errs.Wrap(err, "") } @@ -166,7 +166,7 @@ func WithWorkerName(name string) SearchWorkersParamsFunc { //LookupBookRaw gets book data (raw data) func (c *Client) LookupBookRaw(id int) ([]byte, error) { - b, err := c.get(c.MakeLookupCommand(TargetBooks, id)) + b, err := c.get(c.makeLookupCommand(TargetBooks, id)) return b, errs.Wrap(err, "") } @@ -182,19 +182,19 @@ func (c *Client) LookupBook(id int) (*Book, error) { //LookupBookCardRaw gets book card info (HTML page data) func (c *Client) LookupBookCardRaw(id int) ([]byte, error) { - b, err := c.get(c.MakeCardCommand(id)) + b, err := c.get(c.makeCardCommand(id)) return b, errs.Wrap(err, "") } //LookupBookContentRaw gets book content (plain or HTML formatted text data) func (c *Client) LookupBookContentRaw(id int, f Format) ([]byte, error) { - b, err := c.get(c.MakeContentCommand(id, f)) + b, err := c.get(c.makeContentCommand(id, f)) return b, errs.Wrap(err, "") } //LookupPersonRaw gets person data (raw data) func (c *Client) LookupPersonRaw(id int) ([]byte, error) { - b, err := c.get(c.MakeLookupCommand(TargetPersons, id)) + b, err := c.get(c.makeLookupCommand(TargetPersons, id)) return b, errs.Wrap(err, "") } @@ -210,7 +210,7 @@ func (c *Client) LookupPerson(id int) (*Person, error) { //LookupWorker gets worker data (raw data) func (c *Client) LookupWorkerRaw(id int) ([]byte, error) { - b, err := c.get(c.MakeLookupCommand(TargetWorkers, id)) + b, err := c.get(c.makeLookupCommand(TargetWorkers, id)) return b, errs.Wrap(err, "") } @@ -226,7 +226,7 @@ func (c *Client) LookupWorker(id int) (*Worker, error) { //RankingRaw gets ranking data (raw data) func (c *Client) RankingRaw(tm time.Time) ([]byte, error) { - b, err := c.get(c.MakeRankingCommand(tm)) + b, err := c.get(c.makeRankingCommand(tm)) return b, errs.Wrap(err, "") } @@ -240,60 +240,59 @@ func (c *Client) Ranking(tm time.Time) (Ranking, error) { return ranking, errs.Wrap(err, "") } -//MakeSearchCommand returns URI for search command -func (c *Client) MakeSearchCommand(t Target, v url.Values) *url.URL { +func (c *Client) makeSearchCommand(t Target, v url.Values) *url.URL { u := c.server.URL() - u.Path = fmt.Sprintf("/%v/%v", APIVersion, t) + u.Path = fmt.Sprintf("/%v/%v", c.apiDir(), t) u.RawQuery = v.Encode() return u } -//MakeLookupCommand returns URI for lookup command -func (c *Client) MakeLookupCommand(t Target, id int) *url.URL { +func (c *Client) makeLookupCommand(t Target, id int) *url.URL { u := c.server.URL() - u.Path = fmt.Sprintf("/%v/%v/%v", APIVersion, t, strconv.Itoa(id)) + u.Path = fmt.Sprintf("/%v/%v/%v", c.apiDir(), t, strconv.Itoa(id)) return u } -//MakeLookupCommand returns URI for lookup command -func (c *Client) MakeCardCommand(id int) *url.URL { - u := c.MakeLookupCommand(TargetBooks, id) +func (c *Client) makeCardCommand(id int) *url.URL { + u := c.makeLookupCommand(TargetBooks, id) u.Path = u.Path + "/card" return u } -//MakeLookupCommand returns URI for lookup command -func (c *Client) MakeContentCommand(id int, f Format) *url.URL { - u := c.MakeLookupCommand(TargetBooks, id) +func (c *Client) makeContentCommand(id int, f Format) *url.URL { + u := c.makeLookupCommand(TargetBooks, id) u.Path = u.Path + "/content" u.RawQuery = (url.Values{"format": {f.String()}}).Encode() return u } -//MakeLookupCommand returns URI for lookup ranking info command -func (c *Client) MakeRankingCommand(tm time.Time) *url.URL { +func (c *Client) makeRankingCommand(tm time.Time) *url.URL { u := c.server.URL() - u.Path = fmt.Sprintf("/%v/%v/%v/%v", APIVersion, TargetRanking, "xhtml", tm.Format("2006/01")) + u.Path = fmt.Sprintf("/%v/%v/%v/%v", c.apiDir(), TargetRanking, "xhtml", tm.Format("2006/01")) return u } +func (c *Client) apiDir() string { + return defaultAPIDir +} + func (c *Client) get(u *url.URL) ([]byte, error) { req, err := http.NewRequestWithContext(c.ctx, "GET", u.String(), nil) if err != nil { - return nil, errs.Wrap(err, "", errs.WithParam("url", u.String())) + return nil, errs.Wrap(err, "", errs.WithContext("url", u.String())) } resp, err := c.client.Do(req) if err != nil { - return nil, errs.Wrap(err, "", errs.WithParam("url", u.String())) + return nil, errs.Wrap(err, "", errs.WithContext("url", u.String())) } defer resp.Body.Close() if !(resp.StatusCode != 0 && resp.StatusCode < http.StatusBadRequest) { - return nil, errs.Wrap(ErrHTTPStatus, "", errs.WithParam("url", u.String()), errs.WithParam("status", resp.Status)) + return nil, errs.Wrap(ErrHTTPStatus, "", errs.WithContext("url", u.String()), errs.WithContext("status", resp.Status)) } body, err := ioutil.ReadAll(resp.Body) if err != nil { - return body, errs.Wrap(err, "", errs.WithParam("url", u.String())) + return body, errs.Wrap(err, "", errs.WithContext("url", u.String())) } return body, nil } diff --git a/client_test.go b/client_test.go index a40561a..2952398 100644 --- a/client_test.go +++ b/client_test.go @@ -28,7 +28,7 @@ func TestMakeSearchCommand(t *testing.T) { } for _, tc := range testCases { - u := DefaultClient().MakeSearchCommand(tc.t, tc.v) + u := DefaultClient().makeSearchCommand(tc.t, tc.v) if u.String() != tc.str { t.Errorf("Client.MakeSearchCommand() is \"%v\", want \"%v\"", u.String(), tc.str) } @@ -47,7 +47,7 @@ func TestMakeLookupCommand(t *testing.T) { } for _, tc := range testCases { - u := (*Server)(nil).CreateClient(WithContext(context.Background()), WithHttpClient(&http.Client{})).MakeLookupCommand(tc.t, tc.id) + u := (*Server)(nil).CreateClient(WithContext(context.Background()), WithHttpClient(&http.Client{})).makeLookupCommand(tc.t, tc.id) if u.String() != tc.str { t.Errorf("Client.MakeLookupCommand() is \"%v\", want \"%v\"", u.String(), tc.str) } @@ -63,7 +63,7 @@ func TestMakeCardCommand(t *testing.T) { } for _, tc := range testCases { - u := DefaultClient().MakeCardCommand(tc.id) + u := DefaultClient().makeCardCommand(tc.id) if u.String() != tc.str { t.Errorf("Client.MakeCardCommand() is \"%v\", want \"%v\"", u.String(), tc.str) } @@ -82,9 +82,9 @@ func TestMakeContentCommand(t *testing.T) { } for _, tc := range testCases { - u := DefaultClient().MakeContentCommand(tc.id, tc.f) + u := DefaultClient().makeContentCommand(tc.id, tc.f) if u.String() != tc.str { - t.Errorf("Client.MakeContentCommand() is \"%v\", want \"%v\"", u.String(), tc.str) + t.Errorf("Client.makeContentCommand() is \"%v\", want \"%v\"", u.String(), tc.str) } } } @@ -98,7 +98,7 @@ func TestMakeRankingCommand(t *testing.T) { } for _, tc := range testCases { - u := DefaultClient().MakeRankingCommand(tc.tm) + u := DefaultClient().makeRankingCommand(tc.tm) if u.String() != tc.str { t.Errorf("Client.MakeRankingCommand() is \"%v\", want \"%v\"", u.String(), tc.str) } diff --git a/go.mod b/go.mod index ce1ccb1..e277488 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,6 @@ go 1.13 require ( github.com/spf13/cobra v0.0.5 - github.com/spiegel-im-spiegel/errs v0.2.2 + github.com/spiegel-im-spiegel/errs v0.3.1 github.com/spiegel-im-spiegel/gocli v0.10.1 ) diff --git a/go.sum b/go.sum index 9a9e846..583b692 100644 --- a/go.sum +++ b/go.sum @@ -22,8 +22,8 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6 github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= -github.com/spiegel-im-spiegel/errs v0.2.2 h1:ltRYJzh2Vn59HfySjQCex1vOji/dH0xJV6jyzBCeClg= -github.com/spiegel-im-spiegel/errs v0.2.2/go.mod h1:NwHSe6m3oAhRj2bAkkbzz9xAffIDNcP9uTdyJd9fJNs= +github.com/spiegel-im-spiegel/errs v0.3.1 h1:GheZPMUx4o+UxShr1cOjKO5FGIJl4bcr7AY7bp1crUs= +github.com/spiegel-im-spiegel/errs v0.3.1/go.mod h1:NwHSe6m3oAhRj2bAkkbzz9xAffIDNcP9uTdyJd9fJNs= github.com/spiegel-im-spiegel/gocli v0.10.1 h1:XWyq4dKFp2xTjiH2P2bxPZyi0++4IHp9HOksgGdfwpA= github.com/spiegel-im-spiegel/gocli v0.10.1/go.mod h1:9vRvly2giutJ2sAtQjrw570p9ulJA3twgKlurmnj12g= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= diff --git a/server.go b/server.go index 42db718..ff2cd7e 100644 --- a/server.go +++ b/server.go @@ -7,7 +7,8 @@ import ( ) const ( - DefaultHost = "www.aozorahack.net" + defaultScheme = "http" + defaultHost = "www.aozorahack.net" ) //Server is informations of Aozora API @@ -21,7 +22,7 @@ type ServerOptFunc func(*Server) //New returns new Server instance func New(opts ...ServerOptFunc) *Server { - server := &Server{scheme: "http", name: DefaultHost} + server := &Server{scheme: defaultScheme, name: defaultHost} for _, opt := range opts { opt(server) } diff --git a/server_test.go b/server_test.go index 1302b4e..554d9b1 100644 --- a/server_test.go +++ b/server_test.go @@ -7,8 +7,8 @@ func TestServer(t *testing.T) { s *Server str string }{ - {s: (*Server)(nil), str: "http://" + DefaultHost}, - {s: New(), str: "http://" + DefaultHost}, + {s: (*Server)(nil), str: defaultScheme + "://" + defaultHost}, + {s: New(), str: defaultScheme + "://" + defaultHost}, {s: New(WithScheme("foo"), WithServerName("bar")), str: "foo://bar"}, } diff --git a/values-date.go b/values-date.go index 00e01d5..edcbc1e 100644 --- a/values-date.go +++ b/values-date.go @@ -38,7 +38,7 @@ func (t *Date) UnmarshalJSON(b []byte) error { var lastErr error for _, tmplt := range timeTemplate { if tm, err := time.Parse(tmplt, s); err != nil { - lastErr = errs.Wrap(err, "", errs.WithParam("time_string", s), errs.WithParam("time_template", tmplt)) + lastErr = errs.Wrap(err, "", errs.WithContext("time_string", s), errs.WithContext("time_template", tmplt)) } else { *t = Date{tm} return nil