-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
92 lines (78 loc) · 1.91 KB
/
report.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
package awql
import (
"encoding/csv"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// Report download URL.
const reportURL = "https://adwords.google.com/api/adwords/reportdownload/" + version
type reportQuery struct {
fields []string
res *http.Response
next []string
reader *csv.Reader
}
var _ Rows = &reportQuery{}
// Create report query request.
func newReportQuery(c *Client, q string) (*reportQuery, error) {
req, err := http.NewRequest(
"POST", reportURL,
strings.NewReader(url.Values{"__rdquery": {q}, "__fmt": {"CSV"}}.Encode()),
)
if err != nil {
return nil, err
}
// https://developers.google.com/adwords/api/docs/guides/reporting#request_headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; param=value")
req.Header.Add("Accept", "*/*")
req.Header.Add("clientCustomerId", c.auth.CustomerId)
req.Header.Add("developerToken", c.auth.DeveloperToken)
req.Header.Add("skipReportHeader", "true")
req.Header.Add("skipReportSummary", "true")
req.Header.Add("skipColumnHeader", "true")
res, err := c.client.Do(req)
if err != nil || res.StatusCode != 200 {
if err == nil && res.StatusCode != 200 {
defer res.Body.Close()
var b []byte
b, err = ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
err = errors.New(string(b))
}
return nil, err
}
return &reportQuery{
fields: getFields(q),
res: res,
reader: csv.NewReader(res.Body),
}, nil
}
// Iterator for query results.
func (r *reportQuery) Next() bool {
var err error
r.next, err = r.reader.Read()
if err != nil {
return false
}
return true
}
// Get a row.
func (r *reportQuery) Scan() (map[string]interface{}, error) {
if len(r.next) == 0 {
return nil, ErrNoRows
}
m := make(map[string]interface{})
for i := range r.fields {
m[r.fields[i]] = r.next[i]
}
return m, nil
}
// Close response body properly.
func (r *reportQuery) Close() {
r.res.Body.Close()
}