-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
63 lines (51 loc) · 1.5 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
package calendarmod
import (
"log"
"net/http"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/calendar/v3"
)
// Client based on Service Account to connect to Google API
type CalendarClient struct {
ctx context.Context
config *jwt.Config
httpClient *http.Client
}
// Get Context
func (c *CalendarClient) Context() context.Context {
return c.ctx
}
// Get Config
func (c *CalendarClient) Config() *jwt.Config {
return c.config
}
// Get HttpClient
func (c *CalendarClient) HttpClient() *http.Client {
return c.httpClient
}
// Initialize authentification client with service account to access Google API
//
// @return {*CalendarClient}
//
// @param {bool} useCalendar- true if need to use Google Calendar API, should always be true to use subscription service
func SetUpSVAClient(serviceAccountJSON []byte, useCalendar bool) *CalendarClient {
// This is a variable needed for all http actions with the google API
ctx := context.Background()
var scope []string
if useCalendar {
scope = append(scope, calendar.CalendarScope)
}
config, err := google.JWTConfigFromJSON(serviceAccountJSON, scope...)
if err != nil {
log.Printf("Could not create config for service account=> {%s}", err)
return nil
}
client := config.Client(oauth2.NoContext)
// initilize authentification client
calendarclient := &CalendarClient{ctx: ctx, config: config, httpClient: client}
log.Println("Client sets up")
return calendarclient
}