Skip to content

Commit

Permalink
API not blockin, custom URL and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steevepay committed Sep 14, 2023
1 parent 82c2499 commit ee5b608
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
5 changes: 4 additions & 1 deletion API-REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions carbone/carbone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}
Expand Down
50 changes: 50 additions & 0 deletions carbone/carbone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ee5b608

Please sign in to comment.