-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
88 lines (67 loc) · 1.86 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
package client
import (
"fmt"
"net/http"
"github.com/dghubble/sling"
)
type Client struct {
// HTTP client used to communicate with the OM API.
client *http.Client
// Base URL for API requests.
baseURL string
// User agent for client
userAgent string
// Auth token - passed to org manager as:
// Authorization: bearer <access_token>
token string
// the services this client can work with
Version *VersionService
Events *EventsService
Venues *VenuesService
}
type service struct {
sling *sling.Sling
}
func New(optionFuncs ...optionFunc) *Client {
client := &Client{
client: http.DefaultClient,
baseURL: defaultBaseURL,
userAgent: defaultUserAgent,
}
for _, option := range optionFuncs {
option(client)
}
s := sling.New().Client(client.client).Base(client.baseURL).Set("Accept", "application/json").Set("User-agent", client.userAgent)
if client.token != "" {
s = s.Set("Authorization", "Bearer "+client.token)
}
client.Version = &VersionService{s.New().Path("version")}
client.Events = &EventsService{s.New().Path("v1/events/")}
client.Venues = &VenuesService{s.New().Path("v1/venues/")}
return client
}
type optionFunc func(*Client)
// SetAuthToken is a client option for setting the auth token used accessing Org Manager
func SetAuthToken(token string) optionFunc {
return func(c *Client) {
c.token = token
}
}
// SetHttpClient is a client option for setting the *http.Client
func SetHttpClient(httpClient *http.Client) optionFunc {
return func(c *Client) {
c.client = httpClient
}
}
// SetBaseURL is a client option for setting the base url
func SetBaseURL(baseURL string) optionFunc {
return func(c *Client) {
c.baseURL = baseURL
}
}
// SetUserAgent is a client option for setting the user agent.
func SetUserAgent(ua string) optionFunc {
return func(c *Client) {
c.userAgent = fmt.Sprintf("%s %s", ua, c.userAgent)
}
}