Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Environment struct {
SiteName string
ChargebeeDomain string
Protocol string
HttpClient *http.Client
}

var (
Expand All @@ -32,9 +33,14 @@ func Configure(key string, siteName string) {
}
DefaultEnv = Environment{Key: key, SiteName: siteName}
}

func WithHTTPClient(c *http.Client) {
httpClient = c
// I don't really like setting this on the default environment, but maintains
// backwards compatibility at least while having the ability for client to be
// configured differently.
DefaultEnv.HttpClient = c
}

func (env *Environment) apiBaseUrl() string {
if env.Protocol == "" {
env.Protocol = "https"
Expand Down
27 changes: 24 additions & 3 deletions http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,32 @@ import (
"net/http"
)

var httpClient = &http.Client{Timeout: DefaultHTTPTimeout}
// RequestOption allows you to pass along functions that customize a
// RequestOptions struct.
type RequestOption func(*RequestOptions)

// HTTPOption contains a struct of various request options that could be
//customized.
type RequestOptions struct {
client *http.Client
}

// Client allows you to customize the http client on a per-request basis
func SetClient(hc *http.Client) RequestOption {
return func(h *RequestOptions) {
h.client = hc
}
}

//Do is used to execute an API Request.
func Do(req *http.Request) (string, error) {
response, err := httpClient.Do(req)
func Do(req *http.Request, opts ...RequestOption) (string, error) {
reqOpts := RequestOptions{
client: &http.Client{},
}
for _, o := range opts {
o(&reqOpts)
}
response, err := reqOpts.client.Do(req)
if err != nil {
return "", err
}
Expand Down
6 changes: 5 additions & 1 deletion list_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ func (request RequestObj) ListRequestWithEnv(env Environment) (*ResultList, erro
if err != nil {
panic(err)
}
res, err1 := Do(req)
opts := []RequestOption{}
if env.HttpClient != nil {
opts = append(opts, SetClient(env.HttpClient))
}
res, err1 := Do(req, opts...)
result := &ResultList{}
if err1 != nil {
return result, err1
Expand Down
6 changes: 5 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ func (request RequestObj) RequestWithEnv(env Environment) (*Result, error) {
if err != nil {
panic(err)
}
res, err1 := Do(req)
opts := []RequestOption{}
if env.HttpClient != nil {
opts = append(opts, SetClient(env.HttpClient))
}
res, err1 := Do(req, opts...)
result := &Result{}
if err1 != nil {
return result, err1
Expand Down