Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support arbitrary URLs and port numbers #3

Open
sfro opened this issue Feb 28, 2019 · 2 comments
Open

Support arbitrary URLs and port numbers #3

sfro opened this issue Feb 28, 2019 · 2 comments
Assignees

Comments

@sfro
Copy link

sfro commented Feb 28, 2019

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
}

@cb-goutham
Copy link
Contributor

@sfro Hi ,
You can initialise the environment struct to define an arbitrary url and port number in the following way :

env := chargebee.Environment{
		Key:             "api-key",
		SiteName:        "site-name",
		ChargebeeDomain: "arbitrary-url.in:8080",
		Protocol:        "http",
	} 

And you can pass the above env along with your request as below :

res,err := subscriptionAction.Retrieve("__test__5SK0bLNFRFuFEQ3d7").RequestWithEnv(env)
	if err != nil {
		fmt.Println(err)
	}else{
		Subscription := res.Subscription
		fmt.Println(Subscription.Id)

	}

@sfro
Copy link
Author

sfro commented Mar 5, 2019

Yeah, I ended up doing something like that, it just feels a little hacky to have to declare an url with a "." in it and have the port number in the Chargebee domain field. It works, I just don't like it very much 😄 so for localhost it would look like below:

	env := chargebee.Environment{
		Key:             "api-key",
		SiteName:        "127.0",
		ChargebeeDomain: "0.1:8080",
		Protocol:        "http",
	}

cb-navaneedhan added a commit that referenced this issue Sep 29, 2020
Adding core files to the typescript client library: SUBSENGG-6138
@cb-rakesh cb-rakesh self-assigned this Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants