diff --git a/client.go b/client.go index 8078b4a..99d18fd 100644 --- a/client.go +++ b/client.go @@ -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 @@ -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())) @@ -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) @@ -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=") @@ -107,7 +115,7 @@ 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())) @@ -115,7 +123,7 @@ func (c *Client) signiture(signed string, hds *headers) string { 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(), @@ -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") } @@ -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. diff --git a/client_test.go b/client_test.go index cc09da3..32fa5e3 100644 --- a/client_test.go +++ b/client_test.go @@ -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) } @@ -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. diff --git a/go.mod b/go.mod index 9cfe2bb..01bcfd8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3311155..2437e58 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/server.go b/server.go index 19d5f25..cdb14e1 100644 --- a/server.go +++ b/server.go @@ -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, @@ -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 } @@ -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.