Capstone/service/vendors/pco/pco.go

64 lines
1.0 KiB
Go
Raw Normal View History

package pco
import (
"context"
"net/http"
"net/url"
"golang.org/x/oauth2"
)
const PCO_API_URL = "https://api.planningcenteronline.com"
type PcoApiClient struct {
oauth *oauth2.Config
2023-11-16 13:00:28 -05:00
tokenSource oauth2.TokenSource
client *http.Client
url *url.URL
}
func NewClient() *PcoApiClient {
pco_url, err := url.Parse(PCO_API_URL)
if err != nil {
panic(err)
}
pco := &PcoApiClient{
oauth: &oauth2.Config{},
url: pco_url,
}
return pco
}
2023-11-16 13:00:28 -05:00
func NewClientWithOauthConfig(conf *oauth2.Config, tokenSource oauth2.TokenSource) *PcoApiClient {
pco_url, err := url.Parse(PCO_API_URL)
if err != nil {
panic(err)
}
pco := &PcoApiClient{
oauth: conf,
2023-11-16 13:00:28 -05:00
tokenSource: tokenSource,
url: pco_url,
}
return pco
}
func (api *PcoApiClient) getClient() *http.Client {
if api.client == nil {
2023-11-16 13:00:28 -05:00
api.client = oauth2.NewClient(context.Background(), api.tokenSource)
}
return api.client
}
func (api *PcoApiClient) Url() *url.URL {
return api.url
}
func (api *PcoApiClient) Do(req *http.Request) (*http.Response, error) {
return api.getClient().Do(req)
}