From c3b7e45556a47a665296f48e3be58a40d8f9f35b Mon Sep 17 00:00:00 2001 From: Bailin He <15058035+bailinhe@users.noreply.github.com> Date: Tue, 28 May 2024 11:05:42 -0400 Subject: [PATCH] small-fixes-client-lib (#121) - use 201 for extension resource create - add support for query strings --- pkg/client/sys_extension_resources.go | 24 ++++++++++++++++++--- pkg/client/sys_extension_resources_test.go | 8 +++---- pkg/client/user_extension_resources.go | 23 +++++++++++++++++--- pkg/client/user_extension_resources_test.go | 12 +++++------ 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/pkg/client/sys_extension_resources.go b/pkg/client/sys_extension_resources.go index f1a1948..703fa82 100644 --- a/pkg/client/sys_extension_resources.go +++ b/pkg/client/sys_extension_resources.go @@ -105,7 +105,8 @@ func (c *Client) SystemExtensionResource( // SystemExtensionResources list all system resources func (c *Client) SystemExtensionResources( - ctx context.Context, extensionSlug, erdSlugPlural, erdVersion string, deleted bool, + ctx context.Context, extensionSlug, erdSlugPlural, erdVersion string, + deleted bool, queries map[string]string, ) ([]*v1alpha1.SystemExtensionResource, error) { if extensionSlug == "" { return nil, ErrMissingExtensionIDOrSlug @@ -121,7 +122,24 @@ func (c *Client) SystemExtensionResources( ) if deleted { - u += "?deleted" + queries["deleted"] = "" + } + + i := 0 + for k, v := range queries { + if i == 0 { + u += "?" + } else { + u += "&" + } + + if v == "" { + u += k + } else { + u += fmt.Sprintf("%s=%s", k, v) + } + + i++ } req, err := c.newGovernorRequest(ctx, http.MethodGet, u) @@ -209,7 +227,7 @@ func (c *Client) CreateSystemExtensionResource( return nil, handleResourceStatusNotFound(respBody) } - if resp.StatusCode != http.StatusOK && + if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusNoContent { return nil, ErrRequestNonSuccess diff --git a/pkg/client/sys_extension_resources_test.go b/pkg/client/sys_extension_resources_test.go index 7181fac..9a532ca 100644 --- a/pkg/client/sys_extension_resources_test.go +++ b/pkg/client/sys_extension_resources_test.go @@ -168,7 +168,7 @@ func TestClient_SystemExtensionResources(t *testing.T) { token: &oauth2.Token{AccessToken: "topSekret"}, } got, err := c.SystemExtensionResources( - context.TODO(), "test-extension-1", "some-resources", "v1", false, + context.TODO(), "test-extension-1", "some-resources", "v1", false, nil, ) if tt.expectedErr != nil { @@ -411,7 +411,7 @@ func TestClient_CreateSystemExtensionResource(t *testing.T) { httpClient: &mockHTTPDoer{ t: t, resp: []byte(testExtensionResourceResponse), - statusCode: http.StatusOK, + statusCode: http.StatusCreated, }, }, req: testExtensionResourcePayload, @@ -457,7 +457,7 @@ func TestClient_CreateSystemExtensionResource(t *testing.T) { fields: fields{ httpClient: &mockHTTPDoer{ t: t, - statusCode: http.StatusOK, + statusCode: http.StatusCreated, resp: []byte(`{`), }, }, @@ -473,7 +473,7 @@ func TestClient_CreateSystemExtensionResource(t *testing.T) { httpClient: &mockHTTPDoer{ t: t, resp: []byte(testExtensionResourceResponse), - statusCode: http.StatusOK, + statusCode: http.StatusCreated, }, }, req: testExtensionResourcePayload, diff --git a/pkg/client/user_extension_resources.go b/pkg/client/user_extension_resources.go index b9c5fe9..8d00f2a 100644 --- a/pkg/client/user_extension_resources.go +++ b/pkg/client/user_extension_resources.go @@ -83,7 +83,7 @@ func (c *Client) UserExtensionResource( // UserExtensionResources lists all user extension resources for a user func (c *Client) UserExtensionResources( ctx context.Context, userID, extensionSlug, erdSlugPlural, erdVersion string, - deleted bool, + deleted bool, queries map[string]string, ) ([]*v1alpha1.UserExtensionResource, error) { if userID == "" { return nil, ErrMissingUserID @@ -108,7 +108,24 @@ func (c *Client) UserExtensionResources( ) if deleted { - u += "?deleted" + queries["deleted"] = "" + } + + i := 0 + for k, v := range queries { + if i == 0 { + u += "?" + } else { + u += "&" + } + + if v == "" { + u += k + } else { + u += fmt.Sprintf("%s=%s", k, v) + } + + i++ } req, err := c.newGovernorRequest(ctx, http.MethodGet, u) @@ -196,7 +213,7 @@ func (c *Client) CreateUserExtensionResource( return nil, handleResourceStatusNotFound(respBody) } - if resp.StatusCode != http.StatusOK && + if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusNoContent { return nil, ErrRequestNonSuccess diff --git a/pkg/client/user_extension_resources_test.go b/pkg/client/user_extension_resources_test.go index bb5b4f2..aa37f5c 100644 --- a/pkg/client/user_extension_resources_test.go +++ b/pkg/client/user_extension_resources_test.go @@ -117,7 +117,7 @@ func TestClient_UserExtensionResources(t *testing.T) { token: &oauth2.Token{AccessToken: "topSekret"}, } got, err := c.UserExtensionResources( - context.TODO(), "user-1", "test-extension-1", "some-resources", "v1", false, + context.TODO(), "user-1", "test-extension-1", "some-resources", "v1", false, nil, ) if tt.expectedErr != nil { @@ -422,7 +422,7 @@ func TestClient_CreateUserExtensionResource(t *testing.T) { httpClient: &mockHTTPDoer{ t: t, resp: []byte(testExtensionResourceResponse), - statusCode: http.StatusOK, + statusCode: http.StatusCreated, }, }, req: testExtensionResourcePayload, @@ -471,7 +471,7 @@ func TestClient_CreateUserExtensionResource(t *testing.T) { fields: fields{ httpClient: &mockHTTPDoer{ t: t, - statusCode: http.StatusOK, + statusCode: http.StatusCreated, resp: []byte(`{`), }, }, @@ -488,7 +488,7 @@ func TestClient_CreateUserExtensionResource(t *testing.T) { httpClient: &mockHTTPDoer{ t: t, resp: []byte(testExtensionResourceResponse), - statusCode: http.StatusOK, + statusCode: http.StatusCreated, }, }, req: testExtensionResourcePayload, @@ -505,7 +505,7 @@ func TestClient_CreateUserExtensionResource(t *testing.T) { httpClient: &mockHTTPDoer{ t: t, resp: []byte(testExtensionResourceResponse), - statusCode: http.StatusOK, + statusCode: http.StatusCreated, }, }, req: testExtensionResourcePayload, @@ -522,7 +522,7 @@ func TestClient_CreateUserExtensionResource(t *testing.T) { httpClient: &mockHTTPDoer{ t: t, resp: []byte(testExtensionResourceResponse), - statusCode: http.StatusOK, + statusCode: http.StatusCreated, }, }, req: testExtensionResourcePayload,