158 lines
4.5 KiB
Go
158 lines
4.5 KiB
Go
package main
|
|
|
|
import "net/http"
|
|
import "io/ioutil"
|
|
import "log"
|
|
import "bytes"
|
|
|
|
import "github.com/tidwall/gjson"
|
|
import pb "git.wit.com/wit/witProtobuf"
|
|
|
|
func fetchGET(token string, a string) []byte {
|
|
return fetchJSON("GET", token, a)
|
|
}
|
|
|
|
func fetchPOST(token string, a string) []byte {
|
|
return fetchJSON("POST", token, a)
|
|
}
|
|
|
|
func fetchJSON(GETorPOST string, token string, a string) []byte {
|
|
log.Println("GOT HERE: fetchJSON(): ", GETorPOST + ": " + a)
|
|
req, err := http.NewRequest(token, a, nil)
|
|
if err != nil {
|
|
log.Fatal("can't make new req")
|
|
}
|
|
|
|
// always set X-Wit-Auth even on token request
|
|
if (token == "") {
|
|
token = "badtoken"
|
|
}
|
|
req.Header.Set("X-Wit-Auth", token)
|
|
|
|
// needed to allow 'CORS' to work
|
|
req.Header.Set("Access-Control-Allow-Origin", "*")
|
|
req.Header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
|
req.Header.Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
|
|
|
log.Println("GOT HERE: fetchJSON(): token =", token)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Println("Can't exec the req to list networks: %s", err)
|
|
}
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Fatal("can't read resp")
|
|
}
|
|
log.Println("GOT HERE: fetchJSON() body =", string(body))
|
|
return body
|
|
}
|
|
|
|
func updateToken(ge *pb.Event) bool {
|
|
if ge == nil {
|
|
return false
|
|
}
|
|
|
|
url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password
|
|
json := newFetchBody(ge, "POST", url)
|
|
|
|
jwt := gjson.Get(json, "jwt")
|
|
if (jwt.String() == "") {
|
|
ge.Comment = "updateToken() GOT TOKEN == nil"
|
|
log.Println("updateToken() ge.Comment =", ge.Comment)
|
|
return false
|
|
}
|
|
if (jwt.String() != ge.Account.Token) {
|
|
log.Println("updateToken() GOT NEW TOKEN", jwt)
|
|
ge.Account.Token = jwt.String()
|
|
}
|
|
log.Println("updateToken() END")
|
|
return true
|
|
}
|
|
|
|
// url := ge.Account.URL + "/auth/login" + "?email=" + ge.Account.Email + "&password=" + ge.Account.Password
|
|
|
|
func newFetchBody(ge *pb.Event, POSTorGET string, url string) string {
|
|
if ge == nil {
|
|
log.Println("newFetchBody() has ge = nil")
|
|
ge.Comment = "newFetchBody() has ge = nil"
|
|
return ""
|
|
}
|
|
if ge.Account == nil {
|
|
log.Println("newFetchBody() has ge.Account = nil")
|
|
ge.Comment = "newFetchBody() has ge.Account = nil"
|
|
return ""
|
|
}
|
|
|
|
log.Println("newFetchBody()", POSTorGET, "URL =", url)
|
|
|
|
var jsonStr = []byte(`{}`)
|
|
req, err := http.NewRequest(POSTorGET, url, bytes.NewBuffer(jsonStr))
|
|
// req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-Wit-Auth", "tokenrequest")
|
|
|
|
if (POSTorGET == "POST") {
|
|
log.Println("newFetchBody()", POSTorGET, "ADDING CORS headers")
|
|
// needed to allow 'CORS' to work
|
|
req.Header.Set("Access-Control-Allow-Origin", "*")
|
|
req.Header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
|
req.Header.Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
|
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11")
|
|
}
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Println(err)
|
|
ge.Comment = err.Error()
|
|
return ""
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
log.Println("newFetchBody() response Body:", string(body))
|
|
log.Println("newFetchBody() response Status:", resp.Status)
|
|
log.Println("newFetchBody() response Headers:", resp.Header)
|
|
|
|
return string(body)
|
|
}
|
|
|
|
func GETbody(ge *pb.Event, URL string) string {
|
|
// req, err := http.NewRequest("GET", ge.Account.URL + "clusters", nil)
|
|
req, err := http.NewRequest("GET", URL, nil)
|
|
if err != nil {
|
|
log.Println("GETbody() can't make new req")
|
|
return ""
|
|
}
|
|
req.Header.Set("X-Wit-Auth", ge.Account.Token)
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Printf("Can't exec the req to list networks: %s", err)
|
|
return ""
|
|
}
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Println("can't read resp")
|
|
return ""
|
|
}
|
|
// log.Printf("GETbody() body =", string(body))
|
|
if body == nil {
|
|
log.Println("networks is nil")
|
|
return ""
|
|
}
|
|
/*
|
|
len := len(body)
|
|
err = json.Unmarshal(body, &networks)
|
|
if err != nil {
|
|
log.Println("body = ", body)
|
|
log.Println("body len = ", len)
|
|
log.Println("can't unmarshal resp for list networks: %s", err)
|
|
}
|
|
*/
|
|
// spew.Dump(networks)
|
|
return string(body)
|
|
}
|