Documentation
¶
Overview ¶
Package r2 is a rewrite of the request package that eschews fluent apis in favor of the options pattern. It is meant to be feature equivalent to request.
Index ¶
- Constants
- type CertInfo
- type Defaults
- type Event
- type EventOption
- type OnRequestListener
- type OnResponseListener
- type Option
- func OptBasicAuth(username, password string) Option
- func OptBody(contents io.ReadCloser) Option
- func OptContext(ctx context.Context) Option
- func OptCookie(cookie *http.Cookie) Option
- func OptCookieValue(name, value string) Option
- func OptDelete() Option
- func OptGet() Option
- func OptHeader(headers http.Header) Option
- func OptHeaderValue(key, value string) Option
- func OptJSONBody(obj interface{}) Option
- func OptLogRequest(log logger.Log) Option
- func OptLogResponse(log logger.Log) Option
- func OptLogResponseWithBody(log logger.Log) Option
- func OptMethod(method string) Option
- func OptOnRequest(listener OnRequestListener) Option
- func OptOnResponse(listener OnResponseListener) Option
- func OptPatch() Option
- func OptPost() Option
- func OptPostForm(postForm url.Values) Option
- func OptPostFormValue(key, value string) Option
- func OptPut() Option
- func OptQuery(query url.Values) Option
- func OptQueryValue(key, value string) Option
- func OptReadLimit(byteCount int64) Option
- func OptResponseBodyInterceptor(interceptor ReaderInterceptor) Option
- func OptResponseHeaderTimeout(d time.Duration) Option
- func OptTLSClientCert(cert, key []byte) Option
- func OptTLSClientConfig(cfg *tls.Config) Option
- func OptTLSHandshakeTimeout(d time.Duration) Option
- func OptTLSRootCAs(pool *x509.CertPool) Option
- func OptTimeout(d time.Duration) Option
- func OptTracer(tracer Tracer) Option
- func OptTransport(transport http.RoundTripper) Option
- func OptXMLBody(obj interface{}) Option
- type ReadCloser
- type ReaderInterceptor
- type Request
- func (r *Request) Bytes() ([]byte, error)
- func (r *Request) Close() (*http.Response, error)
- func (r *Request) CopyTo(dst io.Writer) (int64, error)
- func (r *Request) Discard() error
- func (r *Request) Do() (*http.Response, error)
- func (r *Request) JSON(dst interface{}) error
- func (r *Request) String() (string, error)
- func (r *Request) XML(dst interface{}) error
- type TraceFinisher
- type Tracer
Constants ¶
const ( // MethodGet is a method. MethodGet = "GET" // MethodPost is a method. MethodPost = "POST" // MethodPut is a method. MethodPut = "PUT" // MethodPatch is a method. MethodPatch = "PATCH" // MethodDelete is a method. MethodDelete = "DELETE" // MethodOptions is a method. MethodOptions = "OPTIONS" )
const ( // HeaderConnection is a http header. HeaderConnection = "Connection" // HeaderContentType is a http header. HeaderContentType = "Content-Type" )
const ( // ContentTypeApplicationJSON is a content type header value. ContentTypeApplicationJSON = "application/json; charset=utf-8" // ContentTypeApplicationXML is a content type header value. ContentTypeApplicationXML = "application/xml" // ContentTypeApplicationFormEncoded is a content type header value. ContentTypeApplicationFormEncoded = "application/x-www-form-urlencoded" // ContentTypeApplicationOctetStream is a content type header value. ContentTypeApplicationOctetStream = "application/octet-stream" )
const ( // Flag is a logger event flag. Flag logger.Flag = "request" // FlagResponse is a logger event flag. FlagResponse logger.Flag = "request.response" )
const (
// ConnectionKeepAlive is a connection header value.
ConnectionKeepAlive = "keep-alive"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CertInfo ¶
type CertInfo struct {
IssuerCommonName string `json:"issuerCommonName" yaml:"issuerCommonName"`
DNSNames []string `json:"dnsNames" yaml:"dnsNames"`
NotAfter time.Time `json:"notAfter" yaml:"notAfter"`
NotBefore time.Time `json:"notBefore" yaml:"notBefore"`
}
CertInfo is the information for a certificate.
func ParseCertInfo ¶
ParseCertInfo returns a new cert info from a response.
type Defaults ¶
type Defaults []Option
Defaults is a helper to create requests with a common set of base options.
func (Defaults) ConcatWith ¶
ConcatWith concats the options with a given set of new options for output.
type Event ¶
type Event struct {
*logger.EventMeta
// Started is the time the request was started.
// It is used for elapsed time calculations.
Started time.Time
// The request metadata.
Request *http.Request
// The response metadata (excluding the body).
Response *http.Response
// The response body.
Body []byte
}
Event is a response to outgoing requests.
func NewEvent ¶
func NewEvent(flag logger.Flag, options ...EventOption) *Event
NewEvent returns a new event.
type EventOption ¶
type EventOption func(e *Event)
EventOption is an event option.
func OptEventCompleted ¶
func OptEventCompleted(ts time.Time) EventOption
OptEventCompleted sets the event completed time.
func OptEventFlag ¶
func OptEventFlag(flag logger.Flag) EventOption
OptEventFlag sets the event flag.
func OptEventRequest ¶
func OptEventRequest(req *http.Request) EventOption
OptEventRequest sets the response.
func OptEventResponse ¶
func OptEventResponse(res *http.Response) EventOption
OptEventResponse sets the response.
func OptEventStarted ¶
func OptEventStarted(ts time.Time) EventOption
OptEventStarted sets the start time.
type OnRequestListener ¶
OnRequestListener is an a listener for on request events.
type OnResponseListener ¶
OnResponseListener is an on response listener.
type Option ¶
Option is a modifier for a request.
func OptBasicAuth ¶
OptBasicAuth is an option that sets the http basic auth.
func OptBody ¶
func OptBody(contents io.ReadCloser) Option
OptBody sets the post body on the request.
func OptCookieValue ¶
OptCookieValue adds a cookie with a given name and value.
func OptHeaderValue ¶
OptHeaderValue adds or sets a header value.
func OptJSONBody ¶
func OptJSONBody(obj interface{}) Option
OptJSONBody sets the post body on the request.
func OptLogRequest ¶
OptLogRequest adds an OnResponse listener to log the response of a call.
func OptLogResponse ¶
OptLogResponse adds an OnResponse listener to log the response of a call.
func OptLogResponseWithBody ¶
OptLogResponseWithBody adds an OnResponse listener to log the response of a call. It reads the contents of the response fully before emitting the event. Do not use this if the size of the responses can be large.
func OptOnRequest ¶
func OptOnRequest(listener OnRequestListener) Option
OptOnRequest sets an on request listener.
func OptOnResponse ¶
func OptOnResponse(listener OnResponseListener) Option
OptOnResponse adds an on response listener. If an OnResponse listener has already been addded, it will be merged with the existing listener.
func OptPostForm ¶
OptPostForm sets the request post form and the content type.
func OptPostFormValue ¶
OptPostFormValue sets a request post form value.
func OptQueryValue ¶
OptQueryValue adds or sets a query value.
func OptReadLimit ¶
OptReadLimit applies an `io.LimitReader` to a request's response.
func OptResponseBodyInterceptor ¶
func OptResponseBodyInterceptor(interceptor ReaderInterceptor) Option
OptResponseBodyInterceptor sets the response reader on the request. This should be used to do things that modify how we read the response.
func OptResponseHeaderTimeout ¶
OptResponseHeaderTimeout sets the client transport ResponseHeaderTimeout.
func OptTLSClientCert ¶
OptTLSClientCert adds a client cert and key to the request.
func OptTLSClientConfig ¶
OptTLSClientConfig sets the tls config for the request. It will create a client, and a transport if unset.
func OptTLSHandshakeTimeout ¶
OptTLSHandshakeTimeout sets the client transport TLSHandshakeTimeout.
func OptTLSRootCAs ¶
OptTLSRootCAs sets the client tls root ca pool.
func OptTransport ¶
func OptTransport(transport http.RoundTripper) Option
OptTransport sets the client transport for a request.
func OptXMLBody ¶
func OptXMLBody(obj interface{}) Option
OptXMLBody sets the post body on the request.
type ReadCloser ¶
ReadCloser allows you to split a ReaderCloser up into separate components. This lets use apply `io.LimitReader` and the like to response bodies, but preserve the original Close functionality.
func NewReadCloser ¶
func NewReadCloser(body io.ReadCloser, interceptor ReaderInterceptor) *ReadCloser
NewReadCloser creates a new read closer from a given body and interceptor.
type ReaderInterceptor ¶
ReaderInterceptor is a handler for request.ReadResponse
type Request ¶
type Request struct {
*http.Request
Client *http.Client
// Err is an error set on construction.
// It pre-empts the request going out.
Err error
// ResponseBodyInterceptor is an optional custom step to alter the response stream.
ResponseBodyInterceptor ReaderInterceptor
Tracer Tracer
// OnRequest and OnResponse are lifecycle hooks.
OnRequest []OnRequestListener
OnResponse []OnResponseListener
}
Request is a combination of the http.Request options and the underlying client.
func (*Request) Close ¶
Close executes and closes the response. It returns the response for metadata purposes. It does not read any data from the response.
type TraceFinisher ¶
TraceFinisher is a finisher for traces.
type Tracer ¶
type Tracer interface {
Start(*http.Request) TraceFinisher
}
Tracer is a tracer for requests.
Source Files
¶
- cert_info.go
- constants.go
- defaults.go
- event.go
- event_option.go
- opt_basic_auth.go
- opt_body.go
- opt_context.go
- opt_cookie.go
- opt_header.go
- opt_json_body.go
- opt_log.go
- opt_method.go
- opt_on_request.go
- opt_on_response.go
- opt_post_form.go
- opt_query.go
- opt_read_limit.go
- opt_response_body_interceptor.go
- opt_response_header_timeout.go
- opt_timeout.go
- opt_tls_client_cert.go
- opt_tls_client_config.go
- opt_tls_handshake_timeout.go
- opt_tls_root_cas.go
- opt_tracer.go
- opt_transport.go
- opt_xml_body.go
- option.go
- package.go
- read_closer.go
- reader_interceptor.go
- request.go
- tracer.go