feat: add precise diagnostics and optional client copy logging controls
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
type requestOptions struct {
|
||||
headers http.Header
|
||||
maxResponseBytes int64
|
||||
successStatuses [2]uint64
|
||||
restrictStatus bool
|
||||
}
|
||||
|
||||
// RequestOption applies only to one JSON call. Returned options can be reused
|
||||
@@ -97,3 +99,41 @@ func checkHeader(name string, values []string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WithSuccessStatuses restricts this JSON call to the listed 2xx statuses.
|
||||
// Omission keeps all 2xx successful. Empty/out-of-range lists are rejected before
|
||||
// sending a request. Options snapshot their input and can be reused concurrently.
|
||||
// The policy does not add retries or validate application-specific JSON fields.
|
||||
func WithSuccessStatuses(statuses ...int) RequestOption {
|
||||
var accepted [2]uint64
|
||||
var err error
|
||||
if len(statuses) == 0 {
|
||||
err = errors.New("httpc: at least one success status is required")
|
||||
}
|
||||
for _, status := range statuses {
|
||||
if status < 200 || status >= 300 {
|
||||
err = errors.New("httpc: success statuses must be between 200 and 299")
|
||||
break
|
||||
}
|
||||
bit := status - 200
|
||||
accepted[bit/64] |= uint64(1) << uint(bit%64)
|
||||
}
|
||||
return func(o *requestOptions) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o.successStatuses = accepted
|
||||
o.restrictStatus = true
|
||||
return nil
|
||||
}
|
||||
}
|
||||
func (o requestOptions) accepts(status int) bool {
|
||||
if status < 200 || status >= 300 {
|
||||
return false
|
||||
}
|
||||
if !o.restrictStatus {
|
||||
return true
|
||||
}
|
||||
bit := status - 200
|
||||
return o.successStatuses[bit/64]&(uint64(1)<<uint(bit%64)) != 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user