Skip to content

Commit

Permalink
small-fixes-client-lib (#121)
Browse files Browse the repository at this point in the history
- use 201 for extension resource create
- add support for query strings
  • Loading branch information
bailinhe authored May 28, 2024
1 parent 7db1753 commit c3b7e45
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
24 changes: 21 additions & 3 deletions pkg/client/sys_extension_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/sys_extension_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -457,7 +457,7 @@ func TestClient_CreateSystemExtensionResource(t *testing.T) {
fields: fields{
httpClient: &mockHTTPDoer{
t: t,
statusCode: http.StatusOK,
statusCode: http.StatusCreated,
resp: []byte(`{`),
},
},
Expand All @@ -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,
Expand Down
23 changes: 20 additions & 3 deletions pkg/client/user_extension_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/client/user_extension_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -471,7 +471,7 @@ func TestClient_CreateUserExtensionResource(t *testing.T) {
fields: fields{
httpClient: &mockHTTPDoer{
t: t,
statusCode: http.StatusOK,
statusCode: http.StatusCreated,
resp: []byte(`{`),
},
},
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit c3b7e45

Please sign in to comment.