improved graph/workflow retrieval
This commit is contained in:
53
models/http.go
Normal file
53
models/http.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type HttpQuery struct {
|
||||
baseurl string
|
||||
jar http.CookieJar
|
||||
Cookies map[string]string
|
||||
}
|
||||
|
||||
func (h *HttpQuery) Init(url string) {
|
||||
h.baseurl = url
|
||||
h.jar, _ = cookiejar.New(nil)
|
||||
h.Cookies = make(map[string]string)
|
||||
}
|
||||
|
||||
func (h *HttpQuery) Get(url string) ([]byte, error) {
|
||||
client := &http.Client{Jar: h.jar}
|
||||
resp, err := client.Get(h.baseurl + url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// store received cookies
|
||||
for _, cookie := range h.jar.Cookies(resp.Request.URL) {
|
||||
h.Cookies[cookie.Name] = cookie.Value
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func (h *HttpQuery) Post(url string, data url.Values) (*http.Response, error) {
|
||||
client := &http.Client{Jar: h.jar}
|
||||
resp, err := client.PostForm(h.baseurl+url, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// store received cookies
|
||||
for _, cookie := range h.jar.Cookies(resp.Request.URL) {
|
||||
h.Cookies[cookie.Name] = cookie.Value
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
Reference in New Issue
Block a user