Support custom client for amazon app store

This commit is contained in:
Junpei Tsuji
2018-05-18 11:42:20 +09:00
parent d53311131e
commit 034e269dc9
2 changed files with 29 additions and 63 deletions

View File

@@ -7,7 +7,6 @@ import (
"fmt"
"net/http"
"os"
"time"
)
const (
@@ -25,13 +24,6 @@ func getSandboxURL() string {
return url
}
// Config is a configuration to initialize client
type Config struct {
IsProduction bool
Secret string
TimeOut time.Duration
}
// The IAPResponse type has the response properties
type IAPResponse struct {
ReceiptID string `json:"receiptId"`
@@ -57,34 +49,31 @@ type IAPClient interface {
type Client struct {
URL string
Secret string
TimeOut time.Duration
httpCli *http.Client
}
// New creates a client object
func New(secret string) IAPClient {
client := Client{
func New(secret string) *Client {
client := &Client{
URL: getSandboxURL(),
Secret: secret,
TimeOut: time.Second * 5,
httpCli: http.DefaultClient,
}
if os.Getenv("IAP_ENVIRONMENT") == "production" {
client.URL = ProductionURL
}
return client
}
// NewWithConfig creates a client with configuration
func NewWithConfig(config Config) Client {
if config.TimeOut == 0 {
config.TimeOut = time.Second * 5
}
client := Client{
// NewWithClient creates a client with a custom client.
func NewWithClient(secret string, cli *http.Client) *Client {
client := &Client{
URL: getSandboxURL(),
Secret: config.Secret,
TimeOut: config.TimeOut,
Secret: secret,
httpCli: cli,
}
if config.IsProduction {
if os.Getenv("IAP_ENVIRONMENT") == "production" {
client.URL = ProductionURL
}
@@ -92,19 +81,16 @@ func NewWithConfig(config Config) Client {
}
// Verify sends receipts and gets validation result
func (c Client) Verify(ctx context.Context, userID string, receiptID string) (IAPResponse, error) {
func (c *Client) Verify(ctx context.Context, userID string, receiptID string) (IAPResponse, error) {
result := IAPResponse{}
url := fmt.Sprintf("%v/version/1.0/verifyReceiptId/developer/%v/user/%v/receiptId/%v", c.URL, c.Secret, userID, receiptID)
client := http.Client{
Timeout: c.TimeOut,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return result, err
}
req = req.WithContext(ctx)
resp, err := client.Do(req)
resp, err := c.httpCli.Do(req)
if err != nil {
return result, fmt.Errorf("%v", err)
}