Support custom http client for GAE

This commit is contained in:
Junpei Tsuji
2018-05-17 18:16:00 +09:00
parent f2bcd8eb15
commit 1dbebe0623
2 changed files with 28 additions and 26 deletions

View File

@@ -9,24 +9,12 @@ import (
"encoding/base64"
"fmt"
"net/http"
"time"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
androidpublisher "google.golang.org/api/androidpublisher/v2"
)
const (
defaultTimeout = time.Second * 5
)
var timeout = defaultTimeout
// SetTimeout sets dial timeout duration
func SetTimeout(t time.Duration) {
timeout = t
}
// The IABClient type is an interface to verify purchase token
type IABClient interface {
VerifySubscription(context.Context, string, string, string) (*androidpublisher.SubscriptionPurchase, error)
@@ -44,14 +32,24 @@ type Client struct {
// 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(oauth2.NoContext, oauth2.HTTPClient, &http.Client{
Timeout: timeout,
})
func New(jsonKey []byte) (*Client, error) {
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, http.DefaultClient)
conf, err := google.JWTConfigFromJSON(jsonKey, androidpublisher.AndroidpublisherScope)
return Client{conf.Client(ctx)}, err
return &Client{conf.Client(ctx)}, err
}
// NewWithClient returns http client which includes the custom http client.
func NewWithClient(jsonKey []byte, cli *http.Client) (*Client, error) {
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, cli)
conf, err := google.JWTConfigFromJSON(jsonKey, androidpublisher.AndroidpublisherScope)
if err != nil {
return nil, err
}
return &Client{conf.Client(ctx)}, err
}
// VerifySubscription verifies subscription status