Support custom client for amazon app store
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -25,13 +24,6 @@ func getSandboxURL() string {
|
|||||||
return url
|
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
|
// The IAPResponse type has the response properties
|
||||||
type IAPResponse struct {
|
type IAPResponse struct {
|
||||||
ReceiptID string `json:"receiptId"`
|
ReceiptID string `json:"receiptId"`
|
||||||
@@ -57,34 +49,31 @@ type IAPClient interface {
|
|||||||
type Client struct {
|
type Client struct {
|
||||||
URL string
|
URL string
|
||||||
Secret string
|
Secret string
|
||||||
TimeOut time.Duration
|
httpCli *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a client object
|
// New creates a client object
|
||||||
func New(secret string) IAPClient {
|
func New(secret string) *Client {
|
||||||
client := Client{
|
client := &Client{
|
||||||
URL: getSandboxURL(),
|
URL: getSandboxURL(),
|
||||||
Secret: secret,
|
Secret: secret,
|
||||||
TimeOut: time.Second * 5,
|
httpCli: http.DefaultClient,
|
||||||
}
|
}
|
||||||
if os.Getenv("IAP_ENVIRONMENT") == "production" {
|
if os.Getenv("IAP_ENVIRONMENT") == "production" {
|
||||||
client.URL = ProductionURL
|
client.URL = ProductionURL
|
||||||
}
|
}
|
||||||
|
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWithConfig creates a client with configuration
|
// NewWithClient creates a client with a custom client.
|
||||||
func NewWithConfig(config Config) Client {
|
func NewWithClient(secret string, cli *http.Client) *Client {
|
||||||
if config.TimeOut == 0 {
|
client := &Client{
|
||||||
config.TimeOut = time.Second * 5
|
|
||||||
}
|
|
||||||
|
|
||||||
client := Client{
|
|
||||||
URL: getSandboxURL(),
|
URL: getSandboxURL(),
|
||||||
Secret: config.Secret,
|
Secret: secret,
|
||||||
TimeOut: config.TimeOut,
|
httpCli: cli,
|
||||||
}
|
}
|
||||||
if config.IsProduction {
|
if os.Getenv("IAP_ENVIRONMENT") == "production" {
|
||||||
client.URL = ProductionURL
|
client.URL = ProductionURL
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,19 +81,16 @@ func NewWithConfig(config Config) Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify sends receipts and gets validation result
|
// 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{}
|
result := IAPResponse{}
|
||||||
url := fmt.Sprintf("%v/version/1.0/verifyReceiptId/developer/%v/user/%v/receiptId/%v", c.URL, c.Secret, userID, receiptID)
|
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)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := c.httpCli.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return result, fmt.Errorf("%v", err)
|
return result, fmt.Errorf("%v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,10 +59,10 @@ func TestHandle400Error(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
expected := Client{
|
expected := &Client{
|
||||||
URL: SandboxURL,
|
URL: SandboxURL,
|
||||||
TimeOut: time.Second * 5,
|
|
||||||
Secret: "developerSecret",
|
Secret: "developerSecret",
|
||||||
|
httpCli: http.DefaultClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
actual := New("developerSecret")
|
actual := New("developerSecret")
|
||||||
@@ -72,10 +72,10 @@ func TestNew(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNewWithEnvironment(t *testing.T) {
|
func TestNewWithEnvironment(t *testing.T) {
|
||||||
expected := Client{
|
expected := &Client{
|
||||||
URL: ProductionURL,
|
URL: ProductionURL,
|
||||||
TimeOut: time.Second * 5,
|
|
||||||
Secret: "developerSecret",
|
Secret: "developerSecret",
|
||||||
|
httpCli: http.DefaultClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Setenv("IAP_ENVIRONMENT", "production")
|
os.Setenv("IAP_ENVIRONMENT", "production")
|
||||||
@@ -87,40 +87,20 @@ func TestNewWithEnvironment(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewWithConfig(t *testing.T) {
|
func TestNewWithClient(t *testing.T) {
|
||||||
t.Parallel()
|
expected := &Client{
|
||||||
config := Config{
|
|
||||||
IsProduction: true,
|
|
||||||
Secret: "developerSecret",
|
|
||||||
TimeOut: time.Second * 2,
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := Client{
|
|
||||||
URL: ProductionURL,
|
URL: ProductionURL,
|
||||||
TimeOut: time.Second * 2,
|
|
||||||
Secret: "developerSecret",
|
Secret: "developerSecret",
|
||||||
|
httpCli: &http.Client{
|
||||||
|
Timeout: time.Second * 2,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
os.Setenv("IAP_ENVIRONMENT", "production")
|
||||||
|
|
||||||
actual := NewWithConfig(config)
|
cli := &http.Client{
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
Timeout: time.Second * 2,
|
||||||
t.Errorf("got %v\nwant %v", actual, expected)
|
|
||||||
}
|
}
|
||||||
}
|
actual := NewWithClient("developerSecret", cli)
|
||||||
|
|
||||||
func TestNewWithConfigTimeout(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
config := Config{
|
|
||||||
IsProduction: true,
|
|
||||||
Secret: "developerSecret",
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := Client{
|
|
||||||
URL: ProductionURL,
|
|
||||||
TimeOut: time.Second * 5,
|
|
||||||
Secret: "developerSecret",
|
|
||||||
}
|
|
||||||
|
|
||||||
actual := NewWithConfig(config)
|
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
t.Errorf("got %v\nwant %v", actual, expected)
|
t.Errorf("got %v\nwant %v", actual, expected)
|
||||||
}
|
}
|
||||||
@@ -175,6 +155,6 @@ func testTools(code int, body string) (*httptest.Server, *Client) {
|
|||||||
fmt.Fprintln(w, body)
|
fmt.Fprintln(w, body)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
client := &Client{URL: server.URL, TimeOut: time.Second * 2, Secret: "developerSecret"}
|
client := &Client{URL: server.URL, Secret: "developerSecret", httpCli: &http.Client{Timeout: 2 * time.Second}}
|
||||||
return server, client
|
return server, client
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user