chargebee-go icon indicating copy to clipboard operation
chargebee-go copied to clipboard

Support arbitrary URLs and port numbers

Open sfro opened this issue 6 years ago • 2 comments

Hi!

I would like to be able to use this client to make requests to an URL that does not follow the convention defined here: https://github.com/chargebee/chargebee-go/blob/master/environment.go#L39

I would like to be able to define an arbitrary url as well as a port number for test purposes. Would it be possible to do something like this instead?

package chargebee

import (
	"errors"
	"fmt"
	"time"
)

type Environment struct {
	Key             string
	SiteName        string
	ChargebeeDomain string
	Protocol        string
	BaseURL         string
}

var (
	DefaultHTTPTimeout    = 80 * time.Second
	ExportWaitInSecs      = 3 * time.Second
	TimeMachineWaitInSecs = 3 * time.Second
	DefaultEnv            Environment
)

const (
	APIVersion = "v2"
	Charset    = "UTF-8"
)

// Keep for backwards compatibility
func Configure(key, siteName string) {
	if key == "" || siteName == "" {
		panic(errors.New("Key/siteName cannot be empty"))
	}
	DefaultEnv = Environment{Key: key, BaseURL: fmt.Sprintf("https://%v.chargebee.com/api/%v", siteName, APIVersion)}
}

func ConfigureWithBaseURL(key, protocol, baseURL, port string) {
	if key == "" || baseURL == "" {
		panic(errors.New("Key/URL cannot be empty"))
	}
	if protocol == "" {
		protocol = "https"
	}
	if port == "" {
		DefaultEnv = Environment{Key: key, BaseURL: fmt.Sprintf("%v://%v/api/%v", protocol, baseURL, APIVersion)}
	} else {
		DefaultEnv = Environment{Key: key, BaseURL: fmt.Sprintf("%v://%v:%v/api/%v", protocol, baseURL, port, APIVersion)}
	}
}

func (env *Environment) apiBaseUrl() string {
	return env.BaseURL
}

func DefaultConfig() Environment {
	if DefaultEnv == (Environment{}) {
		panic(errors.New("The default environment has not been configured"))
	}
	return DefaultEnv
}

sfro avatar Feb 28 '19 13:02 sfro