@@ -10,76 +10,69 @@ import (
1010)
1111
1212func (client * sysdigMonitorClient ) GetDashboardByID (ctx context.Context , ID int ) (* model.Dashboard , error ) {
13- res , err := client .doSysdigMonitorRequest (ctx , http .MethodGet , client .getDashboardUrl (ID ), nil )
13+ response , err := client .doSysdigMonitorRequest (ctx , http .MethodGet , client .getDashboardUrl (ID ), nil )
1414 if err != nil {
1515 return nil , err
1616 }
17- defer res .Body .Close ()
17+ defer response .Body .Close ()
1818
19- body , err := ioutil .ReadAll (res .Body )
20- if err != nil {
21- return nil , nil
19+ if response .StatusCode != http .StatusOK {
20+ return nil , errorFromResponse (response )
2221 }
2322
24- if res .StatusCode != http .StatusOK {
25- return nil , fmt .Errorf (string (body ))
23+ body , err := ioutil .ReadAll (response .Body )
24+ if err != nil {
25+ return nil , nil
2626 }
27-
2827 return model .DashboardFromJSON (body ), nil
2928}
3029
3130func (client * sysdigMonitorClient ) CreateDashboard (ctx context.Context , dashboard * model.Dashboard ) (* model.Dashboard , error ) {
32- res , err := client .doSysdigMonitorRequest (ctx , http .MethodPost , client .getDashboardsUrl (), dashboard .ToJSON ())
31+ response , err := client .doSysdigMonitorRequest (ctx , http .MethodPost , client .getDashboardsUrl (), dashboard .ToJSON ())
3332 if err != nil {
3433 return nil , err
3534 }
36- defer res .Body .Close ()
35+ defer response .Body .Close ()
3736
38- body , err := ioutil .ReadAll (res .Body )
39- if err != nil {
40- return nil , err
37+ if response .StatusCode != http .StatusOK && response .StatusCode != http .StatusCreated {
38+ return nil , errorFromResponse (response )
4139 }
4240
43- if res .StatusCode != http .StatusOK && res .StatusCode != http .StatusCreated {
44- return nil , fmt .Errorf (string (body ))
41+ body , err := ioutil .ReadAll (response .Body )
42+ if err != nil {
43+ return nil , err
4544 }
46-
4745 return model .DashboardFromJSON (body ), nil
4846}
4947
5048func (client * sysdigMonitorClient ) UpdateDashboard (ctx context.Context , dashboard * model.Dashboard ) (* model.Dashboard , error ) {
51- res , err := client .doSysdigMonitorRequest (ctx , http .MethodPut , client .getDashboardUrl (dashboard .ID ), dashboard .ToJSON ())
49+ response , err := client .doSysdigMonitorRequest (ctx , http .MethodPut , client .getDashboardUrl (dashboard .ID ), dashboard .ToJSON ())
5250 if err != nil {
5351 return nil , err
5452 }
55- defer res .Body .Close ()
53+ defer response .Body .Close ()
5654
57- body , err := ioutil .ReadAll (res .Body )
58- if err != nil {
59- return nil , err
55+ if response .StatusCode != http .StatusOK && response .StatusCode != http .StatusCreated {
56+ return nil , errorFromResponse (response )
6057 }
6158
62- if res .StatusCode != http .StatusOK && res .StatusCode != http .StatusCreated {
63- return nil , fmt .Errorf (string (body ))
59+ body , err := ioutil .ReadAll (response .Body )
60+ if err != nil {
61+ return nil , err
6462 }
6563
6664 return model .DashboardFromJSON (body ), nil
6765}
6866
6967func (client * sysdigMonitorClient ) DeleteDashboard (ctx context.Context , ID int ) error {
70- res , err := client .doSysdigMonitorRequest (ctx , http .MethodDelete , client .getDashboardUrl (ID ), nil )
68+ response , err := client .doSysdigMonitorRequest (ctx , http .MethodDelete , client .getDashboardUrl (ID ), nil )
7169 if err != nil {
7270 return err
7371 }
74- defer res .Body .Close ()
75-
76- if res .StatusCode != http .StatusOK && res .StatusCode != http .StatusNoContent {
77- body , err := ioutil .ReadAll (res .Body )
78- if err != nil {
79- return err
80- }
72+ defer response .Body .Close ()
8173
82- return fmt .Errorf (string (body ))
74+ if response .StatusCode != http .StatusNoContent && response .StatusCode != http .StatusOK {
75+ return errorFromResponse (response )
8376 }
8477
8578 return nil
0 commit comments