Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
chore: enhance configuration of APISet (#378)
Browse files Browse the repository at this point in the history
* enhance configuration of APISet

Signed-off-by: warber <[email protected]>

* added WithScheme option

Signed-off-by: warber <[email protected]>
  • Loading branch information
warber authored Jan 24, 2022
1 parent b7470c0 commit d68990e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 21 deletions.
78 changes: 60 additions & 18 deletions pkg/api/utils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type KeptnInterface interface {
type APISet struct {
endpointURL *url.URL
apiToken string
authHeader string
scheme string
httpClient *http.Client
apiHandler *APIHandler
authHandler *AuthHandler
eventHandler *EventHandler
Expand Down Expand Up @@ -108,27 +111,66 @@ func (c *APISet) Endpoint() *url.URL {
return c.endpointURL
}

// NewAPISet creates a new APISet
func NewAPISet(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) (*APISet, error) {
// WithAuthToken sets the given auth token.
// Optionally a custom auth header can be set (default x-token)
func WithAuthToken(authToken string, authHeader ...string) func(*APISet) {
aHeader := "x-token"
if len(authHeader) > 0 {
aHeader = authHeader[0]
}
return func(a *APISet) {
a.apiToken = authToken
a.authHeader = aHeader
}
}

// WithHTTPClient configures a custom http client to use
func WithHTTPClient(client *http.Client) func(*APISet) {
return func(a *APISet) {
a.httpClient = client
}
}

// WithScheme sets the scheme
// If this option is not used, then default scheme "http" is used by the APISet
func WithScheme(scheme string) func(*APISet) {
return func(a *APISet) {
a.scheme = scheme
}
}

// New creates a new APISet instance
func New(baseURL string, options ...func(*APISet)) (*APISet, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, fmt.Errorf("unable to create apiset: %w", err)
}
httpClient = createInstrumentedClientTransport(httpClient)
var as APISet
as := &APISet{}
for _, o := range options {
o(as)
}
as.endpointURL = u
as.apiToken = authToken
as.apiHandler = createAuthenticatedAPIHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.authHandler = createAuthenticatedAuthHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.logHandler = createAuthenticatedLogHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.eventHandler = createAuthenticatedEventHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.projectHandler = createAuthProjectHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.resourceHandler = createAuthenticatedResourceHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.secretHandler = createAuthenticatedSecretHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.sequenceControlHandler = createAuthenticatedSequenceControlHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.serviceHandler = createAuthenticatedServiceHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.shipyardControlHandler = createAuthenticatedShipyardControllerHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.stageHandler = createAuthenticatedStageHandler(baseURL, authToken, authHeader, httpClient, scheme)
as.uniformHandler = createAuthenticatedUniformHandler(baseURL, authToken, authHeader, httpClient, scheme)
return &as, nil
as.httpClient = createInstrumentedClientTransport(as.httpClient)

if as.scheme == "" {
if as.endpointURL.Scheme != "" {
as.scheme = u.Scheme
} else {
as.scheme = "http"
}
}

as.apiHandler = createAuthenticatedAPIHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.authHandler = createAuthenticatedAuthHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.logHandler = createAuthenticatedLogHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.eventHandler = createAuthenticatedEventHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.projectHandler = createAuthProjectHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.resourceHandler = createAuthenticatedResourceHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.secretHandler = createAuthenticatedSecretHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.sequenceControlHandler = createAuthenticatedSequenceControlHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.serviceHandler = createAuthenticatedServiceHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.shipyardControlHandler = createAuthenticatedShipyardControllerHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.stageHandler = createAuthenticatedStageHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
as.uniformHandler = createAuthenticatedUniformHandler(baseURL, as.apiToken, as.authHeader, as.httpClient, as.scheme)
return as, nil
}
34 changes: 31 additions & 3 deletions pkg/api/utils/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package api

import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestApiSetWithInvalidURL(t *testing.T) {
apiSet, err := NewAPISet("://http.lol", "a-token", "x-token", nil, "http")
apiSet, err := New("://http.lol")
assert.Nil(t, apiSet)
assert.Error(t, err)
}

func TestApiSetCreatesHandlers(t *testing.T) {
apiSet, err := NewAPISet("http://base-url.com", "a-token", "x-token", nil, "http")
apiSet, err := New("http://base-url.com")
assert.NoError(t, err)
assert.Equal(t, "a-token", apiSet.Token())
assert.Equal(t, "http://base-url.com", apiSet.Endpoint().String())
assert.Equal(t, "http", apiSet.scheme)
assert.NotNil(t, apiSet.UniformV1())
assert.NotNil(t, apiSet.Endpoint())
assert.NotNil(t, apiSet.ShipyardControlHandlerV1())
Expand All @@ -30,3 +31,30 @@ func TestApiSetCreatesHandlers(t *testing.T) {
assert.NotNil(t, apiSet.ResourcesV1())
assert.NotNil(t, apiSet.LogsV1())
}

func TestAPISetDefaultValues(t *testing.T) {
apiSet, err := New("base-url.com")
assert.Nil(t, err)
assert.NotNil(t, apiSet)
assert.Equal(t, "http", apiSet.scheme)
assert.Equal(t, "", apiSet.authHeader)
assert.Equal(t, "", apiSet.apiToken)
assert.NotNil(t, apiSet.httpClient)

apiSet, err = New("https://base-url.com")
assert.Nil(t, err)
assert.NotNil(t, apiSet)
assert.Equal(t, "https", apiSet.scheme)
assert.Equal(t, "", apiSet.authHeader)
assert.Equal(t, "", apiSet.apiToken)
assert.NotNil(t, apiSet.httpClient)
}

func TestAPISetWithOptions(t *testing.T) {
apiSet, err := New("base-url.com", WithAuthToken("a-token"), WithHTTPClient(&http.Client{}), WithScheme("https"))
assert.NoError(t, err)
assert.Equal(t, "a-token", apiSet.Token())
assert.Equal(t, "x-token", apiSet.authHeader)
assert.Equal(t, "https", apiSet.scheme)
assert.NotNil(t, apiSet.httpClient)
}

0 comments on commit d68990e

Please sign in to comment.