Skip to content

Commit

Permalink
Merge pull request #12 from spiegel-im-spiegel/fix-bugs-and-testing
Browse files Browse the repository at this point in the history
Refactoring API
  • Loading branch information
spiegel-im-spiegel committed Jan 26, 2020
2 parents b5f7d32 + ad0fc0e commit ed58b21
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
32 changes: 20 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ type Query interface {
Payload() ([]byte, error)
}

//Client is http.Client for Aozora API Server
type Client struct {
//Client interface
type Client interface {
Marketplace() string
PartnerTag() string
PartnerType() string
Request(Query) ([]byte, error)
}

//client is http.Client for Aozora API Server
type client struct {
server *Server
client *http.Client
ctx context.Context
Expand All @@ -35,21 +43,21 @@ type Client struct {
}

//Marketplace returns name of Marketplace parameter for PA-API v5
func (c *Client) Marketplace() string {
func (c *client) Marketplace() string {
return c.server.Marketplace()
}

//PartnerTag returns PartnerTag parameter for PA-API v5
func (c *Client) PartnerTag() string {
func (c *client) PartnerTag() string {
return c.partnerTag
}

//PartnerType returns PartnerType parameter for PA-API v5
func (c *Client) PartnerType() string {
func (c *client) PartnerType() string {
return defaultPartnerType
}

func (c *Client) Request(q Query) ([]byte, error) {
func (c *client) Request(q Query) ([]byte, error) {
payload, err := q.Payload()
if err != nil {
return nil, errs.Wrap(err, "", errs.WithContext("Operation", q.Operation().String()))
Expand All @@ -61,7 +69,7 @@ func (c *Client) Request(q Query) ([]byte, error) {
return b, nil
}

func (c *Client) post(cmd Operation, payload []byte) ([]byte, error) {
func (c *client) post(cmd Operation, payload []byte) ([]byte, error) {
dt := NewTimeStamp(time.Now())
u := c.server.URL(cmd.Path())
hds := newHeaders(c.server, cmd, dt)
Expand Down Expand Up @@ -95,7 +103,7 @@ func (c *Client) post(cmd Operation, payload []byte) ([]byte, error) {
return body, nil
}

func (c *Client) authorization(sig string, hds *headers) string {
func (c *client) authorization(sig string, hds *headers) string {
buf := bytes.Buffer{}
buf.WriteString(c.server.HMACAlgorithm())
buf.WriteString(" Credential=")
Expand All @@ -107,15 +115,15 @@ func (c *Client) authorization(sig string, hds *headers) string {
return buf.String()
}

func (c *Client) signiture(signed string, hds *headers) string {
func (c *client) signiture(signed string, hds *headers) string {
dateKey := hmacSHA256([]byte("AWS4"+c.secretKey), []byte(hds.dt.StringDate()))
regionKey := hmacSHA256(dateKey, []byte(c.server.Region()))
serviceKey := hmacSHA256(regionKey, []byte(c.server.ServiceName()))
requestKey := hmacSHA256(serviceKey, []byte(c.server.AWS4Request()))
return hex.EncodeToString(hmacSHA256(requestKey, []byte(signed)))
}

func (c *Client) signedString(hds *headers, payload []byte) string {
func (c *client) signedString(hds *headers, payload []byte) string {
return strings.Join(
[]string{
c.server.HMACAlgorithm(),
Expand All @@ -127,7 +135,7 @@ func (c *Client) signedString(hds *headers, payload []byte) string {
)
}

func (c *Client) canonicalRequest(hds *headers, payload []byte) string {
func (c *client) canonicalRequest(hds *headers, payload []byte) string {
request := []string{"POST", hds.cmd.Path(), "", hds.values(), "", hds.list(), hashedString(payload)}
return strings.Join(request, "\n")
}
Expand Down Expand Up @@ -181,7 +189,7 @@ func (h *headers) values() string {
return strings.Join(list, "\n")
}

/* Copyright 2019 Spiegel
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
24 changes: 12 additions & 12 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ func TestClient(t *testing.T) {
},
}
for _, tc := range testCases {
client := New().CreateClient(tc.partnerTag, tc.accessKey, tc.secretKey)
if client.Marketplace() != tc.marketplace {
t.Errorf("Client.Marketplace() is \"%v\", want \"%v\"", client.Marketplace(), tc.marketplace)
c := New().CreateClient(tc.partnerTag, tc.accessKey, tc.secretKey)
if c.Marketplace() != tc.marketplace {
t.Errorf("Client.Marketplace() is \"%v\", want \"%v\"", c.Marketplace(), tc.marketplace)
}
if client.PartnerTag() != tc.partnerTag {
t.Errorf("Client.PartnerTag() is \"%v\", want \"%v\"", client.PartnerTag(), tc.partnerTag)
if c.PartnerTag() != tc.partnerTag {
t.Errorf("Client.PartnerTag() is \"%v\", want \"%v\"", c.PartnerTag(), tc.partnerTag)
}
if client.PartnerType() != tc.partnerType {
t.Errorf("Client.PartnerType() is \"%v\", want \"%v\"", client.PartnerType(), tc.partnerType)
if c.PartnerType() != tc.partnerType {
t.Errorf("Client.PartnerType() is \"%v\", want \"%v\"", c.PartnerType(), tc.partnerType)
}
hds := newHeaders(client.server, GetItems, tc.date)
hds := newHeaders(c.(*client).server, GetItems, tc.date)
if hds.get("Content-Encoding") != tc.contentEncoding {
t.Errorf("headers.get(\"Content-Encoding\") is \"%v\", want \"%v\"", hds.get("Content-Encoding"), tc.contentEncoding)
}
Expand All @@ -63,22 +63,22 @@ func TestClient(t *testing.T) {
if hds.get("X-Amz-Target") != tc.xAmzTarget {
t.Errorf("headers.get(\"X-Amz-Target\") is \"%v\", want \"%v\"", hds.get("X-Amz-Target"), tc.xAmzTarget)
}
str := client.signedString(hds, tc.payload)
str := c.(*client).signedString(hds, tc.payload)
if str != tc.sigedText {
t.Errorf("Client.signedString() is \"%v\", want \"%v\"", str, tc.sigedText)
}
sig := client.signiture(str, hds)
sig := c.(*client).signiture(str, hds)
if sig != tc.sig {
t.Errorf("Client.signiture() is \"%v\", want \"%v\"", sig, tc.sig)
}
auth := client.authorization(sig, hds)
auth := c.(*client).authorization(sig, hds)
if auth != tc.authorization {
t.Errorf("Client.authorization() is \"%v\", want \"%v\"", auth, tc.authorization)
}
}
}

/* Copyright 2019 Spiegel
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/spiegel-im-spiegel/pa-api

go 1.13

require github.com/spiegel-im-spiegel/errs v0.3.3
require github.com/spiegel-im-spiegel/errs v0.3.4
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/spiegel-im-spiegel/errs v0.3.3 h1:fFjMj6C45bY9z8ByckmrTwSgUwi/zBL/dgksIEzoHYA=
github.com/spiegel-im-spiegel/errs v0.3.3/go.mod h1:NwHSe6m3oAhRj2bAkkbzz9xAffIDNcP9uTdyJd9fJNs=
github.com/spiegel-im-spiegel/errs v0.3.4 h1:lqiLtlT2ex7zpYS+WNWFuFX2x/o8RnG/UeaA8CFRYeE=
github.com/spiegel-im-spiegel/errs v0.3.4/go.mod h1:NwHSe6m3oAhRj2bAkkbzz9xAffIDNcP9uTdyJd9fJNs=
18 changes: 9 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ func (s *Server) ContentEncoding() string {
}

//ClientOptFunc type is self-referential function type for Server.CreateClient method. (functional options pattern)
type ClientOptFunc func(*Client)
type ClientOptFunc func(*client)

//CreateClient method returns an Client instance with associate-tag, access-key, secret-key, and other options.
func (s *Server) CreateClient(associateTag, accessKey, secretKey string, opts ...ClientOptFunc) *Client {
func (s *Server) CreateClient(associateTag, accessKey, secretKey string, opts ...ClientOptFunc) Client {
if s == nil {
s = New()
}
cli := &Client{
cli := &client{
server: s,
client: nil,
ctx: nil,
Expand All @@ -160,7 +160,7 @@ func (s *Server) CreateClient(associateTag, accessKey, secretKey string, opts ..
//WithContext function returns ClientOptFunc function value.
//This function is used in Server.CreateClient method that represents context.Context.
func WithContext(ctx context.Context) ClientOptFunc {
return func(c *Client) {
return func(c *client) {
if c != nil {
c.ctx = ctx
}
Expand All @@ -169,20 +169,20 @@ func WithContext(ctx context.Context) ClientOptFunc {

//WithHttpClient function returns ClientOptFunc function value.
//This function is used in Server.CreateClient method that represents http.Client.
func WithHttpClient(client *http.Client) ClientOptFunc {
return func(c *Client) {
func WithHttpClient(hc *http.Client) ClientOptFunc {
return func(c *client) {
if c != nil {
c.client = client
c.client = hc
}
}
}

//DefaultClient function returns an default Client instance with associate-tag, access-key, and secret-key parameters.
func DefaultClient(associateTag, accessKey, secretKey string) *Client {
func DefaultClient(associateTag, accessKey, secretKey string) Client {
return New().CreateClient(associateTag, accessKey, secretKey)
}

/* Copyright 2019 Spiegel and contributors
/* Copyright 2019,2020 Spiegel and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit ed58b21

Please sign in to comment.