Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sdk): perform request instead of only creating it #87

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 19 additions & 59 deletions internal/sdk/cloudian/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,9 @@ func (client Client) ListUsers(ctx context.Context, groupId string, offsetUserId
params["offset"] = *offsetUserId
}

req, err := client.newRequest(ctx, http.MethodGet, "/user/list", params, nil)
resp, err := client.newRequest(ctx, http.MethodGet, "/user/list", params, nil)
if err != nil {
return nil, fmt.Errorf("GET error creating list request: %w", err)
}

resp, err := client.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("GET list users failed: %w", err)
return nil, err
}

defer resp.Body.Close() // nolint:errcheck
Expand Down Expand Up @@ -196,15 +191,10 @@ func (client Client) ListUsers(ctx context.Context, groupId string, offsetUserId

// Delete a single user. Errors if the user does not exist.
func (client Client) DeleteUser(ctx context.Context, user User) error {
req, err := client.newRequest(ctx, http.MethodDelete, "/user",
resp, err := client.newRequest(ctx, http.MethodDelete, "/user",
map[string]string{"groupId": user.GroupID, "userId": user.UserID}, nil)
if err != nil {
return fmt.Errorf("DELETE error creating request: %w", err)
}

resp, err := client.httpClient.Do(req)
if err != nil {
return fmt.Errorf("DELETE to cloudian /user got: %w", err)
return err
}
defer resp.Body.Close() // nolint:errcheck

Expand All @@ -224,30 +214,20 @@ func (client Client) CreateUser(ctx context.Context, user User) error {
return fmt.Errorf("error marshaling JSON: %w", err)
}

req, err := client.newRequest(ctx, http.MethodPut, "/user", nil, jsonData)
resp, err := client.newRequest(ctx, http.MethodPut, "/user", nil, jsonData)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

resp, err := client.httpClient.Do(req)
if err != nil {
return fmt.Errorf("PUT to cloudian /user: %w", err)
return err
}

return resp.Body.Close()
}

// GetUserCredentials fetches all the credentials of a user.
func (client Client) GetUserCredentials(ctx context.Context, user User) ([]SecurityInfo, error) {
req, err := client.newRequest(ctx, http.MethodGet, "/user/credentials/list",
resp, err := client.newRequest(ctx, http.MethodGet, "/user/credentials/list",
map[string]string{"groupId": user.GroupID, "userId": user.UserID}, nil)
if err != nil {
return nil, fmt.Errorf("error creating credentials request: %w", err)
}

resp, err := client.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error performing credentials request: %w", err)
return nil, err
}

defer resp.Body.Close() // nolint:errcheck
Expand Down Expand Up @@ -276,7 +256,6 @@ func (client Client) GetUserCredentials(ctx context.Context, user User) ([]Secur
// Delete a group and all its members.
func (client Client) DeleteGroupRecursive(ctx context.Context, groupId string) error {
users, err := client.ListUsers(ctx, groupId, nil)

if err != nil {
return fmt.Errorf("error listing users: %w", err)
}
Expand All @@ -292,15 +271,10 @@ func (client Client) DeleteGroupRecursive(ctx context.Context, groupId string) e

// Deletes a group if it is without members.
func (client Client) DeleteGroup(ctx context.Context, groupId string) error {
req, err := client.newRequest(ctx, http.MethodDelete, "/group",
resp, err := client.newRequest(ctx, http.MethodDelete, "/group",
map[string]string{"groupId": groupId}, nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

resp, err := client.httpClient.Do(req)
if err != nil {
return fmt.Errorf("DELETE to cloudian /group got: %w", err)
return err
}

return resp.Body.Close()
Expand All @@ -313,14 +287,9 @@ func (client Client) CreateGroup(ctx context.Context, group Group) error {
return fmt.Errorf("error marshaling JSON: %w", err)
}

req, err := client.newRequest(ctx, http.MethodPut, "/group", nil, jsonData)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

resp, err := client.httpClient.Do(req)
resp, err := client.newRequest(ctx, http.MethodPut, "/group", nil, jsonData)
if err != nil {
return fmt.Errorf("POST to cloudian /group: %w", err)
return err
}

return resp.Body.Close()
Expand All @@ -334,14 +303,9 @@ func (client Client) UpdateGroup(ctx context.Context, group Group) error {
}

// Create a context with a timeout
req, err := client.newRequest(ctx, http.MethodPost, "/group", nil, jsonData)
resp, err := client.newRequest(ctx, http.MethodPost, "/group", nil, jsonData)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}

resp, err := client.httpClient.Do(req)
if err != nil {
return fmt.Errorf("PUT to cloudian /group: %w", err)
return err
}

return resp.Body.Close()
Expand All @@ -350,17 +314,12 @@ func (client Client) UpdateGroup(ctx context.Context, group Group) error {
// Get a group. Returns an error even in the case of a group not found.
// This error can then be checked against ErrNotFound: errors.Is(err, ErrNotFound)
func (client Client) GetGroup(ctx context.Context, groupId string) (*Group, error) {
req, err := client.newRequest(ctx, http.MethodGet, "/group",
resp, err := client.newRequest(ctx, http.MethodGet, "/group",
map[string]string{"groupId": groupId}, nil)
if err != nil {
return nil, err
}

resp, err := client.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("GET error: %w", err)
}

defer resp.Body.Close() // nolint:errcheck

switch resp.StatusCode {
Expand All @@ -385,15 +344,16 @@ func (client Client) GetGroup(ctx context.Context, groupId string) (*Group, erro
}
}

func (client Client) newRequest(ctx context.Context, method string, url string, query map[string]string, body []byte) (*http.Request, error) {
func (client Client) newRequest(ctx context.Context, method string, url string, query map[string]string, body []byte) (*http.Response, error) {
var buffer io.Reader = nil
if body != nil {
buffer = bytes.NewBuffer(body)
}
req, err := http.NewRequestWithContext(ctx, method, client.baseURL+url, buffer)
if err != nil {
return req, fmt.Errorf("error creating request: %w", err)
return nil, fmt.Errorf("error creating request: %w", err)
}

req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", client.authHeader)
Expand All @@ -404,5 +364,5 @@ func (client Client) newRequest(ctx context.Context, method string, url string,
}
req.URL.RawQuery = q.Encode()

return req, nil
return client.httpClient.Do(req)
}
Loading