Skip to content

Commit

Permalink
Merge pull request #5 from spiegel-im-spiegel/fix-bugs
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
spiegel-im-spiegel authored Oct 14, 2019
2 parents de59f8a + f0c5dc6 commit d4eecd8
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 43 deletions.
57 changes: 28 additions & 29 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
APIVersion = "api/v0.1"
defaultAPIDir = "api/v0.1"
)

//Client is http.Client for Aozora API Server
Expand All @@ -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, "")
}

Expand Down Expand Up @@ -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, "")
}

Expand Down Expand Up @@ -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, "")
}

Expand All @@ -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, "")
}

Expand All @@ -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, "")
}

Expand All @@ -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, "")
}

Expand All @@ -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, "")
}

Expand All @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
5 changes: 3 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

const (
DefaultHost = "www.aozorahack.net"
defaultScheme = "http"
defaultHost = "www.aozorahack.net"
)

//Server is informations of Aozora API
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}

Expand Down
2 changes: 1 addition & 1 deletion values-date.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d4eecd8

Please sign in to comment.