chargebee / chargebee-go

Go library for the Chargebee API.
https://apidocs.chargebee.com/docs/api?lang=go
MIT License
24 stars 23 forks source link

Support arbitrary URLs and port numbers #3

Open sfro opened 5 years ago

sfro commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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",
    }