Skip to content

Commit

Permalink
improve logging (#164)
Browse files Browse the repository at this point in the history
Co-authored-by: AlteredCoder <AlteredCoder>
  • Loading branch information
AlteredCoder authored Jul 30, 2020
1 parent f8abb01 commit d23512e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
17 changes: 10 additions & 7 deletions pkg/cwapi/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,18 @@ func (ctx *ApiCtx) Signin() error {
return fmt.Errorf("api signin: missing credentials in api.yaml")
}
jsonResp := &ApiResp{}
errResp := &ApiResp{}

resp, err := ctx.Http.New().Post(ctx.SigninPath).BodyJSON(ctx.Creds).ReceiveSuccess(jsonResp)
resp, err := ctx.Http.New().Post(ctx.SigninPath).BodyJSON(ctx.Creds).Receive(jsonResp, errResp)
if err != nil {
return fmt.Errorf("api signin: HTTP request creation failed: %s", err)
}

if resp.StatusCode != 200 {
return fmt.Errorf("api signin: return bad HTTP code (%d)", resp.StatusCode)
return fmt.Errorf("api signin: return bad HTTP code (%d): %s", resp.StatusCode, errResp.Message)
}
if jsonResp.Message == "" || jsonResp.StatusCode != 200 {
return fmt.Errorf("api signin failed. http response")
return fmt.Errorf("api signin failed")
}

ctx.Http = ctx.Http.Set("Authorization", jsonResp.Message)
Expand All @@ -164,14 +165,15 @@ func (ctx *ApiCtx) RegisterMachine(machineID string, password string) error {
ctx.Creds.User = machineID
ctx.Creds.Password = password
jsonResp := &ApiResp{}
errResp := &ApiResp{}

resp, err := ctx.Http.New().Post(ctx.RegisterPath).BodyJSON(ctx.Creds).ReceiveSuccess(jsonResp)
resp, err := ctx.Http.New().Post(ctx.RegisterPath).BodyJSON(ctx.Creds).Receive(jsonResp, errResp)
if err != nil {
return fmt.Errorf("api register machine: HTTP request creation failed: %s", err)
}

if resp.StatusCode != 200 {
return fmt.Errorf("api register machine: return bad HTTP code (%d)", resp.StatusCode)
return fmt.Errorf("api register machine: return bad HTTP code (%d) : %s", resp.StatusCode, errResp.Message)
}

if jsonResp.Message == "" || jsonResp.Message != "OK" || jsonResp.StatusCode != 200 {
Expand All @@ -184,15 +186,16 @@ func (ctx *ApiCtx) ResetPassword(machineID string, password string) error {
ctx.Creds.User = machineID
ctx.Creds.Password = password
jsonResp := &ApiResp{}
errResp := &ApiResp{}

data := map[string]string{"machine_id": ctx.Creds.User, "password": ctx.Creds.Password}
resp, err := ctx.Http.New().Post(ctx.ResetPwdPath).BodyJSON(data).ReceiveSuccess(jsonResp)
resp, err := ctx.Http.New().Post(ctx.ResetPwdPath).BodyJSON(data).Receive(jsonResp, errResp)
if err != nil {
return fmt.Errorf("api reset password: HTTP request creation failed: %s", err)
}

if resp.StatusCode != 200 {
return fmt.Errorf("api reset password: return bad HTTP code (%d)", resp.StatusCode)
return fmt.Errorf("api reset password: return bad HTTP code (%d): %s", resp.StatusCode, errResp.Message)
}

if jsonResp.Message == "" || jsonResp.StatusCode != 200 {
Expand Down
5 changes: 3 additions & 2 deletions pkg/cwapi/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
func (ctx *ApiCtx) Enroll(userID string) error {
toPush := map[string]string{"user_id": userID}
jsonResp := &ApiResp{}
errResp := &ApiResp{}

resp, err := ctx.Http.New().Post(ctx.EnrollPath).BodyJSON(&toPush).ReceiveSuccess(jsonResp)
resp, err := ctx.Http.New().Post(ctx.EnrollPath).BodyJSON(&toPush).Receive(jsonResp, errResp)
if err != nil {
return fmt.Errorf("api enroll: HTTP request creation failed: %s", err)
}

if resp.StatusCode != 200 {
return fmt.Errorf("api enroll: user '%s' return bad HTTP code (%d)", userID, resp.StatusCode)
return fmt.Errorf("api enroll: user '%s' return bad HTTP code (%d): %s", userID, resp.StatusCode, errResp.Message)
}
if jsonResp.Message == "" || jsonResp.Message != "OK" || jsonResp.StatusCode != 200 {
return fmt.Errorf("api user enroll failed")
Expand Down
6 changes: 4 additions & 2 deletions pkg/cwapi/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (

func (ctx *ApiCtx) PullTop() ([]map[string]string, error) {
top := &PullResp{}
resp, err := ctx.Http.New().Get(ctx.PullPath).ReceiveSuccess(top)
errResp := &ApiResp{}

resp, err := ctx.Http.New().Get(ctx.PullPath).Receive(top, errResp)
if err != nil {
return nil, fmt.Errorf("api pull: HTTP request creation failed: %s", err)
}

if resp.StatusCode != 200 {
return nil, fmt.Errorf("api pull: return bad HTTP code (%d)", resp.StatusCode)
return nil, fmt.Errorf("api pull: return bad HTTP code (%d): %s", resp.StatusCode, errResp.Message)
}

log.Debugf("api pull: response : %+v", top.Body)
Expand Down
5 changes: 3 additions & 2 deletions pkg/cwapi/signals.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func (ctx *ApiCtx) pushSignals() error {
return nil
}
jsonResp := &ApiResp{}
resp, err := ctx.Http.New().Put(ctx.PushPath).BodyJSON(&ctx.toPush).ReceiveSuccess(jsonResp)
errResp := &ApiResp{}
resp, err := ctx.Http.New().Put(ctx.PushPath).BodyJSON(&ctx.toPush).Receive(jsonResp, errResp)
if err != nil {
return fmt.Errorf("api push signal: HTTP request creation failed: %s", err)
}
Expand All @@ -41,7 +42,7 @@ 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)", resp.StatusCode)
return fmt.Errorf("api push signal: return bad HTTP code (%d): %s", resp.StatusCode, errResp.Message)
}
}

Expand Down

0 comments on commit d23512e

Please sign in to comment.