From e13d7f53ff748b5510752b9491eab9b5fbf460ec Mon Sep 17 00:00:00 2001 From: Ruidy Date: Mon, 5 Aug 2024 13:55:16 +0200 Subject: [PATCH] poc calendar service --- internal/calendar/service.go | 113 +++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 internal/calendar/service.go diff --git a/internal/calendar/service.go b/internal/calendar/service.go new file mode 100644 index 0000000..130a02c --- /dev/null +++ b/internal/calendar/service.go @@ -0,0 +1,113 @@ +package calendar + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "log" + "net/http" + "os" + "time" + + "golang.org/x/oauth2" + "golang.org/x/oauth2/google" + "google.golang.org/api/calendar/v3" + "google.golang.org/api/option" +) + +const tokFile = "token.json" + +type Service struct { + *calendar.Service +} + +func NewService(ctx context.Context, credJson string) *Service { + b := []byte(credJson) + + config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope) + if err != nil { + log.Fatalf("Unable to parse client secret file to config: %v", err) + } + client := getClient(ctx, config) + + srv, err := calendar.NewService(ctx, option.WithHTTPClient(client)) + if err != nil { + log.Fatalf("Unable to retrieve Calendar client: %v", err) + } + + return &Service{srv} +} + +// TODO: implement create event, list events in a period, delete event + +// TODO: Delete this. It is just for Proof of Concept +func (s Service) ListNextEvents() (*calendar.Events, error) { + t := time.Now().Format(time.RFC3339) + events, err := s.Events.List("primary").ShowDeleted(false). + SingleEvents(true).TimeMin(t).MaxResults(10).OrderBy("startTime").Do() + if err != nil { + return nil, fmt.Errorf("unable to retrieve next ten of the user's events: %w", err) + } + if len(events.Items) == 0 { + return nil, errors.New("no upcoming events found") + } else { + // TODO: return a custom type + return events, nil + } +} + +// Retrieve a token, saves the token, then returns the generated client. +func getClient(ctx context.Context, config *oauth2.Config) *http.Client { + // The file token.json stores the user's access and refresh tokens, and is + // created automatically when the authorization flow completes for the first + // time. + tok, err := tokenFromFile(tokFile) + if err != nil { + tok = getTokenFromWeb(config) + saveToken(tokFile, tok) + } + return config.Client(ctx, tok) +} + +// Request a token from the web, then returns the retrieved token. +func getTokenFromWeb(config *oauth2.Config) *oauth2.Token { + authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline) + fmt.Printf("Click on the following link to open in your browser \n%v\n", authURL) + + // TODO: write a callback endpoint that will parse the auth code from the URL + var authCode string + fmt.Print("Type the authorization code: \n> ") + if _, err := fmt.Scan(&authCode); err != nil { + log.Fatalf("Unable to read authorization code: %v", err) + } + + tok, err := config.Exchange(context.TODO(), authCode) + if err != nil { + log.Fatalf("Unable to retrieve token from web: %v", err) + } + return tok +} + +// Retrieves a token from a local file. +func tokenFromFile(file string) (*oauth2.Token, error) { + f, err := os.Open(file) + if err != nil { + return nil, err + } + defer f.Close() + tok := &oauth2.Token{} + err = json.NewDecoder(f).Decode(tok) + return tok, err +} + +// Saves a token to a file path. +func saveToken(path string, token *oauth2.Token) { + fmt.Printf("Saving credential file to: %s\n", path) + f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + log.Fatalf("Unable to cache oauth token: %v", err) + } + defer f.Close() + json.NewEncoder(f).Encode(token) +}