forked from Opteo/google-ads-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
112 lines (95 loc) · 3.57 KB
/
client.ts
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
112
import Bottleneck from 'bottleneck'
import crypto from 'crypto'
import { noop } from 'lodash'
import Customer, { CustomerInstance } from './customer'
import GrpcClient, { GoogleAdsNodeOptions } from './grpc'
import { normaliseCustomerId } from './utils'
import { ClientOptions, CustomerOptions } from './types'
import AccessibleCustomersService from './services/accessible_customers'
interface ListAccessibleCustomersOptions extends GoogleAdsNodeOptions {
refresh_token: string
}
export default class GoogleAdsApi {
private readonly options: ClientOptions
private throttler: Bottleneck
constructor(options: ClientOptions) {
this.options = options
const throttler_options = {
minTime: 10, // roughly 100 requests per second
id:
'id' +
crypto
.createHash('md5')
.update(this.options.developer_token)
.digest('hex'), // don't want to leak dev token to redis
/* Clustering options */
datastore: this.options.redis_options ? 'redis' : 'local',
clearDatastore: false,
clientOptions: this.options.redis_options,
timeout: 1000 * 60 * 10,
}
this.throttler = new Bottleneck(throttler_options)
this.throttler.on('error', err => {
console.error('Could not connect to redis: ')
console.error(err)
})
}
public Customer({
customer_account_id,
refresh_token,
login_customer_id,
linked_customer_id,
pre_report_hook,
post_report_hook,
prevent_mutations,
logging,
}: CustomerOptions): CustomerInstance {
if (!customer_account_id || !refresh_token) {
throw new Error('Must specify {customer_account_id, refresh_token}')
}
pre_report_hook = pre_report_hook || noop
post_report_hook = post_report_hook || noop
customer_account_id = normaliseCustomerId(customer_account_id)
login_customer_id = normaliseCustomerId(login_customer_id)
linked_customer_id = normaliseCustomerId(linked_customer_id)
const gads_node_options = {
prevent_mutations,
logging,
}
const client = new GrpcClient(
this.options.developer_token,
this.options.client_id,
this.options.client_secret,
refresh_token as string,
login_customer_id,
linked_customer_id,
gads_node_options
)
return Customer(customer_account_id, client, this.throttler, pre_report_hook, post_report_hook)
}
public async listAccessibleCustomers({
refresh_token,
prevent_mutations,
logging,
}: ListAccessibleCustomersOptions): Promise<Array<string>> {
if (!refresh_token) {
throw new Error('Must specify {refresh_token}')
}
const gads_node_options = {
prevent_mutations,
logging,
}
const client = new GrpcClient(
this.options.developer_token,
this.options.client_id,
this.options.client_secret,
refresh_token as string,
'', // login-customer-id not needed for this function
'', // linked-customer-id not needed for this function
gads_node_options
)
const cusService = new AccessibleCustomersService(client, this.throttler, 'CustomerService')
const { resource_names } = await cusService.listAccessibleCustomers()
return resource_names
}
}