本文整理汇总了Golang中google/golang.org/appengine/urlfetch.Client函数的典型用法代码示例。如果您正苦于以下问题:Golang Client函数的具体用法?Golang Client怎么用?Golang Client使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Client函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: init
// init is called before the application starts.
func init() {
m := martini.Classic()
m.Use(render.Renderer())
m.Use(func(res http.ResponseWriter, req *http.Request) {
authorization := &spark.Authorization{AccessToken: os.Getenv("SPARK_TOKEN")}
spark.InitClient(authorization)
ctx := appengine.NewContext(req)
spark.SetHttpClient(urlfetch.Client(ctx), ctx)
})
m.Post("/spark", binding.Json(SparkEvent{}), func(sparkEvent SparkEvent, res http.ResponseWriter, req *http.Request, r render.Render) {
ctx := appengine.NewContext(req)
client := urlfetch.Client(ctx)
message := spark.Message{ID: sparkEvent.Id}
message.Get()
log.Infof(ctx, message.Text)
if strings.HasPrefix(message.Text, "/") {
s := strings.Split(sparkEvent.Text, " ")
command := s[0]
log.Infof(ctx, "command = %s", command)
if command == "/routes" {
resp, _ := client.Get("http://galwaybus.herokuapp.com/routes.json")
defer resp.Body.Close()
contents, _ := ioutil.ReadAll(resp.Body)
log.Infof(ctx, "body = %s\n", contents)
var routeMap map[string]BusRoute
json.Unmarshal([]byte(contents), &routeMap)
text := "Routes:\n\n"
for _, route := range routeMap {
text = text + strconv.Itoa(route.Id) + " " + route.LongName + "\n"
}
message := spark.Message{
RoomID: sparkEvent.RoomId,
Text: text,
}
message.Post()
}
}
})
http.Handle("/", m)
}
开发者ID:joreilly,项目名称:GalwayBusSparkBot,代码行数:53,代码来源:main.go
示例2: init
func init() {
renderer := render.New(render.Options{})
token := os.Getenv("SLACK_TOKEN")
http.HandleFunc("/v1/cmd", func(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
req, err := goslash.ParseFormSlashCommandRequest(r)
if err != nil {
renderer.JSON(w, http.StatusInternalServerError, err.Error())
return
}
if req.Token != token {
log.Errorf(ctx, "received invalid token:%v from %v", req.Token, r.RemoteAddr)
renderer.JSON(w, http.StatusForbidden, "invalid token")
return
}
slashPlugins := map[string]plugins.Plugin{
"echo": echo.New(),
"time": time.New(),
"突然": suddendeath.New(),
"LGTM": lgtm.New(urlfetch.Client(ctx)),
"akari": akari.New(),
}
slashCmd := plugins.New(urlfetch.Client(ctx), slashPlugins)
if appengine.IsDevAppServer() {
// development
cmd, _ := req.CmdArgs()
p, ok := slashPlugins[cmd]
if !ok {
renderer.JSON(w, http.StatusNotFound, "cmd not found")
return
}
msg := p.Do(req)
var jsonData bytes.Buffer
if err := json.NewEncoder(&jsonData).Encode(&msg); err != nil {
renderer.JSON(w, http.StatusInternalServerError, err.Error())
return
}
renderer.JSON(w, http.StatusOK, jsonData.String())
} else {
// production
renderer.Text(w, http.StatusOK, slashCmd.Execute(req))
}
})
}
开发者ID:yhanada,项目名称:goslash-sample-gae,代码行数:51,代码来源:app.go
示例3: textMessageHandler
func textMessageHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
to := r.FormValue("to")
text := r.FormValue("text")
log.Debugf(ctx, "to: %s, text: %s.", to, text)
chi, _ := strconv.ParseInt(os.Getenv("CHANNEL_ID"), 10, 64)
chs := os.Getenv("CHANNEL_SECRET")
mid := os.Getenv("MID")
client := urlfetch.Client(ctx)
bot, err := linebot.NewClient(chi, chs, mid, linebot.WithHTTPClient(client))
if err != nil {
log.Errorf(ctx, "Client initialization failure. to: %s, text: %s, context: %s", to, text, err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
res, err := bot.SendText([]string{to}, text)
if err != nil {
log.Errorf(ctx, "Message Send Failed. to: %s, text: %s, context: %s", to, text, err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Infof(ctx, "Message send succeed. MessageID: %s, to: %s, text: %s", res.MessageID, to, text)
w.WriteHeader(http.StatusOK)
}
开发者ID:tksmaru,项目名称:line-gae-echo-bot,代码行数:29,代码来源:app.go
示例4: jobV2adsbHandler
func jobV2adsbHandler(r *http.Request, f *oldfdb.Flight) (string, error) {
ctx := req2ctx(r)
str := ""
// Allow overwrite, for the (36,0) disastery
// if f.HasTrack("ADSB") { return "", nil } // Already has one
err, deb := f.GetV2ADSBTrack(urlfetch.Client(ctx))
str += fmt.Sprintf("*getv2ADSB [%v]:-\n", err, deb)
if err != nil {
return str, err
}
if !f.HasTrack("ADSB") {
return "", nil
} // Didn't find one
f.Analyse() // Retrigger Class-B stuff
db := oldfgae.NewDB(r)
if err := db.UpdateFlight(*f); err != nil {
log.Errorf(ctx, "Persist Flight %s: %v", f, err)
return str, err
}
log.Infof(ctx, "Updated flight %s", f)
str += fmt.Sprintf("--\nFlight was updated\n")
return str, nil
}
开发者ID:skypies,项目名称:flightdb,代码行数:29,代码来源:flight-batch.go
示例5: registerFeed
func registerFeed(w http.ResponseWriter, r *http.Request) *appError {
s := strings.TrimSpace(r.FormValue("url"))
if s == "" {
return &appError{
Error: errInvalidRequest,
Message: "url is required",
Code: http.StatusBadRequest,
}
}
u, err := url.Parse(s)
if err != nil {
return &appError{
Error: err,
Message: "can't parse url",
Code: http.StatusBadRequest,
}
}
if !u.IsAbs() {
return &appError{
Error: errInvalidRequest,
Message: "require an absolute url",
Code: http.StatusBadRequest,
}
}
c := appengine.NewContext(r)
client := urlfetch.Client(c)
f, err := fetchFeed(client, s)
if err != nil {
return &appError{
Error: err,
Message: "failed to get",
Code: http.StatusInternalServerError,
}
}
m := Magazine{
Title: f.Title,
URL: s,
Creation: time.Now(),
LastMod: time.Now(),
}
m.Init(c)
usr := user.Current(c)
if usr == nil {
return &appError{
Error: errUserRejected,
Message: "failed to initialize user environment",
Code: http.StatusUnauthorized,
}
}
err = RegisterMagazine(c, usr, &m, f)
if err != nil {
return &appError{
Error: err,
Message: "failed to register",
Code: http.StatusInternalServerError,
}
}
http.Redirect(w, r, "/", http.StatusFound)
return nil
}
开发者ID:lufia,项目名称:magazine,代码行数:60,代码来源:main.go
示例6: activeUsersInChannel
// count active users in channel with channels.info then users.getPresence
// very slow due to network
func activeUsersInChannel(c context.Context, channelId string) (users []string, err error) {
bot := bot.WithClient(urlfetch.Client(c))
members, err := bot.ChannelsInfo(channelId)
l.Infof(c, "check %v", members)
active := make(chan string, len(members))
var wg sync.WaitGroup
for i := range members {
wg.Add(1)
go func(user string, active chan string, wg *sync.WaitGroup) {
defer wg.Done()
l.Infof(c, "begin "+user)
if p, err := bot.UsersGetPresence(user); err != nil {
l.Errorf(c, "%s", err)
return
} else if p == "active" {
active <- user
}
l.Infof(c, "done "+user)
}(members[i], active, &wg)
}
wg.Wait()
l.Infof(c, "done wait")
close(active)
users = make([]string, len(members))
for user := range active {
users = append(users, user)
}
return
}
开发者ID:hsluo,项目名称:slack-bot,代码行数:31,代码来源:vote.go
示例7: SendSimpleMessageHandler
func SendSimpleMessageHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
httpc := urlfetch.Client(ctx)
mg := mailgun.NewMailgun(
"YOUR_DOMAIN_NAME", // Domain name
"YOUR_API_KEY", // API Key
"YOUR_PUBLIC_KEY", // Public Key
)
mg.SetClient(httpc)
msg, id, err := mg.Send(mg.NewMessage(
/* From */ "Excited User <[email protected]_DOMAIN_NAME>",
/* Subject */ "Hello",
/* Body */ "Testing some Mailgun awesomness!",
/* To */ "[email protected]", "[email protected]_DOMAIN_NAME",
))
if err != nil {
msg := fmt.Sprintf("Could not send message: %v, ID %d, %+v", err, id, msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
w.Write([]byte("Message sent!"))
}
开发者ID:wuman,项目名称:golang-samples,代码行数:25,代码来源:mailgun.go
示例8: getAccessToken
func getAccessToken(ctx context.Context, state, code string) (string, error) {
values := make(url.Values)
values.Add("client_id", "vmqwbq2tj8l57k9")
values.Add("client_secret", "rpcztciueyunhba")
values.Add("grant_type", "authorization_code")
values.Add("code", code)
values.Add("redirect_uri", redirectURI)
client := urlfetch.Client(ctx)
response, err := client.PostForm("https://api.dropbox.com/1/oauth2/token", values)
if err != nil {
return "", err
}
defer response.Body.Close()
bs, _ := ioutil.ReadAll(response.Body)
var data struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
UID string `json:"uid"`
}
err = json.Unmarshal(bs, &data)
if err != nil {
return "", err
}
if data.AccessToken == "" {
return "", fmt.Errorf("Invalid Access Token:%s", string(bs))
}
return data.AccessToken, nil
}
开发者ID:robertsturner52,项目名称:oauth,代码行数:31,代码来源:login.go
示例9: authCallbackHandler
func authCallbackHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
c := NewTwitterClient(ctx)
token := r.FormValue("oauth_token")
verificationCode := r.FormValue("oauth_verifier")
log.Debugf(ctx, "oauth_token:%v oauth_verifier:%v", token, verificationCode)
requestToken := FindRequest(ctx, token)
if requestToken == nil {
fmt.Fprint(w, "token not found")
w.WriteHeader(500)
return
}
accessToken, err := c.AuthorizeToken(requestToken, verificationCode)
if err != nil {
fmt.Fprint(w, err)
w.WriteHeader(500)
return
}
fmt.Fprintln(w, accessToken.AdditionalData)
api := anaconda.NewTwitterApi(accessToken.Token, accessToken.Secret)
api.HttpClient = urlfetch.Client(ctx)
ok, err := api.VerifyCredentials()
if err != nil {
fmt.Fprint(w, err)
w.WriteHeader(500)
return
}
fmt.Fprintln(w, ok)
}
开发者ID:neguse,项目名称:webapp-gae,代码行数:29,代码来源:hello.go
示例10: updateFromSource2ForAppEngine
func updateFromSource2ForAppEngine(r *http.Request) (int64, bool, error) {
var result int64
ctx := appengine.NewContext(r)
client := urlfetch.Client(ctx)
resp, err := client.Get(source2Url)
if err != nil {
return 0, false, err
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
if resp.StatusCode == 200 {
var rate entities.Rate2Response
dec.Decode(&rate)
result = rate.TimestampUnix
repo := repository.New(repoSize, true, r)
if err := repo.Push(entities.Rate{
Base: rate.Base,
ID: rate.TimestampUnix,
RUB: rate.Quotes["USDRUB"],
JPY: rate.Quotes["USDJPY"],
GBP: rate.Quotes["USDGBP"],
USD: rate.Quotes["USDUSD"],
EUR: rate.Quotes["USDEUR"],
CNY: rate.Quotes["USDCNY"],
CHF: rate.Quotes["USDCHF"]}); err != nil {
return 0, false, fmt.Errorf("Push rate to repo error: %v", err)
}
} else {
var err2Resp entities.Error2Response
dec.Decode(&err2Resp)
return 0, false, fmt.Errorf(err2Resp.ToString())
}
return result, true, nil
}
开发者ID:rutmir,项目名称:pr.optima,代码行数:35,代码来源:fetchRatesJob.go
示例11: notify
func notify(ctx context.Context, title, body string, to []string) error {
b, err := json.Marshal(PostData{
Notification: Notification{
Title: title,
Body: body,
Icon: "ic_battery_alert_black",
Tag: "battery_low",
},
RegistrationIds: to,
CollapseKey: "battery_low",
})
if err != nil {
return err
}
req, err := http.NewRequest("POST", "https://gcm-http.googleapis.com/gcm/send", bytes.NewReader(b))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "key="+keys.GcmApplicationKey)
c := urlfetch.Client(ctx)
resp, err := c.Do(req)
if err != nil {
return err
}
log.Print(resp)
return nil
}
开发者ID:hota911,项目名称:BatteryMonitorApp,代码行数:33,代码来源:notification.go
示例12: serveTwitterCallback
// serveTwitterCallback handles callbacks from the Twitter OAuth server.
func serveTwitterCallback(c *context) error {
token := c.r.FormValue("oauth_token")
var ci connectInfo
_, err := memcache.Gob.Get(c.c, token, &ci)
if err != nil {
return err
}
memcache.Delete(c.c, token)
tempCred := &oauth.Credentials{
Token: token,
Secret: ci.Secret,
}
httpClient := urlfetch.Client(c.c)
tokenCred, _, err := oauthClient.RequestToken(httpClient, tempCred, c.r.FormValue("oauth_verifier"))
if err != nil {
return err
}
if err := c.updateUserInfo(func(u *userInfo) { u.TwitterCred = *tokenCred }); err != nil {
return err
}
http.Redirect(c.w, c.r, ci.Redirect, 302)
return nil
}
开发者ID:garycarr,项目名称:go-oauth,代码行数:26,代码来源:app.go
示例13: createAppengineStripeClient
//createAppendingeStripeClient creates an httpclient on a per-request basis for use in making api calls to Stripe
//stripe's api is accessed via http requests, need a way to make these requests
//urlfetch is the appengine way of making http requests
//this func returns an httpclient *per request* aka per request to this app
//otherwise one request could use another requests httpclient
//this is for app engine only since the golang http.DefaultClient is unavailable
func createAppengineStripeClient(c context.Context) *client.API {
//create http client
httpClient := urlfetch.Client(c)
//returns "sc" stripe client
return client.New(stripePrivateKey, stripe.NewBackends(httpClient))
}
开发者ID:coreymgilmore,项目名称:stripe-appengine-frontend,代码行数:13,代码来源:card.go
示例14: downloadAccessToken
func downloadAccessToken(c context.Context, url string, auth *osin.BasicAuth, output map[string]interface{}) error {
// download access token
log.Infof(c, "Downloading token from %v", url)
preq, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
if auth != nil {
preq.SetBasicAuth(auth.Username, auth.Password)
}
pclient := urlfetch.Client(c)
presp, err := pclient.Do(preq)
if err != nil {
return err
}
if presp.StatusCode != 200 {
return errors.New("Invalid status code")
}
jdec := json.NewDecoder(presp.Body)
err = jdec.Decode(&output)
return err
}
开发者ID:imtheoperator,项目名称:osin-datastore,代码行数:26,代码来源:main.go
示例15: giffy
func giffy(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
t := req.FormValue("term") + "gaming"
client := urlfetch.Client(ctx)
result, err := client.Get("http://api.giphy.com/v1/gifs/search?q=" + t + "&api_key=dc6zaTOxFJmzC")
if err != nil {
http.Error(res, err.Error(), 500)
return
}
defer result.Body.Close()
var obj struct {
Data []struct {
URL string `json:"url"`
Images struct {
Original struct {
URL string
}
}
}
}
err = json.NewDecoder(result.Body).Decode(&obj)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
for _, img := range obj.Data {
fmt.Fprintf(res, `<a href="%v">%v</a><img src="%v"><br>`, img.URL, img.URL, img.Images.Original.URL)
}
cookie := genCookie(res, req)
m := Model(cookie)
tpl.ExecuteTemplate(res, "gifs.html", m)
}
开发者ID:yash0690,项目名称:Golang,代码行数:34,代码来源:main.go
示例16: SendComplexMessageHandler
func SendComplexMessageHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
httpc := urlfetch.Client(ctx)
mg := mailgun.NewMailgun(
"YOUR_DOMAIN_NAME", // Domain name
"YOUR_API_KEY", // API Key
"YOUR_PUBLIC_KEY", // Public Key
)
mg.SetClient(httpc)
message := mg.NewMessage(
/* From */ "Excited User <[email protected]_DOMAIN_NAME>",
/* Subject */ "Hello",
/* Body */ "Testing some Mailgun awesomness!",
/* To */ "[email protected]",
)
message.AddCC("[email protected]")
message.AddBCC("[email protected]")
message.SetHtml("<html>HTML version of the body</html>")
message.AddAttachment("files/test.jpg")
message.AddAttachment("files/test.txt")
msg, id, err := mg.Send(message)
if err != nil {
msg := fmt.Sprintf("Could not send message: %v, ID %d, %+v", err, id, msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
w.Write([]byte("Message sent!"))
}
开发者ID:wuman,项目名称:golang-samples,代码行数:32,代码来源:mailgun.go
示例17: makeRequest
func (session *Session) makeRequest(url string, params Params) ([]byte, error) {
buf := &bytes.Buffer{}
mime, err := params.Encode(buf)
if err != nil {
return nil, fmt.Errorf("cannot encode params. %v", err)
}
client := urlfetch.Client(session.context)
//HACK: http://stackoverflow.com/questions/10523906/sslcertificateerror-the-handshake-operation-timed-out-when-trying-to-get-an
transport := client.Transport.(*urlfetch.Transport)
transport.AllowInvalidServerCertificate = true
response, err := client.Post(url, mime, buf)
if err != nil {
return nil, fmt.Errorf("cannot reach facebook server. %v", err)
}
defer response.Body.Close()
buf = &bytes.Buffer{}
_, err = io.Copy(buf, response.Body)
if err != nil {
return nil, fmt.Errorf("cannot read facebook response. %v", err)
}
return buf.Bytes(), nil
}
开发者ID:taviti,项目名称:facebook,代码行数:31,代码来源:session.go
示例18: GetMap
func (calculations *Calculations) GetMap(r *http.Request) (string, error) {
var mapCountries = []string{}
var mapColors = []string{"DBDCDD"}
for _, country := range calculations.CountryShare.Countries {
mapCountries = append(mapCountries, country.Code)
mapColors = append(mapColors, MAP_HIGHLIGHTED_COLOR)
}
urlParams := []string{MAP_URL,
"chld=" + strings.Join(mapCountries, "|"),
"chco=" + strings.Join(mapColors, "|"),
}
ctx := newappengine.NewContext(r)
client := urlfetch.Client(ctx)
resp, err := client.Get(strings.Join(urlParams, "&"))
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
result := base64.StdEncoding.EncodeToString(body)
result = "data:image/png;base64," + result
return result, nil
}
开发者ID:uve,项目名称:localization.expert,代码行数:30,代码来源:charts.go
示例19: handleGetGif
func handleGetGif(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
t := req.FormValue("term")
if t == "" {
t = "funny+cat"
}
client := urlfetch.Client(ctx)
result, err := client.Get("http://api.giphy.com/v1/gifs/search?q=" + t + "&api_key=dc6zaTOxFJmzC")
if err != nil {
http.Error(res, err.Error(), 500)
return
}
defer result.Body.Close()
var obj struct {
Data []struct {
URL string `json:"url"`
Images struct {
Original struct {
URL string
}
}
}
}
err = json.NewDecoder(result.Body).Decode(&obj)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
for _, img := range obj.Data {
fmt.Fprintf(res, `<a href="%v">%v</a><img src="%v"><br>`, img.URL, img.URL, img.Images.Original.URL)
}
}
开发者ID:GoesToEleven,项目名称:golang-web,代码行数:34,代码来源:http.go
示例20: chargeAccount
func chargeAccount(ctx context.Context, stripeToken string) error {
// because we're on app engine, use a custom http client
b := stripe.BackendConfiguration{
stripe.APIBackend,
"https://api.stripe.com/v1",
urlfetch.Client(ctx),
}
id, _ := uuid.NewV4()
for {
chargeParams := &stripe.ChargeParams{
Amount: 100 * 200000,
Currency: "usd",
Desc: "Charge for [email protected]",
}
chargeParams.IdempotencyKey = id.String()
chargeParams.SetSource(stripeToken)
chargeClient := &charge.Client{
Key: stripe.Key,
B: b,
}
ch, err := chargeClient.New(chargeParams)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
time.Sleep(time.Second)
continue
}
return err
}
log.Infof(ctx, "CHARGE: %v", ch)
return nil
}
}
开发者ID:RobertoSuarez,项目名称:GolangTraining,代码行数:34,代码来源:stripe.go
注:本文中的google/golang.org/appengine/urlfetch.Client函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论