@@ -3,7 +3,6 @@ package common
33import (
44 "context"
55 "encoding/json"
6- "errors"
76 "fmt"
87 "io/ioutil"
98 "net/http"
@@ -16,13 +15,12 @@ func (client *sysdigCommonClient) GetUserById(ctx context.Context, id int) (u *U
1615 }
1716 defer response .Body .Close ()
1817
19- body , _ := ioutil .ReadAll (response .Body )
20-
2118 if response .StatusCode != http .StatusOK {
22- err = errors . New (response . Status )
19+ err = errorFromResponse (response )
2320 return
2421 }
2522
23+ body , _ := ioutil .ReadAll (response .Body )
2624 user := UserFromJSON (body )
2725 return & user , nil
2826}
@@ -34,18 +32,16 @@ func (client *sysdigCommonClient) GetUserByEmail(ctx context.Context, email stri
3432 }
3533 defer response .Body .Close ()
3634
37- body , _ := ioutil .ReadAll (response .Body )
38-
3935 if response .StatusCode != http .StatusOK {
40- err = errors . New (response . Status )
36+ err = errorFromResponse (response )
4137 return
4238 }
4339
4440 var userList struct {
4541 Users []User `json:"users"`
4642 }
4743
48- err = json .Unmarshal ( body , & userList )
44+ err = json .NewDecoder ( response . Body ). Decode ( & userList )
4945 if err != nil {
5046 return
5147 }
@@ -67,13 +63,12 @@ func (client *sysdigCommonClient) CreateUser(ctx context.Context, uRequest *User
6763 }
6864 defer response .Body .Close ()
6965
70- body , _ := ioutil .ReadAll (response .Body )
71-
7266 if response .StatusCode != http .StatusOK && response .StatusCode != http .StatusCreated {
73- err = errors . New (response . Status )
67+ err = errorFromResponse (response )
7468 return
7569 }
7670
71+ body , _ := ioutil .ReadAll (response .Body )
7772 user := UserFromJSON (body )
7873 return & user , nil
7974}
@@ -85,13 +80,12 @@ func (client *sysdigCommonClient) UpdateUser(ctx context.Context, uRequest *User
8580 }
8681 defer response .Body .Close ()
8782
88- body , _ := ioutil .ReadAll (response .Body )
89-
9083 if response .StatusCode != http .StatusOK {
91- err = errors . New (response . Status )
84+ err = errorFromResponse (response )
9285 return
9386 }
9487
88+ body , _ := ioutil .ReadAll (response .Body )
9589 user := UserFromJSON (body )
9690 return & user , nil
9791}
@@ -103,8 +97,8 @@ func (client *sysdigCommonClient) DeleteUser(ctx context.Context, id int) error
10397 }
10498 defer response .Body .Close ()
10599
106- if response .StatusCode != http .StatusNoContent && response .StatusCode != http .StatusOK {
107- return errors . New (response . Status )
100+ if response .StatusCode != http .StatusNoContent && response .StatusCode != http .StatusOK && response . StatusCode != http . StatusNotFound {
101+ return errorFromResponse (response )
108102 }
109103 return nil
110104}
@@ -117,11 +111,11 @@ func (client *sysdigCommonClient) GetCurrentUser(ctx context.Context) (u *User,
117111 defer response .Body .Close ()
118112
119113 if response .StatusCode != http .StatusOK {
120- err = errors . New (response . Status )
114+ err = errorFromResponse (response )
121115 return
122116 }
123- body , _ := ioutil .ReadAll (response .Body )
124117
118+ body , _ := ioutil .ReadAll (response .Body )
125119 user := UserFromJSON (body )
126120 return & user , nil
127121}
0 commit comments