Newer
Older
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
var builder *strings.Builder
func init() {
builder = &strings.Builder{}
}
// HTTPGet sends sends requests from the CLI to the gosdn HTTP API and processes any response data
func HTTPGet(apiEndpoint, f string, args ...string) error {
for _, p := range args {
builder.WriteString("&")
builder.WriteString(p)
}
resp, err := http.Get(apiEndpoint + apiRoot + "q=" + f + builder.String())
if err != nil {
return err
}
builder.Reset()
switch resp.StatusCode {
case http.StatusOK:
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
pnd := string(bytes[9:45])
sbi := string(bytes[55:91])
viper.Set("CLI_PND", pnd)
viper.Set("CLI_SBI", sbi)
case http.StatusCreated:
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
uuid := string(bytes[19:55])
viper.Set("LAST_DEVICE_UUID", uuid)
default:
log.WithFields(log.Fields{
"status code": resp.StatusCode,
}).Error("operation unsuccessful")