forked from kritzware/google-ads-go
/
ads.go
111 lines (94 loc) · 2.77 KB
/
ads.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package ads
import (
"encoding/json"
"io/ioutil"
"github.com/AleksanderMako/google-ads-go/auth"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"google.golang.org/grpc"
)
const (
defaultVersion string = "v0"
)
type GoogleAdsClient struct {
credentials *oauth2.Config
token *oauth2.Token
conn *grpc.ClientConn
developerToken string
loginCustomerID string
ctx context.Context
}
type GoogleAdsClientParams struct {
ClientID string
ClientSecret string
DeveloperToken string
RefreshToken string
LoginCustomerID string
}
type googleAdsStorageParams struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RefreshToken string `json:"refresh_token"`
DeveloperToken string `json:"developer_token"`
LoginCustomerID string `json:"login_customer_id",omitempty`
}
// NewClient creates a new client with specified credential params
func NewClient(params *GoogleAdsClientParams) (*GoogleAdsClient, error) {
credentials := auth.NewCredentials(params.ClientID, params.ClientSecret)
initialToken := auth.NewPartialToken(params.RefreshToken)
c := &GoogleAdsClient{
credentials: credentials,
token: initialToken,
developerToken: params.DeveloperToken,
loginCustomerID: params.LoginCustomerID,
}
newToken, err := auth.RefreshToken(c.credentials, c.token)
if err != nil {
return nil, err
}
c.token = newToken
conn, ctx, err := auth.NewGrpcConnection(c.token, c.developerToken, c.loginCustomerID)
if err != nil {
return nil, err
}
c.conn = conn
c.ctx = ctx
return c, nil
}
// NewClientFromStorage creates a new client instance from specified "google-ads.json" file
func NewClientFromStorage(filepath string) (*GoogleAdsClient, error) {
params, err := ReadCredentialsFile(filepath)
if err != nil {
return nil, err
}
client, err := NewClient(params)
return client, err
}
// ReadCredentialsFile reads a credentials JSON file and returns the exported config
func ReadCredentialsFile(filepath string) (*GoogleAdsClientParams, error) {
file, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
var g googleAdsStorageParams
json.Unmarshal(file, &g)
return &GoogleAdsClientParams{
ClientID: g.ClientID,
ClientSecret: g.ClientSecret,
RefreshToken: g.RefreshToken,
DeveloperToken: g.DeveloperToken,
LoginCustomerID: g.LoginCustomerID,
}, nil
}
// Conn returns a pointer to the clients gRPC connection
func (g *GoogleAdsClient) Conn() *grpc.ClientConn {
return g.conn
}
// Context returns the context of the client
func (g *GoogleAdsClient) Context() context.Context {
return g.ctx
}
// TokenIsValid returns a bool indicating if the generated access token is valid
func (g *GoogleAdsClient) TokenIsValid() bool {
return g.token.Valid()
}