Skip to content

Commit

Permalink
Rework how components are instantiated
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 5, 2024
1 parent 5bbb835 commit 1191f4f
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 248 deletions.
25 changes: 11 additions & 14 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func NewClient(opts ...ClientOption) (*Client, error) {
}

if c.apiKey == "" {
c.apiKey, _ = getenv(c.env, "DISPATCH_API_KEY")
c.apiKey = getenv(c.env, "DISPATCH_API_KEY")
}
if c.apiKey == "" {
return nil, fmt.Errorf("API key has not been set. Use WithAPIKey(..), or set the DISPATCH_API_KEY environment variable")
return nil, fmt.Errorf("Dispatch API key has not been set. Use WithAPIKey(..), or set the DISPATCH_API_KEY environment variable")
}

if c.apiUrl == "" {
c.apiUrl, _ = getenv(c.env, "DISPATCH_API_URL")
c.apiUrl = getenv(c.env, "DISPATCH_API_URL")
}
if c.apiUrl == "" {
c.apiUrl = DefaultApiUrl
Expand All @@ -50,21 +50,21 @@ func NewClient(opts ...ClientOption) (*Client, error) {
c.httpClient = http.DefaultClient
}

authenticatingInterceptor := connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
authenticator := connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
authorization := "Bearer " + c.apiKey
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
req.Header().Add("Authorization", authorization)
return next(ctx, req)
}
})

validatingInterceptor, err := validate.NewInterceptor()
validator, err := validate.NewInterceptor()
if err != nil {
return nil, err
}

c.client = sdkv1connect.NewDispatchServiceClient(c.httpClient, c.apiUrl,
connect.WithInterceptors(validatingInterceptor, authenticatingInterceptor))
connect.WithInterceptors(validator, authenticator))

return c, nil
}
Expand Down Expand Up @@ -92,10 +92,10 @@ func WithAPIUrl(apiUrl string) ClientOption {
const DefaultApiUrl = "https://api.dispatch.run"

// WithClientEnv sets the environment variables that a Client parses
// default configuration from.
// its default configuration from.
//
// It defaults to os.Environ().
func WithClientEnv(env []string) ClientOption {
func WithClientEnv(env ...string) ClientOption {
return func(c *Client) { c.env = env }
}

Expand Down Expand Up @@ -144,15 +144,12 @@ func (b *Batch) Dispatch(ctx context.Context) ([]ID, error) {
return res.Msg.DispatchIds, nil
}

func getenv(env []string, name string) (string, bool) {
if env == nil {
env = os.Environ()
}
func getenv(env []string, name string) string {
for _, s := range env {
n, v, ok := strings.Cut(s, "=")
if ok && n == name {
return v, true
return v
}
}
return "", false
return ""
}
15 changes: 7 additions & 8 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (
)

func TestClient(t *testing.T) {
var recorder dispatchtest.CallRecorder

server := dispatchtest.NewDispatchServer(&recorder)
recorder := &dispatchtest.CallRecorder{}
server := dispatchtest.NewDispatchServer(recorder)

client, err := dispatch.NewClient(dispatch.WithAPIKey("foobar"), dispatch.WithAPIUrl(server.URL))
if err != nil {
Expand Down Expand Up @@ -41,10 +40,10 @@ func TestClientEnvConfig(t *testing.T) {
recorder := &dispatchtest.CallRecorder{}
server := dispatchtest.NewDispatchServer(recorder)

client, err := dispatch.NewClient(dispatch.WithClientEnv([]string{
client, err := dispatch.NewClient(dispatch.WithClientEnv(
"DISPATCH_API_KEY=foobar",
"DISPATCH_API_URL=" + server.URL,
}))
"DISPATCH_API_URL="+server.URL,
))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -121,10 +120,10 @@ func TestClientBatch(t *testing.T) {
}

func TestClientNoAPIKey(t *testing.T) {
_, err := dispatch.NewClient(dispatch.WithClientEnv(nil))
_, err := dispatch.NewClient(dispatch.WithClientEnv( /* i.e. no env vars */ ))
if err == nil {
t.Fatalf("expected an error")
} else if err.Error() != "API key has not been set. Use WithAPIKey(..), or set the DISPATCH_API_KEY environment variable" {
} else if err.Error() != "Dispatch API key has not been set. Use WithAPIKey(..), or set the DISPATCH_API_KEY environment variable" {
t.Errorf("unexpected error: %v", err)
}
}
Loading

0 comments on commit 1191f4f

Please sign in to comment.