httputilmore

package
v0.26.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2021 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderAccept                       = "Accept"
	HeaderAccessControlAllowHeaders    = "Access-Control-Allow-Headers"
	HeaderAccessControlAllowMethods    = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowOrigin     = "Access-Control-Allow-Origin"
	HeaderAccessControlRequestHeaders  = "Access-Control-Request-Headers"
	HeaderAccessControlRequestMethod   = "Access-Control-Request-Method"
	HeaderAuthorization                = "Authorization"
	HeaderCacheControl                 = "Cache-Control"
	HeaderContentDisposition           = "Content-Disposition"
	HeaderContentEncoding              = "Content-Encoding"
	HeaderContentLanguage              = "Content-Language"
	HeaderContentLength                = "Content-Length"
	HeaderContentMD5                   = "Content-MD5"
	HeaderContentTransferEncoding      = "Content-Transfer-Encoding"
	HeaderContentType                  = "Content-Type"
	HeaderDate                         = "Date"
	HeaderIfMatch                      = "If-Match"
	HeaderIfModifiedSince              = "If-Modified-Since"
	HeaderIfNoneMatch                  = "If-None-Match"
	HeaderIfUnmodifiedSince            = "If-Unmodified-Since"
	HeaderLocation                     = "Location"
	HeaderRange                        = "Range"
	HeaderUserAgent                    = "User-Agent"
	HeaderXContentTypeOptions          = "X-Content-Type-Options"
	ContentTypeAppJson                 = "application/json"
	ContentTypeAppJsonUtf8             = "application/json; charset=utf-8"
	ContentTypeAppFormUrlEncoded       = "application/x-www-form-urlencoded"
	ContentTypeAppXml                  = "application/xml"
	ContentTypeAppXmlUtf8              = "application/xml; charset=utf-8"
	ContentTypeTextCalendarUtf8Request = `text/calendar; charset="utf-8"; method=REQUEST`
	ContentTypeTextHtml                = "text/html"
	ContentTypeTextHtmlUtf8            = "text/html; charset=utf-8"
	ContentTypeTextMarkdown            = "text/markdown"
	ContentTypeTextPlain               = "text/plain"
	ContentTypeTextPlainUsAscii        = "text/plain; charset=us-ascii"
	ContentTypeTextPlainUtf8           = "text/plain; charset=utf-8"
	ContentTypeTextXmlUtf8             = "text/xml; charset=utf-8"
	SchemeHTTPS                        = "https"
)

Constants ensuring that header names are correctly spelled and consistently cased.

View Source
const (
	MethodConnect HTTPMethod = http.MethodConnect
	MethodDelete             = http.MethodDelete
	MethodGet                = http.MethodGet
	MethodHead               = http.MethodHead
	MethodOptions            = http.MethodOptions
	MethodPatch              = http.MethodPatch
	MethodPost               = http.MethodPost
	MethodPut                = http.MethodPut
	MethodTrace              = http.MethodTrace
)

Variables

View Source
var (
	DIAL_TIMEOUT int64 = 5
	TLS_TIMEOUT  int   = 5
	HTTP_TIMEOUT int   = 10
)

Functions

func CondenseResponseNot2xxToError added in v0.0.7

func CondenseResponseNot2xxToError(resp *http.Response, err error, msg string) error

func Delete

func Delete(client *http.Client, url string) (*http.Response, error)

func DoJSON

func DoJSON(client *http.Client, httpMethod, reqURL string, headers map[string]string, reqBody, resBody interface{}) ([]byte, *http.Response, error)

func DoJSONSimple

func DoJSONSimple(client *http.Client, httpMethod, requrl string, headers map[string]string, body []byte) (*http.Response, error)

func DoRequestRateLimited

func DoRequestRateLimited(client *http.Client, req *http.Request, useXrlHyphen bool, fnLog FnLogRateLimitInfo) (*http.Response, error)

DoRequestRateLimited will pause a request for the time specified in the HTTP response headers.

func GetJsonSimple

func GetJsonSimple(requrl string, header http.Header, data interface{}) (*http.Response, error)

func GetRequestRateLimited

func GetRequestRateLimited(client *http.Client, reqURL string, useXrlHyphen bool, fn FnLogRateLimitInfo) (*http.Response, error)

func GetResponseAndBytes

func GetResponseAndBytes(url string) (*http.Response, []byte, error)

GetResponseAndBytes retreives a URL and returns the response body as a byte array in addition to the *http.Response.

func GetWriteFile

func GetWriteFile(client *http.Client, url, filename string) (*http.Response, error)

GetWriteFile gets the conents of a URL and stores the body in the desired filename location.

func GetWriteFileSimple

func GetWriteFileSimple(url string, filename string, perm os.FileMode) ([]byte, error)

GetWriteFile performs a HTTP GET request and saves the response body to the file path specified. It reeads the entire file into memory which is not ideal for large files.

func Log

func Log(handler http.Handler) http.Handler

Log is a custom Http handler that will log all requests. It can be called using http.ListenAndServe(":8080", Log(http.DefaultServeMux)) From: https://groups.google.com/forum/#!topic/golang-nuts/s7Xk1q0LSU0

func LogRequestRateLimited

func LogRequestRateLimited(rlstat RateLimitInfo)

func MergeHeader

func MergeHeader(base, more http.Header, overwrite bool) http.Header

MergeHeader merges two http.Header adding the values of the second to the first.

func NewHeadersMSS

func NewHeadersMSS(headersMap map[string]string) http.Header

NewHeadersMSS returns a `http.Header` struct give a `map[string]string`

func NewHttpClient

func NewHttpClient() *http.Client

The default Go HTTP client never times out. This HTTP client provides default and updatable timeouts More at: https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779#.ymd655pgz

func ParseHeader

func ParseHeader(s string) http.Header

ParseHeader converts a raw strign to a header struct.

func ParseMultipartFormDataBoundaryFromHeader

func ParseMultipartFormDataBoundaryFromHeader(contentType string) string

func PrintRequestOut

func PrintRequestOut(req *http.Request, includeBody bool) error

PrintRequestOut prints a http.Request using `httputil.DumpRequestOut`.

func PrintResponse

func PrintResponse(resp *http.Response, includeBody bool) error

PrintResponse prints a http.Response using `httputil.DumpResponse`.

func ProxyResponse

func ProxyResponse(w http.ResponseWriter, resp *http.Response) ([]byte, error)

ProxyResponse copies the information from a `*http.Response` to a `http.ResponseWriter`.

func ResponseBodyJSONMapIndent

func ResponseBodyJSONMapIndent(res *http.Response, prefix string, indent string) ([]byte, error)

ResponseBodyJSONMapIndent returns the body as a generic JSON dictionary

func ResponseWriterWriteJSON

func ResponseWriterWriteJSON(w http.ResponseWriter, statusCode int, body interface{}, prefix, indent string)

func SendWwwFormUrlEncodedSimple

func SendWwwFormUrlEncodedSimple(method, urlStr string, data url.Values) (*http.Response, error)

Types

type ClientMore

type ClientMore struct {
	Client *http.Client
}

func (*ClientMore) PostToJSON

func (cm *ClientMore) PostToJSON(postURL string, body interface{}) (*http.Response, error)

type FnLogRateLimitInfo

type FnLogRateLimitInfo func(RateLimitInfo)

type HTTPError added in v0.0.8

type HTTPError struct {
	HTTPStatus int    `json:"httpStatus"`
	Stage      string `json:"preOpPost"`
	Message    string `json:"errorMessage"`
}

func NewHTTPError added in v0.0.8

func NewHTTPError(message string, httpStatus int, stage string) *HTTPError

func (*HTTPError) Bytes added in v0.0.8

func (httperr *HTTPError) Bytes() []byte

type HTTPMethod

type HTTPMethod string

func ParseHTTPMethod

func ParseHTTPMethod(method string) (HTTPMethod, error)

ParseHTTPMethod returns a HTTPMethod type for a string.

type RateLimitInfo

type RateLimitInfo struct {
	StatusCode          int
	RetryAfter          int
	XRateLimitLimit     int
	XRateLimitRemaining int
	XRateLimitReset     int
	XRateLimitWindow    int
}

RateLimitInfo is a structure for holding parsed rate limit info. It uses headers from the GitHub, RingCentral and Twitter APIs.

func NewResponseRateLimitInfo

func NewResponseRateLimitInfo(resp *http.Response, useXrlHyphen bool) RateLimitInfo

NewResponseRateLimitInfo returns a RateLimitInfo from a http.Response.

type ResponseInfo

type ResponseInfo struct {
	Name       string            `json:"name,omitempty"` // to distinguish from other requests
	Method     string            `json:"method,omitempty"`
	URL        string            `json:"url,omitempty"`
	StatusCode int               `json:"statusCode,omitempty"`
	Time       time.Time         `json:"time,omitempty"`
	Headers    map[string]string `json:"headers,omitempty"`
	Body       string            `json:"body,omitempty"`
}

ResponseInfo is a generic struct to handle response info.

func (*ResponseInfo) ToJSON

func (resIn *ResponseInfo) ToJSON() []byte

ToJSON returns ResponseInfo as a JSON byte array, embedding json.Marshal errors if encountered.

type TransportWithHeaders

type TransportWithHeaders struct {
	Transport http.RoundTripper
	Header    http.Header
	Override  bool
}

TransportWithHeaders implements http.RoundTripper. When set as Transport of http.Client, it adds HTTP headers to requests. No field is mandatory. Can be implemented with http.Client as: client.Transport = httputilmore.TransportWithHeaders{ Transport:client.Transport, Header:myHeader}

func (TransportWithHeaders) RoundTrip

func (t TransportWithHeaders) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip adds the additional headers per request implements http.RoundTripper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL