2023-11-07 22:34:57 -05:00
|
|
|
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
|
2023-11-07 22:34:57 -05:00
|
|
|
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 {
|
2023-11-07 22:34:57 -05:00
|
|
|
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,
|
2023-11-07 22:34:57 -05:00
|
|
|
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)
|
2023-11-07 22:34:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|