Skip to content

Commit

Permalink
Unify environment variable usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmigueprieto committed Jan 2, 2025
1 parent 2750549 commit 9a44e71
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 105 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ jobs:
gotestsum --format testname --junitfile report.xml -- -v ./test/integration_tests/...
env:
CONDUCTOR_SERVER_URL: ${{ secrets.CONDUCTOR_SERVER_URL }}
KEY: ${{ secrets.KEY }}
SECRET: ${{ secrets.SECRET }}
CONDUCTOR_AUTH_KEY: ${{ secrets.KEY }}
CONDUCTOR_AUTH_SECRET: ${{ secrets.SECRET }}

- name: Set PR Status to Failure
if: ${{ failure() }}
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ RUN go test -v $(go list ./... | grep -v /test/integration_tests)

FROM build as inttest
COPY /test /package/test
ARG KEY
ARG SECRET
ARG CONDUCTOR_AUTH_KEY
ARG CONDUCTOR_AUTH_SECRET
ARG CONDUCTOR_SERVER_URL
ENV KEY=${KEY}
ENV SECRET=${SECRET}
ENV CONDUCTOR_AUTH_KEY=${CONDUCTOR_AUTH_KEY}
ENV CONDUCTOR_AUTH_SECRET=${CONDUCTOR_AUTH_SECRET}
ENV CONDUCTOR_SERVER_URL=${CONDUCTOR_SERVER_URL}
RUN go test -v ./test/integration_tests/...
37 changes: 6 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,41 +173,16 @@ To begin with, let's take a look at the variable declaration in [examples/hello_
```go
var (
apiClient = client.NewAPIClient(
authSettings(),
httpSettings(),
)
apiClient = client.NewAPIClientFromEnv()
taskRunner = worker.NewTaskRunnerWithApiClient(apiClient)
workflowExecutor = executor.NewWorkflowExecutor(apiClient)
)
func authSettings() *settings.AuthenticationSettings {
key := os.Getenv("KEY")
secret := os.Getenv("SECRET")
if key != "" && secret != "" {
return settings.NewAuthenticationSettings(
key,
secret,
)
}
return nil
}
func httpSettings() *settings.HttpSettings {
url := os.Getenv("CONDUCTOR_SERVER_URL")
if url == "" {
fmt.Fprintf(os.Stderr, "Error: CONDUCTOR_SERVER_URL env variable is not set\n")
os.Exit(1)
}
return settings.NewHttpSettings(url)
}
```
First we create an `APIClient` instance. This is a REST client.
We need to pass on the proper settings to our client. For convenience to run the example you can set the following environment variables: `CONDUCTOR_SERVER_URL`, `KEY`, `SECRET`.
We need to provide the correct settings to our client. In this example, `client.NewAPIClientFromEnv()` is used, which initializes a new client by reading the settings from the following environment variables: `CONDUCTOR_SERVER_URL`, `CONDUCTOR_AUTH_KEY`, and `CONDUCTOR_AUTH_SECRET`.
Now let's take a look at the `main` function:
Expand Down Expand Up @@ -265,11 +240,11 @@ cd examples
go run hello_world/main.go
```
#### Running the example in Orkes playground.
#### Running the example with an [Orkes developer account](https://developer.orkescloud.com).
```shell
export CONDUCTOR_SERVER_URL="https://play.orkes.io/api"
export KEY="..."
export SECRET="..."
export CONDUCTOR_SERVER_URL="https://developer.orkescloud.com/api"
export CONDUCTOR_AUTH_KEY="..."
export CONDUCTOR_AUTH_SECRET="..."
cd examples
go run hello_world/main.go
```
Expand Down
30 changes: 1 addition & 29 deletions examples/hello_world/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,23 @@ package main

import (
hello_world "examples/hello_world/src"
"os"
"time"

log "github.com/sirupsen/logrus"

"github.com/conductor-sdk/conductor-go/sdk/client"
"github.com/conductor-sdk/conductor-go/sdk/model"
"github.com/conductor-sdk/conductor-go/sdk/settings"

"github.com/conductor-sdk/conductor-go/sdk/worker"
"github.com/conductor-sdk/conductor-go/sdk/workflow/executor"
)

var (
apiClient = client.NewAPIClient(
authSettings(),
httpSettings(),
)
apiClient = client.NewAPIClientFromEnv()
taskRunner = worker.NewTaskRunnerWithApiClient(apiClient)
workflowExecutor = executor.NewWorkflowExecutor(apiClient)
)

func authSettings() *settings.AuthenticationSettings {
key := os.Getenv("KEY")
secret := os.Getenv("SECRET")
if key != "" && secret != "" {
return settings.NewAuthenticationSettings(
key,
secret,
)
}

return nil
}

func httpSettings() *settings.HttpSettings {
url := os.Getenv("CONDUCTOR_SERVER_URL")
if url == "" {
log.Error("Error: CONDUCTOR_SERVER_URL env variable is not set")
os.Exit(1)
}

return settings.NewHttpSettings(url)
}

func main() {
// Start the Greet Worker. This worker will process "greet" tasks.
taskRunner.StartWorker("greet", hello_world.Greet, 1, time.Millisecond*100)
Expand Down
22 changes: 18 additions & 4 deletions sdk/client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"net"
"net/http"
Expand Down Expand Up @@ -47,7 +48,6 @@ var (

type APIClient struct {
httpRequester *HttpRequester
tokenManager authentication.TokenManager
}

func NewAPIClient(
Expand All @@ -62,9 +62,23 @@ func NewAPIClient(
)
}
func NewAPIClientFromEnv() *APIClient {
authenticationSettings := settings.NewAuthenticationSettings(os.Getenv(CONDUCTOR_AUTH_KEY), os.Getenv(CONDUCTOR_AUTH_SECRET))
httpSettings := settings.NewHttpSettings(os.Getenv(CONDUCTOR_SERVER_URL))
return NewAPIClient(authenticationSettings, httpSettings)
return NewAPIClient(NewAuthenticationSettingsFromEnv(), NewHttpSettingsFromEnv())
}

func NewAuthenticationSettingsFromEnv() *settings.AuthenticationSettings {
return settings.NewAuthenticationSettings(
os.Getenv(CONDUCTOR_AUTH_KEY),
os.Getenv(CONDUCTOR_AUTH_SECRET),
)
}

func NewHttpSettingsFromEnv() *settings.HttpSettings {
url := os.Getenv(CONDUCTOR_SERVER_URL)
if url == "" {
log.Fatalf("Error: %s env variable is not set", CONDUCTOR_SERVER_URL)
}

return settings.NewHttpSettings(url)
}

func NewAPIClientWithTokenExpiration(
Expand Down
40 changes: 5 additions & 35 deletions test/testdata/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,19 @@ import (
"github.com/conductor-sdk/conductor-go/sdk/authentication"
"github.com/conductor-sdk/conductor-go/sdk/client"
"github.com/conductor-sdk/conductor-go/sdk/model"
"github.com/conductor-sdk/conductor-go/sdk/settings"
"github.com/conductor-sdk/conductor-go/sdk/worker"
"github.com/conductor-sdk/conductor-go/sdk/workflow"
"github.com/conductor-sdk/conductor-go/sdk/workflow/executor"

log "github.com/sirupsen/logrus"
)

const (
AUTHENTICATION_KEY_ID = "KEY"
AUTHENTICATION_KEY_SECRET = "SECRET"
BASE_URL = "CONDUCTOR_SERVER_URL"
)

var (
apiClient = getApiClientWithAuthentication()
)

var (
apiClient = client.NewAPIClientWithTokenExpiration(
client.NewAuthenticationSettingsFromEnv(),
client.NewHttpSettingsFromEnv(),
authentication.NewTokenExpiration(3*time.Second, 30*time.Second),
)
MetadataClient = client.MetadataResourceApiService{
APIClient: apiClient,
}
Expand Down Expand Up @@ -98,30 +92,6 @@ func ValidateWorkflowDaemon(waitTime time.Duration, outputChannel chan error, wo
outputChannel <- nil
}

func getApiClientWithAuthentication() *client.APIClient {
return client.NewAPIClientWithTokenExpiration(
getAuthenticationSettings(),
getHttpSettingsWithAuth(),
authentication.NewTokenExpiration(
3*time.Second,
30*time.Second,
),
)
}

func getAuthenticationSettings() *settings.AuthenticationSettings {
return settings.NewAuthenticationSettings(
os.Getenv(AUTHENTICATION_KEY_ID),
os.Getenv(AUTHENTICATION_KEY_SECRET),
)
}

func getHttpSettingsWithAuth() *settings.HttpSettings {
return settings.NewHttpSettings(
os.Getenv(BASE_URL),
)
}

func StartWorkflows(workflowQty int, workflowName string) ([]string, error) {
workflowIdList := make([]string, workflowQty)
for i := 0; i < workflowQty; i += 1 {
Expand Down

0 comments on commit 9a44e71

Please sign in to comment.