Skip to content

Commit

Permalink
fix: cloudian sdk should not log
Browse files Browse the repository at this point in the history
  • Loading branch information
mariatsji committed Nov 22, 2024
1 parent 768565d commit a8f26e2
Showing 1 changed file with 18 additions and 35 deletions.
53 changes: 18 additions & 35 deletions internal/sdk/cloudian/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func (client Client) ListUsers(groupId string, offsetUserId *string) ([]User, er
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
fmt.Println("GET error creating list request: ", err)
return nil, err
return nil, fmt.Errorf("GET error creating list request: %w", err)
}

resp, err := client.httpClient.Do(req)
Expand All @@ -91,14 +90,12 @@ func (client Client) ListUsers(groupId string, offsetUserId *string) ([]User, er
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close() // nolint:errcheck
if err != nil {
fmt.Println("GET reading list users response body failed: ", err)
return nil, err
return nil, fmt.Errorf("GET reading list users response body failed: %w", err)
}

users, err := unmarshalUsersJson(body)
if err != nil {
fmt.Println("GET unmarshal users response body failed: ", err)
return nil, err
return nil, fmt.Errorf("GET unmarshal users response body failed: %w", err)
}

retVal = append(retVal, users...)
Expand All @@ -117,8 +114,7 @@ func (client Client) ListUsers(groupId string, offsetUserId *string) ([]User, er

return retVal, nil
} else {
fmt.Println("GET list users failed: ", err)
return nil, err
return nil, fmt.Errorf("GET list users failed: %w", err)
}

}
Expand All @@ -132,8 +128,7 @@ func (client Client) DeleteUser(user User) error {
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
if err != nil {
fmt.Println("DELETE error creating request: ", err)
return err
return fmt.Errorf("DELETE error creating request: %w", err)
}

req.Header.Set("Content-Type", "application/json")
Expand All @@ -158,8 +153,7 @@ func (client Client) DeleteGroupRecursive(groupId string) error {
for _, user := range users {
err := client.DeleteUser(user)
if err != nil {
fmt.Println("Error deleting user: ", err)
return err
return fmt.Errorf("Error deleting user: %w", err)
}
}

Expand All @@ -178,8 +172,7 @@ func (client Client) DeleteGroup(groupId string) error {
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
if err != nil {
fmt.Println("DELETE error creating request: ", err)
return err
return fmt.Errorf("DELETE error creating request: %w", err)
}

req.Header.Set("Content-Type", "application/json")
Expand All @@ -189,8 +182,7 @@ func (client Client) DeleteGroup(groupId string) error {

if err != nil {
statusErrStr := strconv.Itoa(resp.StatusCode)
fmt.Println("DELETE to cloudian /group got status code ["+statusErrStr+"]", err)
return err
return fmt.Errorf("DELETE to cloudian /group got status code [%s]: %w", statusErrStr, err)
}
defer resp.Body.Close() // nolint:errcheck

Expand All @@ -202,17 +194,15 @@ func (client Client) CreateGroup(group Group) error {

jsonData, err := marshalGroup(group)
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return err
return fmt.Errorf("Error marshaling JSON: %w", err)
}

// Create a context with a timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("POST error creating request: ", err)
return err
return fmt.Errorf("POST error creating request: %w", err)
}

req.Header.Set("Content-Type", "application/json")
Expand All @@ -222,8 +212,7 @@ func (client Client) CreateGroup(group Group) error {

if err != nil {
statusErrStr := strconv.FormatInt(int64(resp.StatusCode), 10)
fmt.Println("POST to cloudian /group got status code ["+statusErrStr+"]", err)
return err
return fmt.Errorf("POST to cloudian /group got status code [%s]: %w", statusErrStr, err)
}
defer resp.Body.Close() // nolint:errcheck

Expand All @@ -235,17 +224,15 @@ func (client Client) UpdateGroup(group Group) error {

jsonData, err := marshalGroup(group)
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return err
return fmt.Errorf("Error marshaling JSON: %w", err)
}

// Create a context with a timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("POST error creating request: ", err)
return err
return fmt.Errorf("PUT error creating request: %w", err)
}

req.Header.Set("Content-Type", "application/json")
Expand All @@ -255,7 +242,7 @@ func (client Client) UpdateGroup(group Group) error {

if err != nil {
statusErrStr := strconv.FormatInt(int64(resp.StatusCode), 10)
fmt.Println("PUT to cloudian /group got status code ["+statusErrStr+"]", err)
return fmt.Errorf("PUT to cloudian /group got status code [%s]: %w", statusErrStr, err)
}

defer resp.Body.Close() // nolint:errcheck
Expand All @@ -271,8 +258,7 @@ func (client Client) GetGroup(groupId string) (*Group, error) {
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
fmt.Println("GET error creating request: ", err)
return nil, err
return nil, fmt.Errorf("GET error creating request: %w", err)
}

req.Header.Set("Content-Type", "application/json")
Expand All @@ -281,23 +267,20 @@ func (client Client) GetGroup(groupId string) (*Group, error) {
resp, err := client.httpClient.Do(req)

if err != nil {
fmt.Println("GET errored towards Cloudian /group: ", err)
return nil, err
return nil, fmt.Errorf("GET error: %w", err)
}

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

if resp.StatusCode == 200 {
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("GET reading response body failed: ", err)
return nil, err
return nil, fmt.Errorf("GET reading response body failed: %w", err)
}

group, err := unmarshalGroupJson(body)
if err != nil {
fmt.Println("GET unmarshal response body failed: ", err)
return nil, err
return nil, fmt.Errorf("GET unmarshal response body failed: %w", err)
}

return &group, nil
Expand Down

0 comments on commit a8f26e2

Please sign in to comment.