diff --git a/API-REFERENCE.md b/API-REFERENCE.md index a60c6dc..67b19bb 100644 --- a/API-REFERENCE.md +++ b/API-REFERENCE.md @@ -50,7 +50,8 @@ func main() { func NewCarboneSDK(SecretAccessToken ...string) (*CSDK, error) ``` Function to create a new instance of CSDK (CarboneSDK). -The access token can be pass as an argument to NewCarboneSDK (`args[0]`) or by the environment variable "CARBONE_TOKEN". +The access token can be pass as the first argument to NewCarboneSDK (`args[0]`) or by the environment variable "CARBONE_TOKEN". +The Api URL can be pass the second argument (`args[1]`) or by the environment variable "CARBONE_URL". To set a new environment variable, use the command: ```bash $ export CARBONE_TOKEN=your-secret-token @@ -65,6 +66,8 @@ Example csdk, err := carbone.NewCarboneSDK("YOUR-ACCESS-TOKEN") // Carbone access token passed as environment variable "Carbone TOKEN" csdk, err := carbone.NewCarboneSDK() +// Carbone access token passed as parameter with a custom API URL as second parameter +csdk, err := carbone.NewCarboneSDK("TOKEN", "https://test.carbone.io") ``` ### Render ```go diff --git a/CHANGELOG.md b/CHANGELOG.md index bb1550c..883aeaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### v1.2.0 + - The API key is not blocking the instantiation and is not required anymore as an environment variable nor as the first argument of the constructor. Nevertheless, A warning message is printed if the key is missing. + - A custom API url can be passed a second argument of the `NewCarboneSDK` function, or it can be passed as environment varible `CARBONE_URL` + ### v1.1.0 - Updated Carbone default API version to 4 - Increased default timeout to 60 seconds diff --git a/README.md b/README.md index e64187b..4144891 100644 --- a/README.md +++ b/README.md @@ -62,14 +62,6 @@ First, Go to the `carbone` package directory. ```bash $ cd carbone ``` -Then, create an environment variable `CARBONE_TOKEN` with the Carbone access token as value: -```bash -$ export CARBONE_TOKEN="YOUR_ACCESS_TOKEN" -``` -Check if it is set by running: -```bash -$ printenv | grep "CARBONE_TOKEN" -``` To run all the tests (-v for verbose output): ```bash $ go test -v diff --git a/carbone/carbone.go b/carbone/carbone.go index 894ff89..7b1faf5 100644 --- a/carbone/carbone.go +++ b/carbone/carbone.go @@ -46,17 +46,24 @@ type CSDK struct { // NewCarboneSDK is a constructor and return a new instance of CSDK func NewCarboneSDK(args ...string) (*CSDK, error) { + apiURL := os.Getenv("CARBONE_URL") apiAccessToken := os.Getenv("CARBONE_TOKEN") if len(args) > 0 && args[0] != "" { apiAccessToken = args[0] } if apiAccessToken == "" { - return nil, errors.New(`NewCarboneSDK error: "apiAccessToken" argument OR "CARBONE_TOKEN" env variable is missing`) + fmt.Println(`Carbone SDK Warning: Cloud API access token and "CARBONE_TOKEN" env variable are missing`) + } + if len(args) > 0 && args[1] != "" { + apiURL = args[1] + } + if apiURL == "" { + apiURL = "https://api.carbone.io" } csdk := &CSDK{ apiVersion: "4", apiAccessToken: apiAccessToken, - apiURL: "https://api.carbone.io", + apiURL: apiURL, apiTimeOut: time.Second * 60, apiHTTPClient: &http.Client{Timeout: time.Second * 60}, } diff --git a/carbone/carbone_test.go b/carbone/carbone_test.go index 76d741c..4d51e01 100644 --- a/carbone/carbone_test.go +++ b/carbone/carbone_test.go @@ -25,6 +25,56 @@ func TestMain(m *testing.M) { os.Exit(status) } +func TestDefaultConfig (t *testing.T) { + t.Run("Should get a default config", func (t *testing.T) { + if csdk.apiURL != "https://api.carbone.io" { + t.Error(errors.New("Default URL not valid")) + } + if csdk.apiVersion != "4" { + t.Error(errors.New("Default URL not valid")) + } + if csdk.apiAccessToken != "" { + t.Error(errors.New("Api Key not empty")) + } + }) +} + +func TestOnPremise(t *testing.T) { + t.Run("Instanciate a new SDK with a custom API URL as argument", func (t *testing.T) { + var apiKey = "1234" + var apiURL = "https://test.carbone.io" + csdk2, err := NewCarboneSDK(apiKey, apiURL) + if err != nil { + log.Fatal(err) + } + if (csdk2.apiURL != apiURL) { + t.Error(errors.New("URL differents")) + } + if (csdk2.apiAccessToken != apiKey) { + t.Error(errors.New("API Key differents")) + } + }) + + t.Run("Instanciate a new SDK with a custom API URL as environment variable", func (t *testing.T) { + var apiKey = "4321" + var apiURL = "https://env.test.carbone.io" + os.Setenv("CARBONE_TOKEN", apiKey) + os.Setenv("CARBONE_URL", apiURL) + csdk3, err := NewCarboneSDK() + if err != nil { + log.Fatal(err) + } + if (csdk3.apiURL != apiURL) { + t.Error(errors.New("URL differents")) + } + if (csdk3.apiAccessToken != apiKey) { + t.Error(errors.New("API Key differents")) + } + os.Unsetenv("CARBONE_TOKEN") + os.Unsetenv("CARBONE_URL") + }) +} + func TestGenerateTemplateID(t *testing.T) { t.Run("(node test 1) generate a templateID from a file without payload", func(t *testing.T) { template := "./tests/template.test.odt"