github.com/coveo/gotemplate@v2.7.7+incompatible/template/extra_net.go (about)

     1  package template
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/PuerkitoBio/goquery"
     8  )
     9  
    10  const (
    11  	netBase = "Net"
    12  )
    13  
    14  var netFuncs = dictionary{
    15  	"httpGet": httpGet,
    16  	"httpDoc": httpDocument,
    17  }
    18  
    19  var netFuncsArgs = arguments{
    20  	"httpGet": {"url"},
    21  	"httpDoc": {"url"},
    22  }
    23  
    24  var netFuncsAliases = aliases{
    25  	"httpDoc": {"httpDocument", "curl"},
    26  }
    27  
    28  var netFuncsHelp = descriptions{
    29  	"httpGet": "Returns http get response from supplied URL.",
    30  	"httpDoc": "Returns http document returned by supplied URL.",
    31  }
    32  
    33  func (t *Template) addNetFuncs() {
    34  	t.AddFunctions(netFuncs, netBase, FuncOptions{
    35  		FuncHelp:    netFuncsHelp,
    36  		FuncArgs:    netFuncsArgs,
    37  		FuncAliases: netFuncsAliases,
    38  	})
    39  }
    40  
    41  func httpGet(url interface{}) (*http.Response, error) {
    42  	return http.Get(fmt.Sprint(url))
    43  }
    44  
    45  func httpDocument(url interface{}) (interface{}, error) {
    46  	response, err := httpGet(url)
    47  	if err != nil {
    48  		return response, err
    49  	}
    50  	return goquery.NewDocumentFromResponse(response)
    51  }