Documentation ¶
Index ¶
- func BuildHttpClient(c *uconfig.Chain) (rv any, err error)
- func BuildHttpServer(c *uconfig.Chain) (rv any, err error)
- 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 Chained
- func (this *Chained) BeforeRequest(f func(req *http.Request) error) *Chained
- func (this *Chained) Body(body *[]byte) *Chained
- func (this *Chained) BodyCopy(dst io.Writer) *Chained
- func (this *Chained) BodyIf(cond CondF, body *[]byte) *Chained
- func (this *Chained) BodyJson(result any) *Chained
- func (this *Chained) BodyJsonIf(cond CondF, result any) *Chained
- func (this *Chained) BodyText(result *string) *Chained
- func (this *Chained) BodyTextIf(cond CondF, result *string) *Chained
- func (this *Chained) BodyWrite(dst io.Writer, nwrote *int64) *Chained
- func (this *Chained) Do() *Chained
- func (this *Chained) DoRetriably(times int, delay time.Duration, ...) (rv *Chained)
- func (this *Chained) Done() (rv *Chained, err error)
- func (this *Chained) Dump(reqW, respW io.Writer) *Chained
- func (this *Chained) Get(url string) *Chained
- func (this *Chained) GetChain(c **Chained) *Chained
- func (this *Chained) IfStatusIn(status []int, then func(c *Chained) error) (rv *Chained)
- func (this *Chained) IfStatusIs(status int, then func(c *Chained) error) (rv *Chained)
- func (this *Chained) IsOK() *Chained
- func (this *Chained) IsOk() *Chained
- func (this *Chained) IsStatus(status int) (rv bool)
- func (this *Chained) IsStatusIn(status []int) (rv bool)
- func (this *Chained) LinkResponseHeaders(key string) (rv map[string]string)
- func (this *Chained) Log() *Chained
- func (this *Chained) LogIf(on bool) *Chained
- func (this *Chained) Post(url, bodyType string, body io.Reader) *Chained
- func (this *Chained) PostAsJson(url string, body any) *Chained
- func (this *Chained) PostForm(url string, values *nurl.Values) *Chained
- func (this *Chained) ResponseHeaders(key string) (rv []string)
- func (this *Chained) SetBasicAuth(user, pass string) *Chained
- func (this *Chained) SetBody(body io.Reader) *Chained
- func (this *Chained) SetBodyBytes(body []byte) *Chained
- func (this *Chained) SetBodyFile(filename string) *Chained
- func (this *Chained) SetBodyJson(body any) *Chained
- func (this *Chained) SetContentLength(length int64) *Chained
- func (this *Chained) SetContentType(ctype string) *Chained
- func (this *Chained) SetHeader(key, value string, values ...string) *Chained
- func (this *Chained) SetHeaders(headers map[string]string) *Chained
- func (this *Chained) SetMethod(method string) *Chained
- func (this *Chained) SetRawHeader(key, value string) *Chained
- func (this *Chained) SetRawHeaders(headers map[string]string) *Chained
- func (this *Chained) SetTimeout(d time.Duration) *Chained
- func (this *Chained) SetUrl(url *nurl.URL) *Chained
- func (this *Chained) SetUrlString(url string) *Chained
- func (this *Chained) Status(status *int) *Chained
- func (this *Chained) StatusIn(status ...int) *Chained
- func (this *Chained) StatusIs(status int) *Chained
- func (this *Chained) Then(f func(c *Chained) error) *Chained
- func (this *Chained) UploadFileMultipart(url, fileName, fileField, fileFieldValue string, fields map[string]string) *Chained
- func (this *Chained) UploadMultipart(url string, contentR io.Reader, fileField, fileFieldValue string, ...) (rv *Chained)
- func (this *Chained) WithContext(ctx context.Context) *Chained
- func (this *Chained) WriteTo(dst io.Writer) (nwrote int64, err error)
- type CondF
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 Chained ¶
type Chained struct { Client *http.Client Request *http.Request Response *http.Response Error error // contains filtered or unexported fields }
a fluent wrapper to deal with http interactions
Example:
_, err := urest.NewChain(nil).Get("http://google.com").IsOK().Done() var client *http.Client ... c, err := urest.NewChain(client). SetMethod("POST"). SetUrlString("http://..."). SetBody(body). Do(). IsOK(). Done() c, err := urest.NewChain(client). PostAsJson("http://...",thing). IsOK(). BodyJson(&resp). Done() c, err := urest.NewChain(client). UploadMultipart("http://...",file,fileParm, ...). IsOK(). Done() var reqW, respW bytes.Buffer _, err := urest.NewChain(client).Dump(&reqW,&respW).PostAsJson(url,...
func NewChain ¶
create a new request chain. if client is nil (not recommended), then use default client.
func (*Chained) BeforeRequest ¶
Perform specialized adjustment of req before making the request
func (*Chained) BodyJsonIf ¶
decode response body JSON into result if condition met
func (*Chained) BodyTextIf ¶
decode response body text into result if condition met
func (*Chained) DoRetriably ¶
func (this *Chained) DoRetriably( times int, delay time.Duration, onResp func(*Chained, int) (retry bool, err error), ) (rv *Chained)
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 (*Chained) Dump ¶
dump out request and/or response to specified writers. put nil in if you don't want one or the other
func (*Chained) IfStatusIn ¶
if return status is one of specified, then invoke func
func (*Chained) IfStatusIs ¶
if return status is as specified, then invoke method
func (*Chained) IsStatusIn ¶
func (*Chained) 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 (*Chained) PostAsJson ¶
perform a simple JSON POST
func (*Chained) ResponseHeaders ¶
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 (*Chained) SetBasicAuth ¶
set basic auth info. if user is "", then do not actually set the info
func (*Chained) SetBody ¶
set the body. setting nil indicates no data in body. the body will be automatically closed
func (*Chained) SetBodyBytes ¶
set the body and content length.
func (*Chained) SetBodyFile ¶
set the body to the contents (and length) of the specified file
func (*Chained) 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 (*Chained) SetContentType ¶
Set the Content-Type HTTP request header
func (*Chained) SetHeaders ¶
set the HTTP request headers
func (*Chained) SetRawHeader ¶
set a header without allowing Go to make the header HTTP compliant, such as capitalizing the header key, etc.
func (*Chained) SetRawHeaders ¶
set headers without allowing Go to make them HTTP compliant, such as capitalizing the header key, etc. Some services are broken and require this.
func (*Chained) SetTimeout ¶
set a timeout to this request
since we create the context, we handle cancelation/cleanup
func (*Chained) SetUrlString ¶
set the URL to use for the request
func (*Chained) UploadFileMultipart ¶
func (this *Chained) UploadFileMultipart( url, fileName, fileField, fileFieldValue string, fields map[string]string, ) *Chained
upload a file by posting as a multipart form
a direct post is preferred as it is much more efficient and much easier, but some things require the form based way of doing things.
we stream the file contents to the server instead of assembling the whole multipart message in memory.
func (*Chained) UploadMultipart ¶
func (this *Chained) UploadMultipart( url string, contentR io.Reader, fileField, fileFieldValue string, fields map[string]string, ) (rv *Chained)
upload content by posting as a multipart form
a direct post is preferred as it is much more efficient and much easier, 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 (*Chained) WithContext ¶
add a cancelation context to the request
since the context is provided by caller, it is caller's responsibility to check the context and cancel it, etc...