This commit is contained in:
AlteredCoder 2020-07-27 18:35:31 +02:00
parent 00ee2d0fdc
commit 55d5b6842c
4 changed files with 12 additions and 48 deletions

View file

@ -147,17 +147,11 @@ func (ctx *ApiCtx) Signin() error {
return fmt.Errorf("api signin: HTTP request creation failed: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("api signin: unable to read API response body: '%s'", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("api signin: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
return fmt.Errorf("api signin: return bad HTTP code (%d)", resp.StatusCode)
}
if jsonResp.Message == "" || jsonResp.StatusCode != 200 {
return fmt.Errorf("api signin failed. http response: %s", body)
return fmt.Errorf("api signin failed. http response")
}
ctx.Http = ctx.Http.Set("Authorization", jsonResp.Message)
@ -174,18 +168,13 @@ func (ctx *ApiCtx) RegisterMachine(machineID string, password string) error {
if err != nil {
return fmt.Errorf("api register machine: HTTP request creation failed: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("api register machine: unable to read API response body: %s", err.Error())
}
if resp.StatusCode != 200 {
return fmt.Errorf("api register machine: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
return fmt.Errorf("api register machine: return bad HTTP code (%d)", resp.StatusCode)
}
if jsonResp.Message == "" || jsonResp.Message != "OK" || jsonResp.StatusCode != 200 {
return fmt.Errorf("api signin failed. http response: %s", body)
return fmt.Errorf("api register machine failed")
}
return nil
}
@ -201,19 +190,12 @@ func (ctx *ApiCtx) ResetPassword(machineID string, password string) error {
return fmt.Errorf("api reset password: HTTP request creation failed: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("api reset password: unable to read API response body: %s", err.Error())
}
if resp.StatusCode != 200 {
return fmt.Errorf("api reset password: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
return fmt.Errorf("api reset password: return bad HTTP code (%d)", resp.StatusCode)
}
if jsonResp.Message == "" || jsonResp.StatusCode != 200 {
return fmt.Errorf("api signin failed. http response: %s", body)
return fmt.Errorf("api reset password failed")
}
return nil
}

View file

@ -2,7 +2,6 @@ package cwapi
import (
"fmt"
"io/ioutil"
log "github.com/sirupsen/logrus"
)
@ -15,17 +14,12 @@ func (ctx *ApiCtx) Enroll(userID string) error {
if err != nil {
return fmt.Errorf("api enroll: HTTP request creation failed: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("api enroll: unable to read API response body: '%s'", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("api enroll: user '%s' return bad HTTP code (%d): %s", userID, resp.StatusCode, string(body))
return fmt.Errorf("api enroll: user '%s' return bad HTTP code (%d)", userID, resp.StatusCode)
}
if jsonResp.Message == "" || jsonResp.Message != "OK" || jsonResp.StatusCode != 200 {
return fmt.Errorf("api user enroll failed. http response: %s", body)
return fmt.Errorf("api user enroll failed")
}
log.Printf("user '%s' is enrolled successfully", string(userID))
return nil

View file

@ -2,7 +2,6 @@ package cwapi
import (
"fmt"
"io/ioutil"
log "github.com/sirupsen/logrus"
)
@ -13,15 +12,9 @@ func (ctx *ApiCtx) PullTop() ([]map[string]string, error) {
if err != nil {
return nil, fmt.Errorf("api pull: HTTP request creation failed: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("api pull: unable to read API response body: '%s'", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("api pull: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
return nil, fmt.Errorf("api pull: return bad HTTP code (%d)", resp.StatusCode)
}
log.Debugf("api pull: response : %+v", top.Body)

View file

@ -26,12 +26,7 @@ func (ctx *ApiCtx) pushSignals() error {
if err != nil {
return fmt.Errorf("api push signal: HTTP request creation failed: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read body : %s", err)
}
log.Debugf("api push signal: HTTP Code: %+v | Body: %s \n", resp.StatusCode, string(body))
log.Debugf("api push signal: HTTP Code: %+v | Body: %s \n", resp.StatusCode)
if resp.StatusCode != 200 {
if resp.StatusCode == 401 && !ctx.tokenExpired {
log.Printf("api push signal: expired token, resigning to API")
@ -46,12 +41,12 @@ func (ctx *ApiCtx) pushSignals() error {
return fmt.Errorf("api push signal: unable to renew api session token: %s", err.Error())
}
} else {
return fmt.Errorf("api push signal: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
return fmt.Errorf("api push signal: return bad HTTP code (%d): %s", resp.StatusCode)
}
}
if resp.StatusCode != 401 && (jsonResp.Message == "" || jsonResp.Message != "OK" || jsonResp.StatusCode != 200) {
return fmt.Errorf("api push failed. http response: %s", body)
return fmt.Errorf("api push failed")
}
if len(ctx.toPush) > 0 {