本文整理汇总了Golang中github.com/daohoangson/go-socialcounters/utils.Utils.HttpClient方法的典型用法代码示例。如果您正苦于以下问题:Golang Utils.HttpClient方法的具体用法?Golang Utils.HttpClient怎么用?Golang Utils.HttpClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/daohoangson/go-socialcounters/utils.Utils
的用法示例。
在下文中一共展示了Utils.HttpClient方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: googleLegacy
func googleLegacy(u utils.Utils, url string) result {
start := time.Now()
var res result
urlJson, err := json.Marshal(url)
if err != nil {
res.Error = err
return res
}
// http://bradsknutson.com/blog/get-google-share-count-url/
body := `[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":` + string(urlJson) + `,"source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]`
req, err := http.NewRequest("POST", "https://clients6.google.com/rpc", bytes.NewBufferString(body))
if err != nil {
res.Error = err
return res
}
utils.Verbosef(u, "Calling http.Client.Do(body=%s)", body)
req.Header.Set("Content-Type", "application/json")
resp, err := u.HttpClient().Do(req)
if err != nil {
res.Error = err
return res
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
res.Error = err
return res
}
res.Response = respBody
u.Debugf("googleLegacy(url=%s) took %s", url, time.Since(start))
jsonparser.ArrayEach(respBody, func(element []byte, _ jsonparser.ValueType, _ int, err error) {
if err != nil {
res.Error = err
return
}
if count, err := jsonparser.GetFloat(element, "result", "metadata", "globalCounts", "count"); err != nil {
res.Error = err
return
} else {
res.Count = int64(count)
}
})
return res
}
示例2: facebookWorker
func facebookWorker(u utils.Utils, req *request) {
start := time.Now()
urls := strings.Join(req.Urls, ",")
url := prepareFbGraphUrl(u, urls)
utils.Verbosef(u, "Calling http.Client.Get(%s)", url)
resp, err := u.HttpClient().Get(url)
if err != nil {
req.Error = err
return
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
req.Error = err
return
}
req.Response = respBody
u.Debugf("facebookWorker(urls=%s) took %s", urls, time.Since(start))
for _, url := range req.Urls {
var res result
if respUrl, _, _, err := jsonparser.Get(respBody, url); err != nil {
res.Error = err
res.Response = respBody
} else {
res.Response = respUrl
if shareCount, err := jsonparser.GetInt(respUrl, "share", "share_count"); err != nil {
// it's alright, for new urls Facebook does not return share.share_count at all
res.Count = COUNT_INITIAL_VALUE
} else {
res.Count = shareCount
}
}
req.Results[url] = res
}
return
}
示例3: twitterLegacy
func twitterLegacy(u utils.Utils, url string) result {
var res result
start := time.Now()
oscUrl := "http://opensharecount.com/count.json?url=" + neturl.QueryEscape(url)
utils.Verbosef(u, "Calling http.Client.Get(%s)", oscUrl)
resp, err := u.HttpClient().Get(oscUrl)
if err != nil {
res.Error = err
return res
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
res.Error = err
return res
}
res.Response = respBody
u.Debugf("twitterLegacy(url=%s) took %s", url, time.Since(start))
if count, err := jsonparser.GetInt(respBody, "count"); err != nil {
res.Error = err
return res
} else {
if count == 0 {
if oscError, err := jsonparser.GetString(respBody, "error"); err != nil {
res.Error = errors.New(oscError)
return res
}
}
res.Count = count
}
return res
}