task_tracker/test/common.go
2019-01-29 19:24:12 -05:00

84 lines
1.6 KiB
Go

package test
import (
"bytes"
"crypto"
"crypto/hmac"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"src/task_tracker/config"
"src/task_tracker/storage"
"strconv"
)
func Post(path string, x interface{}, worker *storage.Worker) *http.Response {
body, err := json.Marshal(x)
buf := bytes.NewBuffer(body)
req, err := http.NewRequest("POST", "http://"+config.Cfg.ServerAddr+path, buf)
handleErr(err)
if worker != nil {
mac := hmac.New(crypto.SHA256.New, worker.Secret)
mac.Write(body)
sig := hex.EncodeToString(mac.Sum(nil))
req.Header.Add("X-Worker-Id", strconv.FormatInt(worker.Id, 10))
req.Header.Add("X-Signature", sig)
}
client := http.Client{}
r, err := client.Do(req)
handleErr(err)
return r
}
func Get(path string, worker *storage.Worker) *http.Response {
url := "http://" + config.Cfg.ServerAddr + path
req, err := http.NewRequest("GET", url, nil)
handleErr(err)
if worker != nil {
fmt.Println(worker.Secret)
mac := hmac.New(crypto.SHA256.New, worker.Secret)
mac.Write([]byte(path))
sig := hex.EncodeToString(mac.Sum(nil))
fmt.Println(strconv.FormatInt(worker.Id, 10))
req.Header.Add("X-Worker-Id", strconv.FormatInt(worker.Id, 10))
req.Header.Add("X-Signature", sig)
}
client := http.Client{}
r, err := client.Do(req)
handleErr(err)
return r
}
func handleErr(err error) {
if err != nil {
panic(err)
}
}
func GenericJson(body io.ReadCloser) map[string]interface{} {
var obj map[string]interface{}
data, _ := ioutil.ReadAll(body)
err := json.Unmarshal(data, &obj)
handleErr(err)
return obj
}