B: PCO Client now takes token source
This commit is contained in:
parent
61aacac37c
commit
f678a738b4
|
@ -3,6 +3,7 @@ package controllers
|
||||||
import (
|
import (
|
||||||
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config"
|
"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/frontend-service/db"
|
||||||
|
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
@ -13,6 +14,7 @@ var (
|
||||||
log *logrus.Logger
|
log *logrus.Logger
|
||||||
mongo *db.DB
|
mongo *db.DB
|
||||||
ytClientMap map[primitive.ObjectID]*youtube.Service
|
ytClientMap map[primitive.ObjectID]*youtube.Service
|
||||||
|
pcoClientMap map[primitive.ObjectID]*pco.PcoApiClient
|
||||||
)
|
)
|
||||||
|
|
||||||
func BuildRouter(r *gin.Engine) {
|
func BuildRouter(r *gin.Engine) {
|
||||||
|
@ -30,6 +32,7 @@ func BuildRouter(r *gin.Engine) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ytClientMap = make(map[primitive.ObjectID]*youtube.Service)
|
ytClientMap = make(map[primitive.ObjectID]*youtube.Service)
|
||||||
|
pcoClientMap = make(map[primitive.ObjectID]*pco.PcoApiClient)
|
||||||
|
|
||||||
pco := r.Group("/pco")
|
pco := r.Group("/pco")
|
||||||
pco.Use(ValidatePcoWebhook)
|
pco.Use(ValidatePcoWebhook)
|
||||||
|
|
|
@ -7,7 +7,10 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"sync"
|
"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/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"
|
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/webhooks"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/jsonapi"
|
"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) {
|
func youtubeServiceForUser(userId primitive.ObjectID) (*youtube.Service, error) {
|
||||||
|
//add youtube client to map if its not there
|
||||||
if client, ok := ytClientMap[userId]; !ok {
|
if client, ok := ytClientMap[userId]; !ok {
|
||||||
ytAccount, err := mongo.FindVendorAccountByUser(userId, models.YOUTUBE_VENDOR_NAME)
|
ytAccount, err := mongo.FindVendorAccountByUser(userId, models.YOUTUBE_VENDOR_NAME)
|
||||||
if err != nil {
|
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 {
|
if err != nil {
|
||||||
log.WithError(err).Error("Failed to initialize youtube client")
|
log.WithError(err).Error("Failed to initialize youtube client")
|
||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ const PCO_API_URL = "https://api.planningcenteronline.com"
|
||||||
|
|
||||||
type PcoApiClient struct {
|
type PcoApiClient struct {
|
||||||
oauth *oauth2.Config
|
oauth *oauth2.Config
|
||||||
token *oauth2.Token
|
tokenSource oauth2.TokenSource
|
||||||
client *http.Client
|
client *http.Client
|
||||||
url *url.URL
|
url *url.URL
|
||||||
}
|
}
|
||||||
|
@ -25,14 +25,13 @@ func NewClient() *PcoApiClient {
|
||||||
|
|
||||||
pco := &PcoApiClient{
|
pco := &PcoApiClient{
|
||||||
oauth: &oauth2.Config{},
|
oauth: &oauth2.Config{},
|
||||||
token: &oauth2.Token{},
|
|
||||||
url: pco_url,
|
url: pco_url,
|
||||||
}
|
}
|
||||||
|
|
||||||
return pco
|
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)
|
pco_url, err := url.Parse(PCO_API_URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -40,7 +39,7 @@ func NewClientWithOauthConfig(conf *oauth2.Config, token *oauth2.Token) *PcoApiC
|
||||||
|
|
||||||
pco := &PcoApiClient{
|
pco := &PcoApiClient{
|
||||||
oauth: conf,
|
oauth: conf,
|
||||||
token: token,
|
tokenSource: tokenSource,
|
||||||
url: pco_url,
|
url: pco_url,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +48,7 @@ func NewClientWithOauthConfig(conf *oauth2.Config, token *oauth2.Token) *PcoApiC
|
||||||
|
|
||||||
func (api *PcoApiClient) getClient() *http.Client {
|
func (api *PcoApiClient) getClient() *http.Client {
|
||||||
if api.client == nil {
|
if api.client == nil {
|
||||||
api.client = api.oauth.Client(context.Background(), api.token)
|
api.client = oauth2.NewClient(context.Background(), api.tokenSource)
|
||||||
}
|
}
|
||||||
|
|
||||||
return api.client
|
return api.client
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 830f9267ad556745e6fd7857260c020dcd9dacea
|
Loading…
Reference in New Issue