From f678a738b44ac27a6cc3bb761feeabf889f1d7d8 Mon Sep 17 00:00:00 2001 From: Preston Baxter Date: Thu, 16 Nov 2023 12:00:28 -0600 Subject: [PATCH] B: PCO Client now takes token source --- service/controllers/controllers.go | 3 +++ service/controllers/pco_webhook.go | 38 +++++++++++++++++++++++++++++- service/vendors/pco/pco.go | 9 ++++--- tailwind-starter-kit | 1 + 4 files changed, 45 insertions(+), 6 deletions(-) create mode 160000 tailwind-starter-kit diff --git a/service/controllers/controllers.go b/service/controllers/controllers.go index 7ea690a..5897a57 100644 --- a/service/controllers/controllers.go +++ b/service/controllers/controllers.go @@ -3,6 +3,7 @@ package controllers import ( "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config" "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db" + "git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson/primitive" @@ -13,6 +14,7 @@ var ( log *logrus.Logger mongo *db.DB ytClientMap map[primitive.ObjectID]*youtube.Service + pcoClientMap map[primitive.ObjectID]*pco.PcoApiClient ) func BuildRouter(r *gin.Engine) { @@ -30,6 +32,7 @@ func BuildRouter(r *gin.Engine) { } ytClientMap = make(map[primitive.ObjectID]*youtube.Service) + pcoClientMap = make(map[primitive.ObjectID]*pco.PcoApiClient) pco := r.Group("/pco") pco.Use(ValidatePcoWebhook) diff --git a/service/controllers/pco_webhook.go b/service/controllers/pco_webhook.go index ea89369..e4f96f9 100644 --- a/service/controllers/pco_webhook.go +++ b/service/controllers/pco_webhook.go @@ -7,7 +7,10 @@ import ( "regexp" "sync" + "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config" "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models" + "git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco" + "git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/services" "git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/webhooks" "github.com/gin-gonic/gin" "github.com/google/jsonapi" @@ -98,7 +101,32 @@ func eventMatch(event string) bool { } } +func pcoServiceForUser(userId primitive.ObjectID) (*pco.PcoApiClient, error) { + //add youtube client to map if its not there + if client, ok := pcoClientMap[userId]; !ok { + pcoAccount, err := mongo.FindVendorAccountByUser(userId, models.PCO_VENDOR_NAME) + if err != nil { + return nil, err + } + + //Build our fancy token source + tokenSource := oauth2.ReuseTokenSource(pcoAccount.Token(), mongo.NewVendorTokenSource(pcoAccount)) + + //init service + conf := config.Config() + client := pco.NewClientWithOauthConfig(conf.Vendors[models.PCO_VENDOR_NAME].OauthConfig(), tokenSource) + + //add user to map + pcoClientMap[userId] = client + + return client, nil + } else { + return client, nil + } +} + func youtubeServiceForUser(userId primitive.ObjectID) (*youtube.Service, error) { + //add youtube client to map if its not there if client, ok := ytClientMap[userId]; !ok { ytAccount, err := mongo.FindVendorAccountByUser(userId, models.YOUTUBE_VENDOR_NAME) if err != nil { @@ -135,11 +163,19 @@ func ScheduleLiveStreamFromWebhook(c *gin.Context, body *webhooks.EventDelivery) } } - client, err := youtubeServiceForUser(uid) + ytClient, err := youtubeServiceForUser(uid) if err != nil { log.WithError(err).Error("Failed to initialize youtube client") return err } + pcoClient, err := pcoServiceForUser(uid) + if err != nil { + log.WithError(err).Error("Failed to initialize youtube client") + return err + } + + planUpdate := &services.Plan{} + return nil } diff --git a/service/vendors/pco/pco.go b/service/vendors/pco/pco.go index 1c941fa..12cc70a 100644 --- a/service/vendors/pco/pco.go +++ b/service/vendors/pco/pco.go @@ -12,7 +12,7 @@ const PCO_API_URL = "https://api.planningcenteronline.com" type PcoApiClient struct { oauth *oauth2.Config - token *oauth2.Token + tokenSource oauth2.TokenSource client *http.Client url *url.URL } @@ -25,14 +25,13 @@ func NewClient() *PcoApiClient { pco := &PcoApiClient{ oauth: &oauth2.Config{}, - token: &oauth2.Token{}, url: pco_url, } return pco } -func NewClientWithOauthConfig(conf *oauth2.Config, token *oauth2.Token) *PcoApiClient { +func NewClientWithOauthConfig(conf *oauth2.Config, tokenSource oauth2.TokenSource) *PcoApiClient { pco_url, err := url.Parse(PCO_API_URL) if err != nil { panic(err) @@ -40,7 +39,7 @@ func NewClientWithOauthConfig(conf *oauth2.Config, token *oauth2.Token) *PcoApiC pco := &PcoApiClient{ oauth: conf, - token: token, + tokenSource: tokenSource, url: pco_url, } @@ -49,7 +48,7 @@ func NewClientWithOauthConfig(conf *oauth2.Config, token *oauth2.Token) *PcoApiC func (api *PcoApiClient) getClient() *http.Client { if api.client == nil { - api.client = api.oauth.Client(context.Background(), api.token) + api.client = oauth2.NewClient(context.Background(), api.tokenSource) } return api.client diff --git a/tailwind-starter-kit b/tailwind-starter-kit new file mode 160000 index 0000000..830f926 --- /dev/null +++ b/tailwind-starter-kit @@ -0,0 +1 @@ +Subproject commit 830f9267ad556745e6fd7857260c020dcd9dacea