Documentation ¶
Index ¶
- func BuildHttpClient(c *uconfig.Chain) (rv any, err error)
- func BuildHttpServer(c *uconfig.Chain) (rv any, err error)
- func CloneHttpClient(prototype *http.Client) (rv *http.Client)
- func DefaultHttpClient() (rv *http.Client)
- func DefaultHttpTransport() (rv *http.Transport)
- func IsTlsServer(s *http.Server) bool
- func ShowHttpClient(name, descr string, help *uconfig.Help) *uconfig.Help
- func ShowHttpServer(name, descr string, help *uconfig.Help) *uconfig.Help
- func StartServer(svr *http.Server, onDone func(err error))
- func StopServer(svr *http.Server, grace time.Duration)
- type CondF
- type Part
- type Requestor
- func (this *Requestor) BeforeRequest(f func(req *http.Request) error) *Requestor
- func (this *Requestor) Body(body *io.Reader) *Requestor
- func (this *Requestor) BodyBytes(body *[]byte) *Requestor
- func (this *Requestor) BodyBytesIf(cond CondF, body *[]byte) *Requestor
- func (this *Requestor) BodyCopy(dst io.Writer) *Requestor
- func (this *Requestor) BodyJson(result any) *Requestor
- func (this *Requestor) BodyJsonIf(cond CondF, result any) *Requestor
- func (this *Requestor) BodyLen(length *int64) *Requestor
- func (this *Requestor) BodyText(result *string) *Requestor
- func (this *Requestor) BodyTextIf(cond CondF, result *string) *Requestor
- func (this *Requestor) Delete() *Requestor
- func (this *Requestor) Do() *Requestor
- func (this *Requestor) DoRetriably(times int, delay time.Duration, ...) *Requestor
- func (this *Requestor) Done() (rv *Requestor, err error)
- func (this *Requestor) Dump(reqW, respW io.Writer) *Requestor
- func (this *Requestor) Get() *Requestor
- func (this *Requestor) GetBody() (bodyLength int64, body io.Reader, err error)
- func (this *Requestor) GetError() error
- func (this *Requestor) GetResponseHeaders(key string) (rv []string)
- func (this *Requestor) GetStatus() (status int, err error)
- func (this *Requestor) Head() *Requestor
- func (this *Requestor) IfStatusIn(status []int, then func(c *Requestor) error) *Requestor
- func (this *Requestor) IfStatusIs(status int, then func(c *Requestor) error) *Requestor
- func (this *Requestor) IsOK() *Requestor
- func (this *Requestor) IsOk() *Requestor
- func (this *Requestor) IsStatus(status int) (rv bool)
- func (this *Requestor) IsStatusIn(status []int) (rv bool)
- func (this *Requestor) LinkResponseHeaders(key string) (rv map[string]string)
- func (this *Requestor) Log() *Requestor
- func (this *Requestor) LogIf(on bool) *Requestor
- func (this *Requestor) Patch() *Requestor
- func (this *Requestor) Post() *Requestor
- func (this *Requestor) PostFileMultipart(fileName, fileField, fileFieldValue string, fields map[string]string) *Requestor
- func (this *Requestor) PostForm(url string, values *nurl.Values) *Requestor
- func (this *Requestor) PostMultipart(contentR io.Reader, fileField, fileName string, fields map[string]string) (rv *Requestor)
- func (this *Requestor) PostMultiparts(fields map[string]string, parts ...Part) (rv *Requestor)
- func (this *Requestor) Put() *Requestor
- func (this *Requestor) Reset() *Requestor
- func (this *Requestor) SetBasicAuth(user, pass string) *Requestor
- func (this *Requestor) SetBody(body io.Reader) *Requestor
- func (this *Requestor) SetBodyBytes(body []byte) *Requestor
- func (this *Requestor) SetBodyFile(filename string) *Requestor
- func (this *Requestor) SetBodyJson(body any) *Requestor
- func (this *Requestor) SetContentLength(length int64) *Requestor
- func (this *Requestor) SetContentType(ctype string) *Requestor
- func (this *Requestor) SetHeader(key, value string, values ...string) *Requestor
- func (this *Requestor) SetHeaders(headers map[string]string) *Requestor
- func (this *Requestor) SetMethod(method string) *Requestor
- func (this *Requestor) SetRawHeader(key, value string) *Requestor
- func (this *Requestor) SetRawHeaders(headers map[string]string) *Requestor
- func (this *Requestor) SetTimeout(d time.Duration) *Requestor
- func (this *Requestor) SetUrl(url *nurl.URL) *Requestor
- func (this *Requestor) SetUrlString(url string) *Requestor
- func (this *Requestor) Status(status *int) *Requestor
- func (this *Requestor) StatusIn(status ...int) *Requestor
- func (this *Requestor) StatusIs(status int) *Requestor
- func (this *Requestor) Then(f func(c *Requestor) error) *Requestor
- func (this *Requestor) WithContext(ctx context.Context) *Requestor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildHttpClient ¶
build a http.Client using uconfig
if c is nil, then build default http.Client
func BuildHttpServer ¶
build a http.Server using uconfig
if c is nil, then build default http.Server
func DefaultHttpClient ¶
func DefaultHttpTransport ¶
func ShowHttpClient ¶
show params available for building http.Client
func ShowHttpServer ¶
show params available for building http.Server
func StartServer ¶
start listening on a server
Types ¶
type Part ¶
type Part struct { FileField string // field name for filename to use in multipart mime part FileName string // filename (basename) to use in multipart mime part ContentType string // if blank, application/octet-stream ContentR io.Reader // where the bytes are Len int64 // bytes to read from ContentR }
Use with UploadMultiparts. See RFC 2045, 2046.
type Requestor ¶
type Requestor struct { Client *http.Client Request *http.Request Response *http.Response // contains filtered or unexported fields }
a fluent wrapper to deal with http interactions
Example:
_, err := urest.NewRequestor(nil).Get("http://google.com").IsOK().Done() var client *http.Client ... c, err := urest.NewRequestor(client). SetUrlString("http://..."). SetBody(body). Post(). IsOK(). Done() var pMyStruct *MyStruct c, err := urest.NewRequestor(client). SetUrlString("http://..."). SetBodyJson(thing). Post(). IsOK(). BodyJson(&pMyStruct). // address of ptr Done() c, err := urest.NewRequestor(client). SetUrlString("http://..."). PostMultipart(file,fileParm, ...). IsOK(). Done() var reqW, respW bytes.Buffer _, err := urest.NewRequestor(client).Dump(&reqW,&respW).PostJson(...
func NewRequestor ¶
create a new request chain. if client is nil (not recommended), then use default client.
func (*Requestor) BeforeRequest ¶
Perform specialized adjustment of req before making the request
func (*Requestor) BodyBytesIf ¶
get the body of the response as []byte if cond met
func (*Requestor) BodyJson ¶
decode response body JSON into result. result should be a ptr to the struct to fill in, or a ptr to ptr.
func (*Requestor) BodyJsonIf ¶
decode response body JSON into result if condition met. result should be a ptr to the struct to fill in, or a ptr to ptr.
func (*Requestor) BodyTextIf ¶
decode response body text into result if condition met
func (*Requestor) DoRetriably ¶
func (this *Requestor) DoRetriably( times int, delay time.Duration, onResp func(*Requestor, int) (retry bool, err error), ) *Requestor
Repeatedly perform request until onResp returns false or an error
if times is less than or equal to 0 (zero), then retry indefinitely as long as onResp returns true
onResp gets a ref to this, so must check for Error, Response, etc.
Error will be set if there is a connection problem (see http.Client.Do)
Error will not be set, but Response may indicate a retriable problem with the server (502, 503, 504, ...)
Error will be set to the returned error (if any) of onResp ¶
NOTE: This is primarily for requests that can be replayed, such as GET. or POST/PUT with no Body. The onResp method must perform any required reset.
func (*Requestor) Dump ¶
dump out request and/or response to specified writers. put nil in if you don't want one or the other
func (*Requestor) GetBody ¶
get the body of the response and the length of it (if known. if not known, then length will be negative
func (*Requestor) GetResponseHeaders ¶
Get response header values. There may be multiple header values for the key, and the values may be CSV separated. The spec says that CSV separated values should be treated the same as multiple header/value pairs. Normalize all of that to an array of values.
func (*Requestor) IfStatusIn ¶
if return status is one of specified, then invoke func
func (*Requestor) IfStatusIs ¶
if return status is as specified, then invoke method
func (*Requestor) IsStatusIn ¶
func (*Requestor) LinkResponseHeaders ¶
get Link headers for pagination, returning map of links per rel type.
if key is not set, it will default to "Link"
Link: <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=15>; rel="next",
<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last", <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=1>; rel="first", <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=13>; rel="prev"
func (*Requestor) PostFileMultipart ¶
func (this *Requestor) PostFileMultipart( fileName, fileField, fileFieldValue string, fields map[string]string, ) *Requestor
Upload the named file using the multipart/form_data method.
The file will be openned and streamed to the server.
If ContentLength is not set, this will fill in the correct value.
If fileFieldValue is not set, then it will be set to filepath.Base(fileName)
func (*Requestor) PostMultipart ¶
func (this *Requestor) PostMultipart( contentR io.Reader, fileField, fileName string, fields map[string]string, ) (rv *Requestor)
Upload content by posting multipart/form-data.
See RFC 2045, 2046.
A direct post is preferred as it is more efficient and simpler, but some things require the form based way of doing things.
We stream the content to the server instead of assembling the whole multipart message in memory.
If you SetContentLength ahead of this, then this will adjust the Content-Length to account for the form data. otherwise, Content-Length will be -1.
fileName should be the basename of the file.
func (*Requestor) PostMultiparts ¶
Upload content from multiple io.Readers in one multipart/form-data POST
See RFC 2045, 2046.
A direct post is preferred as it is more efficient and simpler, but some things require the form based way of doing things.
We stream the content to the server instead of assembling the whole multipart message in memory.
func (*Requestor) SetBasicAuth ¶
set basic auth info. if user is "", then do not actually set the info
func (*Requestor) SetBody ¶
set the body. setting nil indicates no data in body. the body will be automatically closed
func (*Requestor) SetBodyBytes ¶
set the body and content length.
func (*Requestor) SetBodyFile ¶
set the body to the contents (and length) of the specified file
func (*Requestor) SetBodyJson ¶
set a JSON body
func (*Requestor) SetContentLength ¶
Set the Content-Length HTTP request header
if content length is set to a positive number, then go http will use a LimitReader, which will prevent ReaderFrom/WriterTo optimization
func (*Requestor) SetContentType ¶
Set the Content-Type HTTP request header
func (*Requestor) SetHeaders ¶
set the HTTP request headers
func (*Requestor) SetRawHeader ¶
set a header without allowing Go to make the header HTTP compliant, such as capitalizing the header key, etc.
func (*Requestor) SetRawHeaders ¶
set request headers without allowing Go to make them HTTP compliant, such as capitalizing the header key, etc. Some services are broken and require this.
func (*Requestor) SetTimeout ¶
set a timeout to this request
since we create the context, we handle cancelation/cleanup
func (*Requestor) SetUrlString ¶
set the URL to use for the request, parsing the provided string into a URL