Skip to content

Commit

Permalink
[modify] tidy files.
Browse files Browse the repository at this point in the history
  • Loading branch information
mythrnr committed Jun 9, 2021
1 parent 2ebdbcf commit 40c903c
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 220 deletions.
89 changes: 66 additions & 23 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,11 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
)

// timeout is the maximum timeout setting for the client.
// It is recommended to set it to 30s or more, so double it.
// Normally, no response is expected if you wait longer than this.
//
// timeout はクライアントの最長タイムアウト設定.
// 30s 以上に設定することが推奨されている為, 倍を設定.
// 通常, これ以上待機してもレスポンスは期待できない.
const timeout = 60 * time.Second

type ctxkeyTimeout struct{}

func ctxWithTimeout(ctx context.Context, d time.Duration) context.Context {
return context.WithValue(ctx, &ctxkeyTimeout{}, d)
}

func getTimeout(ctx context.Context) time.Duration {
if d, ok := ctx.Value(&ctxkeyTimeout{}).(time.Duration); ok {
return d
}

return timeout
}
"github.com/mythrnr/paypayopa-sdk-go/internal"
)

// opaClient is the client for handling requests/responses to the PayPay API.
//
Expand Down Expand Up @@ -208,3 +188,66 @@ func (c *opaClient) Do(req *http.Request, res interface{}) (*ResultInfo, error)

return info, nil
}

// timeout is the maximum timeout setting for the client.
// It is recommended to set it to 30s or more, so double it.
// Normally, no response is expected if you wait longer than this.
//
// timeout はクライアントの最長タイムアウト設定.
// 30s 以上に設定することが推奨されている為, 倍を設定.
// 通常, これ以上待機してもレスポンスは期待できない.
const timeout = 60 * time.Second

type ctxkeyTimeout struct{}

func ctxWithTimeout(ctx context.Context, d time.Duration) context.Context {
return context.WithValue(ctx, &ctxkeyTimeout{}, d)
}

func getTimeout(ctx context.Context) time.Duration {
if d, ok := ctx.Value(&ctxkeyTimeout{}).(time.Duration); ok {
return d
}

return timeout
}

type authInterceptor struct {
creds *Credential
next http.RoundTripper
}

var _ http.RoundTripper = (*authInterceptor)(nil)

func newAuthenticateInterceptor(
creds *Credential,
next http.RoundTripper,
) http.RoundTripper {
if creds == nil {
panic("*Credential must not be nil")
}

return &authInterceptor{creds: creds, next: next}
}

// RoundTrip intercepts the request and sets the authentication information.
//
// RoundTrip はリクエストに割り込んで認証情報を設定する.
func (i *authInterceptor) RoundTrip(req *http.Request) (*http.Response, error) {
s, err := internal.NewSigner(i.creds.apiKey, i.creds.apiKeySecret, req)
if err != nil {
return nil, err
}

u, _ := url.Parse(string(i.creds.env) + req.URL.String())
req.URL = u

req.Header.Set("Content-Type", s.ContentType())
req.Header.Set("Authorization", s.Sign())

if i.creds.merchantID != "" {
req.Header.Set("X-ASSUME-MERCHANT", i.creds.merchantID)
}

return i.next.RoundTrip(req)
}
41 changes: 0 additions & 41 deletions client_authenticate_inner_test.go

This file was deleted.

52 changes: 26 additions & 26 deletions client_inner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,6 @@ import (
"github.com/stretchr/testify/require"
)

func Test_ctx_timeout(t *testing.T) {
t.Parallel()

t.Run("timeout is set", func(t *testing.T) {
t.Parallel()

expected := time.Second

ctx := ctxWithTimeout(context.Background(), expected)
actual := getTimeout(ctx)

assert.Equal(t, expected, actual)
})

t.Run("timeout is not set", func(t *testing.T) {
t.Parallel()

ctx := context.Background()
actual := getTimeout(ctx)

assert.Equal(t, timeout, actual)
})
}

func Test_newClient(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -96,7 +72,7 @@ func Test_newClientWithHTTPClient(t *testing.T) {
})
}

func Test_clientImpl_Request(t *testing.T) {
func Test_opaClient_Request(t *testing.T) {
t.Parallel()

t.Run("Invalid request body", func(t *testing.T) {
Expand Down Expand Up @@ -177,7 +153,7 @@ func Test_clientImpl_Request(t *testing.T) {
})
}

func Test_clientImpl_Do(t *testing.T) {
func Test_opaClient_Do(t *testing.T) {
t.Parallel()

t.Run("Timeout", func(t *testing.T) {
Expand Down Expand Up @@ -521,3 +497,27 @@ func Test_clientImpl_Do(t *testing.T) {
assert.Equal(t, "value", res.Key)
})
}

func Test_ctx_timeout(t *testing.T) {
t.Parallel()

t.Run("timeout is set", func(t *testing.T) {
t.Parallel()

expected := time.Second

ctx := ctxWithTimeout(context.Background(), expected)
actual := getTimeout(ctx)

assert.Equal(t, expected, actual)
})

t.Run("timeout is not set", func(t *testing.T) {
t.Parallel()

ctx := context.Background()
actual := getTimeout(ctx)

assert.Equal(t, timeout, actual)
})
}
46 changes: 0 additions & 46 deletions client_interceptor.go

This file was deleted.

25 changes: 25 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ type Currency string
// CurrencyJPY は日本円を表す通貨単位.
const CurrencyJPY Currency = "JPY"

// Environment is a type used to specify the destination of the request.
//
// Environment はリクエストの送信先を指定するための型.
type Environment string

const (
// EnvProduction is a value that specifies that the request should be
// sent to the production environment.
//
// EnvProduction は本番環境にリクエストを送ることを指定する値.
EnvProduction Environment = "https://api.paypay.ne.jp"

// EnvStaging is a value that specifies that the request should be
// sent to the staging environment.
//
// EnvStaging はステージング環境にリクエストを送ることを指定する値.
EnvStaging Environment = "https://stg-api.paypay.ne.jp"

// EnvSandbox is a value that specifies that the request should be
// sent to the sandbox environment.
//
// EnvSandbox はサンドボックス環境にリクエストを送ることを指定する値.
EnvSandbox Environment = "https://stg-api.sandbox.paypay.ne.jp"
)

// Lang is a value specified in the lang header to set
// the language of the cashback message text.
//
Expand Down
25 changes: 0 additions & 25 deletions client_credential.go → credential.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
package paypayopa

// Environment is a type used to specify the destination of the request.
//
// Environment はリクエストの送信先を指定するための型.
type Environment string

const (
// EnvProduction is a value that specifies that the request should be
// sent to the production environment.
//
// EnvProduction は本番環境にリクエストを送ることを指定する値.
EnvProduction Environment = "https://api.paypay.ne.jp"

// EnvStaging is a value that specifies that the request should be
// sent to the staging environment.
//
// EnvStaging はステージング環境にリクエストを送ることを指定する値.
EnvStaging Environment = "https://stg-api.paypay.ne.jp"

// EnvSandbox is a value that specifies that the request should be
// sent to the sandbox environment.
//
// EnvSandbox はサンドボックス環境にリクエストを送ることを指定する値.
EnvSandbox Environment = "https://stg-api.sandbox.paypay.ne.jp"
)

// Credential is a structure that holds credentials.
//
// Credential は資格情報を保持する構造体.
Expand Down
8 changes: 4 additions & 4 deletions nonce.go → internal/nonce.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package paypayopa
package internal

import (
"math/rand"
"time"
)

// Nonce generates a random string with a specified number of characters.
// nonce generates a random string with a specified number of characters.
//
// Nonce は指定された文字数のランダムな文字列を生成する.
func Nonce(n uint) string {
// nonce は指定された文字数のランダムな文字列を生成する.
func nonce(n uint) string {
if n == 0 {
return ""
}
Expand Down
7 changes: 3 additions & 4 deletions nonce_test.go → internal/nonce_inner_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package paypayopa_test
package internal

import (
"regexp"
"testing"

"github.com/mythrnr/paypayopa-sdk-go"
"github.com/stretchr/testify/assert"
)

Expand All @@ -14,7 +13,7 @@ var (
ptn100 = regexp.MustCompile("[0-9a-zA-Z]{100}")
)

func Test_None(t *testing.T) {
func Test_Nonce(t *testing.T) {
t.Parallel()

tests := []struct {
Expand Down Expand Up @@ -44,7 +43,7 @@ func Test_None(t *testing.T) {

assert.Equal(t,
tt.want,
tt.rgx.Match([]byte(paypayopa.Nonce(tt.num))),
tt.rgx.Match([]byte(nonce(tt.num))),
)
}
}
Loading

0 comments on commit 40c903c

Please sign in to comment.