2020-05-15 09:39:16 +00:00
|
|
|
package cwapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ctx *ApiCtx) Enroll(userID string) error {
|
|
|
|
toPush := map[string]string{"user_id": userID}
|
2020-07-27 10:18:55 +00:00
|
|
|
jsonResp := &ApiResp{}
|
2020-07-30 13:30:02 +00:00
|
|
|
errResp := &ApiResp{}
|
2020-05-15 09:39:16 +00:00
|
|
|
|
2020-07-30 13:30:02 +00:00
|
|
|
resp, err := ctx.Http.New().Post(ctx.EnrollPath).BodyJSON(&toPush).Receive(jsonResp, errResp)
|
2020-05-15 09:39:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("api enroll: HTTP request creation failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
2020-07-30 13:30:02 +00:00
|
|
|
return fmt.Errorf("api enroll: user '%s' return bad HTTP code (%d): %s", userID, resp.StatusCode, errResp.Message)
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
2020-07-27 10:18:55 +00:00
|
|
|
if jsonResp.Message == "" || jsonResp.Message != "OK" || jsonResp.StatusCode != 200 {
|
2020-07-27 16:35:31 +00:00
|
|
|
return fmt.Errorf("api user enroll failed")
|
2020-07-27 10:18:55 +00:00
|
|
|
}
|
2020-05-15 09:39:16 +00:00
|
|
|
log.Printf("user '%s' is enrolled successfully", string(userID))
|
|
|
|
return nil
|
|
|
|
}
|