mirror of
https://github.com/simon987/task_tracker.git
synced 2025-12-14 07:19:02 +00:00
added optional task unique field
This commit is contained in:
@@ -2,26 +2,61 @@ 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"
|
||||
)
|
||||
|
||||
func Post(path string, x interface{}) *http.Response {
|
||||
func Post(path string, x interface{}, worker *storage.Worker) *http.Response {
|
||||
|
||||
body, err := json.Marshal(x)
|
||||
buf := bytes.NewBuffer(body)
|
||||
|
||||
r, err := http.Post("http://"+config.Cfg.ServerAddr+path, "application/json", buf)
|
||||
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", worker.Id.String())
|
||||
req.Header.Add("X-Signature", sig)
|
||||
}
|
||||
|
||||
client := http.Client{}
|
||||
r, err := client.Do(req)
|
||||
handleErr(err)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func Get(path string) *http.Response {
|
||||
r, err := http.Get("http://" + config.Cfg.ServerAddr + path)
|
||||
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))
|
||||
|
||||
req.Header.Add("X-Worker-Id", worker.Id.String())
|
||||
req.Header.Add("X-Signature", sig)
|
||||
}
|
||||
|
||||
client := http.Client{}
|
||||
r, err := client.Do(req)
|
||||
handleErr(err)
|
||||
|
||||
return r
|
||||
|
||||
Reference in New Issue
Block a user