Documentation
¶
Index ¶
- func CheckHTTPResponse(resp *http.Response, expectedStatus ...int) error
- func CloseBody(resp *http.Response)
- func DoRequest(ctx context.Context, method, url string, expectedStatus ...int) (*http.Response, error)
- func DoRequestWithRetry(ctx context.Context, method, url string, maxRetries int, ...) (*http.Response, error)
- func EnsureHTTPSScheme(address string) string
- func EnsureHTTPScheme(address string) string
- func GetContentLength(ctx context.Context, url string) (int64, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckHTTPResponse ¶
CheckHTTPResponse validates the HTTP response status code against expected values. If no expected statuses are provided, http.StatusOK is used as the default. Returns nil if the response status matches any of the expected statuses. On error, reads up to 1KB of the response body for better error context.
func CloseBody ¶
CloseBody fully reads and closes the response body to enable HTTP connection reuse. For connections to be returned to the pool, the body must be fully read before closing. This function safely handles nil responses and bodies.
func DoRequest ¶
func DoRequest(ctx context.Context, method, url string, expectedStatus ...int) (*http.Response, error)
DoRequest performs an HTTP request with context and validates the response status. It creates the request, executes it, and checks the response status in one call.
IMPORTANT: The caller MUST check the error before using the response. On success (err == nil): caller is responsible for closing resp.Body. On error (err != nil): returns nil response (body already closed internally).
Example usage:
resp, err := DoRequest(ctx, "GET", url)
if err != nil {
return err // resp is nil, nothing to close
}
defer resp.Body.Close() // Only after error check!
Returns the response and nil error if the status matches expected (default: 200 OK).
func DoRequestWithRetry ¶
func DoRequestWithRetry(ctx context.Context, method, url string, maxRetries int, retryInterval time.Duration, expectedStatus ...int) (*http.Response, error)
DoRequestWithRetry performs an HTTP request with retry logic. It retries on network errors and unexpected HTTP status codes.
Parameters:
- maxRetries: maximum number of retry attempts (0 means no retries, just one attempt)
- retryInterval: initial wait time between retries (doubles with each retry)
IMPORTANT: The caller MUST check the error before using the response. On success (err == nil): caller is responsible for closing resp.Body. On error (err != nil): returns nil response.
Example usage:
resp, err := DoRequestWithRetry(ctx, "GET", url, 3, time.Second)
if err != nil {
return err
}
defer resp.Body.Close()
func EnsureHTTPSScheme ¶
EnsureHTTPSScheme adds "https://" to the address if no scheme is present. An existing scheme (including a plain http://) is preserved unchanged.
func EnsureHTTPScheme ¶
EnsureHTTPScheme adds "http://" to the address if no scheme is present.
func GetContentLength ¶
GetContentLength performs a HEAD request to get the expected file size from the server. Returns:
- >0: Known file size from Content-Length header
- 0: Server explicitly reported empty file (Content-Length: 0)
- -1: Content-Length header was not set (unknown size), or an error occurred
Returns an error if the HEAD request fails or returns a non-200 status code. On error, returns -1 to indicate unknown size.
Types ¶
This section is empty.