Added client timeout because http.DefaultClient doesn't have timeout
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
@@ -26,14 +27,14 @@ type IABClient interface {
|
||||
|
||||
// The Client type implements VerifySubscription method
|
||||
type Client struct {
|
||||
httpClient *http.Client
|
||||
httpCli *http.Client
|
||||
}
|
||||
|
||||
// New returns http client which includes the credentials to access androidpublisher API.
|
||||
// You should create a service account for your project at
|
||||
// https://console.developers.google.com and download a JSON key file to set this argument.
|
||||
func New(jsonKey []byte) (*Client, error) {
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, http.DefaultClient)
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, &http.Client{Timeout: 10 * time.Second})
|
||||
|
||||
conf, err := google.JWTConfigFromJSON(jsonKey, androidpublisher.AndroidpublisherScope)
|
||||
|
||||
@@ -59,7 +60,7 @@ func (c *Client) VerifySubscription(
|
||||
subscriptionID string,
|
||||
token string,
|
||||
) (*androidpublisher.SubscriptionPurchase, error) {
|
||||
service, err := androidpublisher.New(c.httpClient)
|
||||
service, err := androidpublisher.New(c.httpCli)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -77,7 +78,7 @@ func (c *Client) VerifyProduct(
|
||||
productID string,
|
||||
token string,
|
||||
) (*androidpublisher.ProductPurchase, error) {
|
||||
service, err := androidpublisher.New(c.httpClient)
|
||||
service, err := androidpublisher.New(c.httpCli)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -90,7 +91,7 @@ func (c *Client) VerifyProduct(
|
||||
|
||||
// CancelSubscription cancels a user's subscription purchase.
|
||||
func (c *Client) CancelSubscription(ctx context.Context, packageName string, subscriptionID string, token string) error {
|
||||
service, err := androidpublisher.New(c.httpClient)
|
||||
service, err := androidpublisher.New(c.httpCli)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -104,7 +105,7 @@ func (c *Client) CancelSubscription(ctx context.Context, packageName string, sub
|
||||
// RefundSubscription refunds a user's subscription purchase, but the subscription remains valid
|
||||
// until its expiration time and it will continue to recur.
|
||||
func (c *Client) RefundSubscription(ctx context.Context, packageName string, subscriptionID string, token string) error {
|
||||
service, err := androidpublisher.New(c.httpClient)
|
||||
service, err := androidpublisher.New(c.httpCli)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -118,7 +119,7 @@ func (c *Client) RefundSubscription(ctx context.Context, packageName string, sub
|
||||
// RevokeSubscription refunds and immediately revokes a user's subscription purchase.
|
||||
// Access to the subscription will be terminated immediately and it will stop recurring.
|
||||
func (c *Client) RevokeSubscription(ctx context.Context, packageName string, subscriptionID string, token string) error {
|
||||
service, err := androidpublisher.New(c.httpClient)
|
||||
service, err := androidpublisher.New(c.httpCli)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func TestNew(t *testing.T) {
|
||||
expected := "oauth2: cannot fetch token: 401 Unauthorized\nResponse: {\n \"error\" : \"invalid_client\",\n \"error_description\" : \"The OAuth client was invalid.\"\n}"
|
||||
|
||||
actual, _ := New(dummyKey)
|
||||
val := actual.httpClient.Transport.(*oauth2.Transport)
|
||||
val := actual.httpCli.Transport.(*oauth2.Transport)
|
||||
token, err := val.Source.Token()
|
||||
if token != nil {
|
||||
t.Errorf("got %#v", token)
|
||||
@@ -48,7 +48,7 @@ func TestNew(t *testing.T) {
|
||||
|
||||
// TODO Normal scenario
|
||||
actual, _ = New(jsonKey)
|
||||
val = actual.httpClient.Transport.(*oauth2.Transport)
|
||||
val = actual.httpCli.Transport.(*oauth2.Transport)
|
||||
token, err = val.Source.Token()
|
||||
if err != nil {
|
||||
t.Errorf("got %#v", err)
|
||||
@@ -62,7 +62,7 @@ func TestNewWithClient(t *testing.T) {
|
||||
httpClient := urlfetch.Client(ctx)
|
||||
|
||||
cli, _ := NewWithClient(dummyKey, httpClient)
|
||||
tr, _ := cli.httpClient.Transport.(*oauth2.Transport)
|
||||
tr, _ := cli.httpCli.Transport.(*oauth2.Transport)
|
||||
|
||||
if !reflect.DeepEqual(tr.Base, httpClient.Transport) {
|
||||
t.Errorf("transport should be urlfetch's one")
|
||||
|
||||
Reference in New Issue
Block a user