Chromium Code Reviews| Index: buildbucket/client/cmd/buildbucket/client.go |
| diff --git a/buildbucket/client/cmd/buildbucket/client.go b/buildbucket/client/cmd/buildbucket/client.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..92905e9c3d763cb2ac18e16892c02cb92891b2f8 |
| --- /dev/null |
| +++ b/buildbucket/client/cmd/buildbucket/client.go |
| @@ -0,0 +1,78 @@ |
| +// Copyright 2016 The LUCI Authors. All rights reserved. |
| +// Use of this source code is governed under the Apache License, Version 2.0 |
| +// that can be found in the LICENSE file. |
| + |
| +package main |
| + |
| +import ( |
| + "bytes" |
| + "encoding/json" |
| + "fmt" |
| + "io/ioutil" |
| + "net/http" |
| + "net/url" |
| + "time" |
| + |
| + "golang.org/x/net/context" |
| + |
| + "github.com/luci/luci-go/common/errors" |
| + "github.com/luci/luci-go/common/logging" |
| + "github.com/luci/luci-go/common/retry" |
| +) |
| + |
| +type client struct { |
| + HTTP *http.Client |
| + baseURL *url.URL |
| +} |
| + |
| +// call makes an HTTP call with exponential back-off on transient errors. |
| +// if urlStr is relative, rebases it on c.baseURL. |
| +func (c *client) call(ctx context.Context, method, urlStr string, body interface{}) (response []byte, err error) { |
| + if !c.baseURL.IsAbs() { |
| + panic("baseURL is not absolute") |
| + } |
| + |
| + var bodyBytes []byte |
| + if body != nil { |
| + var err error |
| + if bodyBytes, err = json.Marshal(body); err != nil { |
| + return nil, err |
| + } |
| + } |
| + |
| + u, err := url.Parse(urlStr) |
| + if err != nil { |
| + return nil, fmt.Errorf("invalid urlStr: %s", err) |
| + } |
| + u = c.baseURL.ResolveReference(u) |
| + logging.Infof(ctx, "%s %s", method, u) |
| + |
| + err = retry.Retry( |
| + ctx, |
| + retry.TransientOnly(retry.Default), |
| + func() error { |
| + req := &http.Request{Method: method, URL: u} |
| + if bodyBytes != nil { |
| + req.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes)) |
| + } |
| + res, err := c.HTTP.Do(req) |
|
Vadim Sh.
2016/08/09 19:09:23
use ctxhttp.Do(ctx, c.HTTP, req) to support contex
nodir
2016/08/09 22:56:31
oops
|
| + if err != nil { |
| + return errors.WrapTransient(err) |
| + } |
| + |
| + defer res.Body.Close() |
| + if res.StatusCode >= 500 { |
| + bodyBytes, _ := ioutil.ReadAll(res.Body) |
| + return errors.WrapTransient(fmt.Errorf( |
| + "status %s: %s", res.Status, bodyBytes)) |
| + } |
| + |
| + response, err = ioutil.ReadAll(res.Body) |
| + return err |
| + }, |
| + func(err error, wait time.Duration) { |
| + logging.WithError(err).Warningf(ctx, "API request failed transiently, will retry in %s", wait) |
| + }, |
| + ) |
| + return |
| +} |