yoake/internal/servetpl/funcmap/http.go

53 lines
1.1 KiB
Go
Raw Normal View History

2022-11-11 16:15:22 -06:00
package funcmap
import (
2022-11-20 12:05:14 -06:00
"io"
2022-11-11 16:15:22 -06:00
"net/http"
"github.com/PuerkitoBio/goquery"
)
const (
ResponseTypeHTML = "html"
ResponseTypeStrippedHTML = "html_stripped"
ResponseTypeText = "text"
)
func HttpRequest(method string, URL string, selector string, responseType string) (data interface{}, err error) {
if method == "" {
method = http.MethodGet
}
if responseType == "" {
responseType = ResponseTypeHTML
}
req, err := http.NewRequest(method, URL, nil)
if err != nil {
return nil, err
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if responseType == ResponseTypeHTML || responseType == ResponseTypeStrippedHTML {
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
result := doc.Contents()
if selector != "" {
result = result.Find(selector)
}
if responseType == ResponseTypeStrippedHTML {
return result.Text(), nil
}
return result.Html()
}
2022-11-20 12:05:14 -06:00
response, err := io.ReadAll(resp.Body)
2022-11-11 16:15:22 -06:00
if err != nil {
return nil, err
}
return string(response), nil
}