Mercurial > code > home > repos > gcalendarwatch
annotate calsync/gcalclient/gcalclient.go @ 91:62caecb41dfd default tip
fix tag
author | drewp@bigasterisk.com |
---|---|
date | Fri, 03 Jan 2025 18:06:13 -0800 |
parents | 79320eff10f2 |
children |
rev | line source |
---|---|
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
1 package gcalclient |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
2 |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
3 /* |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
4 Note: this module keeps gcal *paging* private (we fetch all the pages), but |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
5 *sync* is public. At least, callers may receive a syncToken and have to pass it |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
6 back in. |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
7 */ |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
8 import ( |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
9 "context" |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
10 "crypto/md5" |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
11 "fmt" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
12 "net/url" |
61
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
13 "time" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
14 |
61
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
15 "bigasterisk.com/go/gcalendarwatch/mongoclienttypes" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
16 "google.golang.org/api/calendar/v3" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
17 ) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
18 |
79
30ad34850ef1
redo url structure (still not the same as the old version)
drewp@bigasterisk.com
parents:
73
diff
changeset
|
19 const calUrlBase = "http://bigasterisk.com/calendar/" |
30ad34850ef1
redo url structure (still not the same as the old version)
drewp@bigasterisk.com
parents:
73
diff
changeset
|
20 const eventUrlBase = "http://bigasterisk.com/calendarEvent/" |
54
3a12a3ac9164
refactor. run 30s forever. doesn't work on 2+ cals
drewp@bigasterisk.com
parents:
53
diff
changeset
|
21 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
22 type GCalClient struct { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
23 ctx context.Context |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
24 srv *calendar.Service |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
25 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
26 |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
27 // Same as calendar.Event, but includes our urls |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
28 type CalendarEvent struct { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
29 *calendar.Event |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
30 CalendarUrl string |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
31 EventUrl string |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
32 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
33 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
34 func MakeCalUrl(calId string) string { |
79
30ad34850ef1
redo url structure (still not the same as the old version)
drewp@bigasterisk.com
parents:
73
diff
changeset
|
35 return calUrlBase + url.QueryEscape(calId) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
36 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
37 |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
38 func MakeEventUrl(calUrl string, evId string) string { |
79
30ad34850ef1
redo url structure (still not the same as the old version)
drewp@bigasterisk.com
parents:
73
diff
changeset
|
39 return eventUrlBase + url.QueryEscape(evId) |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
40 } |
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
41 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
42 func New(ctx context.Context) (*GCalClient, error) { |
58 | 43 srv, err := newService(ctx) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
44 if err != nil { |
73
50ddf6a48816
gcalclient should return errors, not exit
drewp@bigasterisk.com
parents:
71
diff
changeset
|
45 return nil, err |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
46 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
47 return &GCalClient{ctx, srv}, nil |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
48 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
49 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
50 func (gc *GCalClient) Close() { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
51 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
52 |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
53 func (gc *GCalClient) AllCalendars() ([]*calendar.CalendarListEntry, error) { |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
54 // todo: pagination |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
55 list, err := gc.srv.CalendarList.List().MaxResults( /*maxResults*/ 100).Do() |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
56 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
57 return nil, err |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
58 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
59 return list.Items, nil |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
60 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
61 |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
62 func shortDebugHash(pageToken string) string { |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
63 if pageToken == "" { |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
64 return "(empty)" |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
65 } |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
66 return fmt.Sprintf("%x", md5.Sum([]byte(pageToken))) |
52 | 67 } |
61
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
68 |
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
69 func (gc *GCalClient) WatchEvents(cal *mongoclienttypes.MongoCal, watchId string, expire time.Duration) error { |
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
70 _, err := watchEventsCall(gc.srv, cal.GoogleId, expire, watchId).Do() |
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
71 return err |
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
72 } |