Documentation ¶
Overview ¶
Package aghhttp provides some common methods to work with HTTP.
Index ¶
- Constants
- func Error(r *http.Request, w http.ResponseWriter, code int, format string, args ...any)
- func ErrorAndLog(ctx context.Context, l *slog.Logger, r *http.Request, w http.ResponseWriter, ...)
- func OK(w http.ResponseWriter)
- func UserAgent() (ua string)
- func WriteJSONResponse(w http.ResponseWriter, r *http.Request, code int, resp any)
- func WriteJSONResponseError(w http.ResponseWriter, r *http.Request, err error)
- func WriteJSONResponseOK(w http.ResponseWriter, r *http.Request, v any)
- func WriteTextPlainDeprecated(w http.ResponseWriter, r *http.Request) (isPlainText bool)
- type ErrorCode
- type HTTPAPIErrorResp
- type JSONDuration
- type JSONTime
- type RegisterFunc
Constants ¶
const ( HdrValApplicationJSON = "application/json" HdrValStrictTransportSecurity = "max-age=31536000; includeSubDomains" HdrValTextPlain = "text/plain" )
HTTP header value constants.
const (
// ErrorCodeTMP000 is the temporary error code used for all errors.
ErrorCodeTMP000 = ""
)
ErrorCode constants.
TODO(a.garipov): Expand and document codes.
Variables ¶
This section is empty.
Functions ¶
func ErrorAndLog ¶
func ErrorAndLog( ctx context.Context, l *slog.Logger, r *http.Request, w http.ResponseWriter, code int, format string, args ...any, )
ErrorAndLog writes formatted message to w and also logs it with the specified logging level.
func UserAgent ¶
func UserAgent() (ua string)
UserAgent returns the ID of the service as a User-Agent string. It can also be used as the value of the Server HTTP header.
func WriteJSONResponse ¶
WriteJSONResponse writes headers with the code, encodes resp into w, and logs any errors it encounters. r is used to get additional information from the request.
func WriteJSONResponseError ¶
func WriteJSONResponseError(w http.ResponseWriter, r *http.Request, err error)
WriteJSONResponseError encodes err as a JSON error into w, and logs any errors it encounters. r is used to get additional information from the request.
func WriteJSONResponseOK ¶
func WriteJSONResponseOK(w http.ResponseWriter, r *http.Request, v any)
WriteJSONResponseOK writes headers with the code 200 OK, encodes v into w, and logs any errors it encounters. r is used to get additional information from the request.
func WriteTextPlainDeprecated ¶
func WriteTextPlainDeprecated(w http.ResponseWriter, r *http.Request) (isPlainText bool)
WriteTextPlainDeprecated responds to the request with a message about deprecation and removal of a plain-text API if the request is made with the "text/plain" content-type.
Types ¶
type ErrorCode ¶
type ErrorCode string
ErrorCode is the error code as used by the HTTP API. See the ErrorCode definition in the OpenAPI specification.
type HTTPAPIErrorResp ¶
HTTPAPIErrorResp is the error response as used by the HTTP API. See the BadRequestResp, InternalServerErrorResp, and similar objects in the OpenAPI specification.
type JSONDuration ¶
JSONDuration is a time.Duration that can be decoded from JSON and encoded into JSON according to our API conventions.
func (JSONDuration) MarshalJSON ¶
func (d JSONDuration) MarshalJSON() (b []byte, err error)
MarshalJSON implements the json.Marshaler interface for JSONDuration. err is always nil.
func (*JSONDuration) UnmarshalJSON ¶
func (d *JSONDuration) UnmarshalJSON(b []byte) (err error)
UnmarshalJSON implements the json.Marshaler interface for *JSONDuration.
type JSONTime ¶
JSONTime is a time.Time that can be decoded from JSON and encoded into JSON according to our API conventions.
func (JSONTime) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for JSONTime. err is always nil.
func (*JSONTime) UnmarshalJSON ¶
UnmarshalJSON implements the json.Marshaler interface for *JSONTime.
type RegisterFunc ¶
type RegisterFunc func(method, url string, handler http.HandlerFunc)
RegisterFunc is the function that sets the handler to handle the URL for the method.
TODO(e.burkov, a.garipov): Get rid of it.