Documentation
¶
Overview ¶
Package httputils defines utilities for making HTTP calls. Includes functions for returning the entire response as a result.Result[*http.Response] or just the body as a result.Result[string] or the result.Result[T] where the response body is unmarashaled from JSON into a T.
In addition to the response body, if the type T implements the ResponseHeaderSetter interface then the response headers will be set on the T before it is returned. Note that the type used with DoAsJSON must match the receiver of the SetResponseHeaders method for that method to be called.
StatusCodes greater than 299 are converted to errors.
Debug output of the request and response can be enabled by the GGDK_HTTP_DEBUG environment variable. Valid values are anything that strconv.ParseBool accepts as well as the value "full". A value of true enables basic debug output, while a value of "full" enables more detailed debug output including the request and response headers and bodies. By default, this output is written to stderr that functions are provided to change this destination to any io.Writer.
Example:
type data struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
headers http.Header
}
func (d *data) SetResponseHeaders(headers http.Header) *data {
d.headers = headers
return d
}
func main() {
req, err := http.NewRequest(http.MethodGet, "https://example.com", nil)
if err != nil {
log.Fatal(err)
}
d := httputils.DoAsJSON[*data](http.DefaultClient, req)
}
Package httputils (see doc.go for package documentation). The bodyclose linter cannot follow the response body through the result chain to ensure it is closed, so it is disabled for this file. nolint:bodyclose
Index ¶
- func Do(c *http.Client, req *http.Request) result.Result[*http.Response]
- func DoAsJSON[T any](c *http.Client, req *http.Request) result.Result[T]
- func DoAsString(c *http.Client, req *http.Request) result.Result[string]
- func DoNoResponse(c *http.Client, req *http.Request) error
- func DoNoResponseBody(c *http.Client, req *http.Request) error
- func SetDebug(level string)
- func SetDebugOutput(w io.Writer)
- type ResponseHeaderSetter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do executes the HTTP request using the given client and returns a result containing the response without further processing. StatusCodes greater than 299 are converted to errors with the response body in the error message. The caller is responsible for closing the response body.
func DoAsJSON ¶
DoAsJSON executes the HTTP request using the given client and returns a Result containing the unmarshaled response of type T. StatusCodes greater than 299 are converted to errors with the response body in the error message.
func DoAsString ¶
DoAsString executes the HTTP request using the given client and returns a Result containing the response body as a string. StatusCodes greater than 299 are converted to errors with the response body in the error message.
func DoNoResponse ¶ added in v0.4.0
DoNoResponse is deprecated and will be removed. Use DoNoResponseBody instead.
func DoNoResponseBody ¶ added in v0.4.1
DoNoResponseBody executes the HTTP request using the given client and returns an error if the request fails (status code > 299) or nil if it succeeds. The response body is closed before returning.
func SetDebug ¶
func SetDebug(level string)
SetDebug sets the debug level based on the given string. It accepts "disabled", "enabled", "full", or any value that strconv.ParseBool can parse (1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False). If the input cannot be parsed, the debug level defaults to disabled.
func SetDebugOutput ¶
SetDebugOutput sets the output destination for HTTP debug information.
Types ¶
type ResponseHeaderSetter ¶
type ResponseHeaderSetter[T any] interface { SetResponseHeaders(http.Header) T //nolint:inamedparam }
ResponseHeaderSetter defines an interface for types that can have response headers set on them. This is used to provide response headers to the caller if the type returned by DoAsJSON implements this interface. The SetResponseHeaders method returns a T so that the method can have a value receiver. If the receiver is a pointer, then the type used with DoAsJson should be a pointer type.