Documentation ¶
Index ¶
- Constants
- Variables
- func DeserializeReaderAsJSON(object interface{}, body io.ReadCloser) error
- func GetHost(r *http.Request) string
- func GetProto(r *http.Request) string
- func GetRemoteAddr(r *http.Request) string
- func GetUserAgent(r *http.Request) string
- func LocalIP() string
- func MustParseURL(rawURL string) *url.URL
- func NewMockRequest(method, path string) *http.Request
- func NewMockRequestWithCookie(method, path, cookieName, cookieValue string) *http.Request
- func PortFromBindAddr(bindAddr string) (port int32)
- func URLWithHost(u *url.URL, host string) *url.URL
- func URLWithPath(u *url.URL, path string) *url.URL
- func URLWithPort(u *url.URL, port string) *url.URL
- func URLWithQuery(u *url.URL, key, value string) *url.URL
- func URLWithRawQuery(u *url.URL, rawQuery string) *url.URL
- func URLWithScheme(u *url.URL, scheme string) *url.URL
- func WriteJSON(w http.ResponseWriter, statusCode int, response interface{}) error
- func WriteNoContent(w http.ResponseWriter) error
- func WriteRawContent(w http.ResponseWriter, statusCode int, content []byte) error
- func WriteXML(w http.ResponseWriter, statusCode int, response interface{}) error
- type MockResponseWriter
- func (res *MockResponseWriter) Bytes() []byte
- func (res *MockResponseWriter) Close() error
- func (res *MockResponseWriter) ContentLength() int
- func (res *MockResponseWriter) Flush() error
- func (res *MockResponseWriter) Header() http.Header
- func (res *MockResponseWriter) InnerResponse() http.ResponseWriter
- func (res *MockResponseWriter) StatusCode() int
- func (res *MockResponseWriter) Write(buffer []byte) (int, error)
- func (res *MockResponseWriter) WriteHeader(statusCode int)
- type RequestSender
- func (rs *RequestSender) Client() *http.Client
- func (rs *RequestSender) Close() bool
- func (rs *RequestSender) Headers() http.Header
- func (rs *RequestSender) Method() string
- func (rs *RequestSender) Send() (*http.Response, error)
- func (rs *RequestSender) SendBytes(ctx context.Context, contents []byte) (*http.Response, error)
- func (rs *RequestSender) SendJSON(ctx context.Context, contents interface{}) (*http.Response, error)
- func (rs *RequestSender) Tracer() RequestTracer
- func (rs *RequestSender) Transport() *http.Transport
- func (rs *RequestSender) WithClose(close bool) *RequestSender
- func (rs *RequestSender) WithHeader(key, value string) *RequestSender
- func (rs *RequestSender) WithHeaders(headers http.Header) *RequestSender
- func (rs *RequestSender) WithMethod(method string) *RequestSender
- func (rs *RequestSender) WithTracer(tracer RequestTracer) *RequestSender
- func (rs *RequestSender) WithTransport(transport *http.Transport) *RequestSender
- type RequestTraceFinisher
- type RequestTracer
Constants ¶
const ( // HeaderAcceptEncoding is the "Accept-Encoding" header. // It indicates what types of encodings the request will accept responses as. // It typically enables or disables compressed (gzipped) responses. HeaderAcceptEncoding = "Accept-Encoding" // HeaderSetCookie is the header that sets cookies in a response. HeaderSetCookie = "Set-Cookie" // HeaderCookie is the request cookie header. HeaderCookie = "Cookie" // HeaderDate is the "Date" header. // It provides a timestamp the response was generated at. // It is typically used by client cache control to invalidate expired items. HeaderDate = "Date" // HeaderCacheControl is the "Cache-Control" header. // It indicates if and how clients should cache responses. // Typical values for this include "no-cache", "max-age", "min-fresh", and "max-stale" variants. HeaderCacheControl = "Cache-Control" // HeaderConnection is the "Connection" header. // It is used to indicate if the connection should remain open by the server // after the final response bytes are sent. // This allows the connection to be re-used, helping mitigate connection negotiation // penalites in making requests. HeaderConnection = "Connection" // HeaderContentEncoding is the "Content-Encoding" header. // It is used to indicate what the response encoding is. // Typical values are "gzip", "deflate", "compress", "br", and "identity" indicating no compression. HeaderContentEncoding = "Content-Encoding" // HeaderContentLength is the "Content-Length" header. // If provided, it specifies the size of the request or response. HeaderContentLength = "Content-Length" // HeaderContentType is the "Content-Type" header. // It specifies the MIME-type of the request or response. HeaderContentType = "Content-Type" // HeaderUserAgent is the user agent header. // It typically indicates what is making the request. HeaderUserAgent = "User-Agent" // HeaderServer is the "Server" header. // It is an informational header to tell the client what server software was used. HeaderServer = "Server" // HeaderVary is the "Vary" header. // It is used to indicate what fields should be used by the client as cache keys. HeaderVary = "Vary" // HeaderXServedBy is the "X-Served-By" header. // It is an informational header that indicates what software was used to generate the response. HeaderXServedBy = "X-Served-By" // HeaderXFrameOptions is the "X-Frame-Options" header. // It indicates if a browser is allowed to render the response in a <frame> element or not. HeaderXFrameOptions = "X-Frame-Options" // HeaderXXSSProtection is the "X-Xss-Protection" header. // It is a feature of internet explorer, and indicates if the browser should allow // requests across domain boundaries. HeaderXXSSProtection = "X-Xss-Protection" // HeaderXContentTypeOptions is the "X-Content-Type-Options" header. HeaderXContentTypeOptions = "X-Content-Type-Options" // HeaderStrictTransportSecurity is the hsts header. HeaderStrictTransportSecurity = "Strict-Transport-Security" // ContentTypeApplicationJSON is a content type for JSON responses. // We specify chartset=utf-8 so that clients know to use the UTF-8 string encoding. ContentTypeApplicationJSON = "application/json; charset=UTF-8" // ContentTypeHTML is a content type for html responses. // We specify chartset=utf-8 so that clients know to use the UTF-8 string encoding. ContentTypeHTML = "text/html; charset=utf-8" //ContentTypeXML is a content type for XML responses. // We specify chartset=utf-8 so that clients know to use the UTF-8 string encoding. ContentTypeXML = "text/xml; charset=utf-8" // ContentTypeText is a content type for text responses. // We specify chartset=utf-8 so that clients know to use the UTF-8 string encoding. ContentTypeText = "text/plain; charset=utf-8" // ConnectionKeepAlive is a value for the "Connection" header and // indicates the server should keep the tcp connection open // after the last byte of the response is sent. ConnectionKeepAlive = "keep-alive" // ContentEncodingIdentity is the identity (uncompressed) content encoding. ContentEncodingIdentity = "identity" // ContentEncodingGZIP is the gzip (compressed) content encoding. ContentEncodingGZIP = "gzip" )
Variables ¶
var ( // ErrURLUnset is a (hopefully) uncommon error. ErrURLUnset = exception.Class("request url unset") // DefaultRequestTimeout is the default webhook timeout. DefaultRequestTimeout = 10 * time.Second // DefaultRequestMethod is the default webhook method. DefaultRequestMethod = "POST" )
Functions ¶
func DeserializeReaderAsJSON ¶
func DeserializeReaderAsJSON(object interface{}, body io.ReadCloser) error
DeserializeReaderAsJSON deserializes a post body as json to a given object.
func GetProto ¶
GetProto gets the request proto. X-FORWARDED-PROTO is checked first, then the original request proto is used.
func GetRemoteAddr ¶
GetRemoteAddr gets the origin/client ip for a request. X-FORWARDED-FOR is checked. If multiple IPs are included the first one is returned X-REAL-IP is checked. If multiple IPs are included the first one is returned Finally r.RemoteAddr is used Only benevolent services will allow access to the real IP.
func GetUserAgent ¶
GetUserAgent gets a user agent from a request.
func MustParseURL ¶
MustParseURL parses a url and panics if there is an error.
func NewMockRequest ¶
NewMockRequest creates a mock request.
func NewMockRequestWithCookie ¶
NewMockRequestWithCookie creates a mock request with a cookie attached to it.
func PortFromBindAddr ¶
PortFromBindAddr returns a port number as an integer from a bind addr.
func URLWithHost ¶
URLWithHost returns a copy url with a given host.
func URLWithPath ¶
URLWithPath returns a copy url with a given path.
func URLWithPort ¶ added in v0.2.0
URLWithPort returns a copy url with a given pprt attached to the host.
func URLWithQuery ¶ added in v0.2.0
URLWithQuery returns a copy url with a given raw query.
func URLWithRawQuery ¶
URLWithRawQuery returns a copy url with a given raw query.
func URLWithScheme ¶
URLWithScheme returns a copy url with a given scheme.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, statusCode int, response interface{}) error
WriteJSON marshalls an object to json.
func WriteNoContent ¶
func WriteNoContent(w http.ResponseWriter) error
WriteNoContent writes http.StatusNoContent for a request.
func WriteRawContent ¶
func WriteRawContent(w http.ResponseWriter, statusCode int, content []byte) error
WriteRawContent writes raw content for the request.
Types ¶
type MockResponseWriter ¶
type MockResponseWriter struct {
// contains filtered or unexported fields
}
MockResponseWriter is an object that satisfies response writer but uses an internal buffer.
func NewMockResponse ¶
func NewMockResponse(buffer io.Writer) *MockResponseWriter
NewMockResponse returns a mocked response writer.
func (*MockResponseWriter) Bytes ¶
func (res *MockResponseWriter) Bytes() []byte
Bytes returns the raw response.
func (*MockResponseWriter) ContentLength ¶
func (res *MockResponseWriter) ContentLength() int
ContentLength returns the content length.
func (*MockResponseWriter) Header ¶
func (res *MockResponseWriter) Header() http.Header
Header returns the response headers.
func (*MockResponseWriter) InnerResponse ¶
func (res *MockResponseWriter) InnerResponse() http.ResponseWriter
InnerResponse returns the backing httpresponse writer.
func (*MockResponseWriter) StatusCode ¶
func (res *MockResponseWriter) StatusCode() int
StatusCode returns the status code.
func (*MockResponseWriter) Write ¶
func (res *MockResponseWriter) Write(buffer []byte) (int, error)
Write writes data and adds to ContentLength.
func (*MockResponseWriter) WriteHeader ¶
func (res *MockResponseWriter) WriteHeader(statusCode int)
WriteHeader sets the status code.
type RequestSender ¶
RequestSender is a slack webhook sender.
func NewRequestSender ¶
func NewRequestSender(destination *url.URL) *RequestSender
NewRequestSender creates a new request sender.
A request sender is a sepcialized request factory that makes request to a single endpoint.
It is useful when calling out to predefined things like webhooks.
You can send either raw bytes as the contents.
func (*RequestSender) Client ¶
func (rs *RequestSender) Client() *http.Client
Client returns the underlying client.
func (*RequestSender) Close ¶
func (rs *RequestSender) Close() bool
Close returns if we should close the connection.
func (*RequestSender) Headers ¶
func (rs *RequestSender) Headers() http.Header
Headers returns the headers.
func (*RequestSender) Method ¶
func (rs *RequestSender) Method() string
Method is the request method. It defaults to "POST".
func (*RequestSender) Send ¶
func (rs *RequestSender) Send() (*http.Response, error)
Send sends a request to the destination without a payload.
func (*RequestSender) SendBytes ¶
SendBytes sends a message to the webhook with a given msg body as raw bytes.
func (*RequestSender) SendJSON ¶
func (rs *RequestSender) SendJSON(ctx context.Context, contents interface{}) (*http.Response, error)
SendJSON sends a message to the webhook with a given msg body as json.
func (*RequestSender) Tracer ¶
func (rs *RequestSender) Tracer() RequestTracer
Tracer returns the request tracer.
func (*RequestSender) Transport ¶
func (rs *RequestSender) Transport() *http.Transport
Transport returns the transport.
func (*RequestSender) WithClose ¶
func (rs *RequestSender) WithClose(close bool) *RequestSender
WithClose sets if we should close the connection.
func (*RequestSender) WithHeader ¶
func (rs *RequestSender) WithHeader(key, value string) *RequestSender
WithHeader adds an individual header.
func (*RequestSender) WithHeaders ¶
func (rs *RequestSender) WithHeaders(headers http.Header) *RequestSender
WithHeaders sets headers.
func (*RequestSender) WithMethod ¶
func (rs *RequestSender) WithMethod(method string) *RequestSender
WithMethod sets the request method (defaults to POST).
func (*RequestSender) WithTracer ¶
func (rs *RequestSender) WithTracer(tracer RequestTracer) *RequestSender
WithTracer sets the request tracer.
func (*RequestSender) WithTransport ¶
func (rs *RequestSender) WithTransport(transport *http.Transport) *RequestSender
WithTransport sets the transport.
type RequestTraceFinisher ¶
RequestTraceFinisher is a finisher for request traces.
type RequestTracer ¶
type RequestTracer interface {
Start(*http.Request) RequestTraceFinisher
}
RequestTracer is a tracer for requests.