-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
152 lines (127 loc) · 3.4 KB
/
client.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package realtime
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"time"
"github.com/google/go-querystring/query"
)
const (
maxErrMsgLen = 256
defaultTimeout = 2 * time.Second
)
var (
defaultBaseURL = func() *url.URL {
u, err := url.Parse("https://www.googleapis.com/analytics/v3/data/")
if err != nil {
panic(err)
}
return u
}()
)
// Response stores the google analytics response.
type Response struct {
TotalsForAllResults *ResponseTotals `json:"totalsForAllResults,omitempty"`
}
// ResponseTotals stores the total active users response.
type ResponseTotals struct {
RtActiveUsers *string `json:"rt:activeUsers,omitempty"`
}
// Options specifies the parameters required to make a valid request.
type Options struct {
IDs string `url:"ids"`
Metrics string `url:"metrics"`
}
// ClientOption modifies the default behavior of a realtime client.
type ClientOption func(*Client)
// WithHTTPClient makes the realtime client use the given HTTP client.
func WithHTTPClient(client *http.Client) ClientOption {
return func(c *Client) { c.client = client }
}
// Client to interact with the realtime API
type Client struct {
client *http.Client
baseURL *url.URL
}
// NewClient creates a client for the realtime API.
func NewClient(opts ...ClientOption) *Client {
client := &Client{
client: &http.Client{Timeout: defaultTimeout},
baseURL: defaultBaseURL,
}
for _, opt := range opts {
opt(client)
}
return client
}
// GetRealTime returns the realtime analytics api.
// https://developers.google.com/apis-explorer/#p/analytics/v3/analytics.data.realtime.get
func (c *Client) GetRealTime(opt *Options) (*Response, error) {
url, errOptions := addOptions("realtime", opt)
if errOptions != nil {
return nil, errOptions
}
response := new(Response)
err := c.doGet(url, func(r io.Reader) error {
return json.NewDecoder(r).Decode(response)
})
return response, err
}
func (c *Client) doGet(resource string, decoder func(r io.Reader) error) error {
return c.doGetURL(c.resolve(resource), decoder)
}
func (c *Client) doGetURL(url string, decoder func(r io.Reader) error) error {
resp, err := c.client.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return c.makeError(resp)
}
return decoder(resp.Body)
}
func (c *Client) makeError(resp *http.Response) error {
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, maxErrMsgLen))
if err != nil {
return err
}
if len(body) >= maxErrMsgLen {
body = append(body[:maxErrMsgLen], []byte("... (elided)")...)
} else if len(body) == 0 {
body = []byte(resp.Status)
}
return fmt.Errorf("unexpected response from realtime API, status %d: %s",
resp.StatusCode, string(body))
}
func (c *Client) resolve(urlStr string) string {
rel, err := url.Parse(urlStr)
if err != nil {
return ""
}
u := *c.baseURL.ResolveReference(rel)
return u.String()
}
// addOptions adds the parameters in opt as URL query parameters to s.
// opt must be a struct whose fields may contain "url" tags.
// all parameters passed as s will be replaced by options in opt.
func addOptions(s string, opt interface{}) (string, error) {
v := reflect.ValueOf(opt)
if v.Kind() == reflect.Ptr && v.IsNil() {
return s, nil
}
u, err := url.Parse(s)
if err != nil {
return s, err
}
qs, err := query.Values(opt)
if err != nil {
return s, err
}
u.RawQuery = qs.Encode()
return u.String(), nil
}