luci: go.chromium.org/luci/common/lhttp Index | Files

package lhttp

import "go.chromium.org/luci/common/lhttp"

Package lhttp implements HTTP client helper code (JSON, automatic retries, authentication, etc).

'l' stands for luci.

Index

Package Files

client.go doc.go utils.go

func CheckURL Uses

func CheckURL(s string) (string, error)

CheckURL ensures that the URL has a valid scheme, and that, if it is an appspot server, that it uses HTTPS.

If no protocol is specified, the protocol defaults to https://.

func GetJSON Uses

func GetJSON(ctx context.Context, rFn retry.Factory, c *http.Client, url string, out interface{}) (int, error)

GetJSON is a shorthand. It returns the HTTP status code and error if any.

func IsHTTPError Uses

func IsHTTPError(err error) (status int, ok bool)

func IsLocalHost Uses

func IsLocalHost(hostport string) bool

IsLocalHost returns true if hostport is local.

func NewRequest Uses

func NewRequest(ctx context.Context, c *http.Client, rFn retry.Factory, rgen RequestGen,
    handler Handler, errorHandler ErrorHandler) func() (int, error)

NewRequest returns a retriable request.

The handler func is responsible for closing the response Body before returning. It should return retry.Error in case of retriable error, for example if a TCP connection is terminated while receiving the content.

If rFn is nil, NewRequest will use a default exponential backoff strategy only for transient errors.

If errorHandler is nil, the default error handler will drain and close the response body.

func NewRequestJSON Uses

func NewRequestJSON(ctx context.Context, c *http.Client, rFn retry.Factory, url, method string, headers map[string]string, in, out interface{}) (func() (int, error), error)

NewRequestJSON returns a retriable request calling a JSON endpoint.

func PostJSON Uses

func PostJSON(ctx context.Context, rFn retry.Factory, c *http.Client, url string, headers map[string]string, in, out interface{}) (int, error)

PostJSON is a shorthand. It returns the HTTP status code and error if any.

type ErrorHandler Uses

type ErrorHandler func(resp *http.Response, err error) error

ErrorHandler is called once or multiple times for each HTTP request that is tried. It is called when any non-200 response code is received, or if some other network error occurs. resp may be nil if a network error occurred before the response was received. The ErrorHandler must close the provided resp, if any. Return the same error again to continue retry behaviour, or nil to pretend this error was a success.

type Handler Uses

type Handler func(*http.Response) error

Handler is called once or multiple times for each HTTP request that is tried.

type RequestGen Uses

type RequestGen func() (*http.Request, error)

RequestGen is a generator function to create a new request. It may be called multiple times if an operation needs to be retried. The HTTP server is responsible for closing the Request body, as per http.Request Body method documentation.

Package lhttp imports 15 packages (graph) and is imported by 13 packages. Updated 2018-10-19. Refresh now. Tools for package owners.