Documentation ¶
Index ¶
- Variables
- func Error(resp http.ResponseWriter, err error) error
- func Html(resp http.ResponseWriter, content string, code int) error
- func IsErrorResponseWithCode(err error, code int) bool
- func IsErrorResponseWithCodeFunc(code int) func(error) bool
- func IsStatusBadRequest(err error) bool
- func IsStatusConflict(err error) bool
- func IsStatusForbidden(err error) bool
- func IsStatusInternalServer(err error) bool
- func IsStatusNotFound(err error) bool
- func IsStatusPreconditionFailed(err error) bool
- func IsStatusUnauthorizedError(err error) bool
- func JSON(resp http.ResponseWriter, result interface{}, code int) error
- func NewErrorResponse(message string, code int) error
- func Text(resp http.ResponseWriter, content string, code int) error
- type ErrorResponse
- type RestClient
- func (c *RestClient) DefaultErrorParser(resp *http.Response, body []byte) error
- func (c *RestClient) DefaultRequestBuilder(method, path string, query url.Values, reqBody interface{}) (*http.Request, error)
- func (c *RestClient) DefaultResponseParser(resp *http.Response, result interface{}) error
- func (c *RestClient) DefaultResultParser(resp *http.Response, body []byte, result interface{}) error
- func (c *RestClient) Request(method, path string, query url.Values, reqBody interface{}, result interface{}) error
Constants ¶
This section is empty.
Variables ¶
var ( ForbiddenError = newErrorResponseWithStatusCodeFunc(http.StatusForbidden) InternalServerError = newErrorResponseWithStatusCodeFunc(http.StatusInternalServerError) BadRequestError = newErrorResponseWithStatusCodeFunc(http.StatusBadRequest) NotFoundError = newErrorResponseWithStatusCodeFunc(http.StatusNotFound) ConflictError = newErrorResponseWithStatusCodeFunc(http.StatusConflict) PreconditionFailedError = newErrorResponseWithStatusCodeFunc(http.StatusPreconditionFailed) WithStack = func(err error) error { return err } Cause = func(err error) error { return err } )
Functions ¶
func Error ¶
func Error(resp http.ResponseWriter, err error) error
Error sends an error message back to the given response writer.
func Html ¶
func Html(resp http.ResponseWriter, content string, code int) error
Html creates a text/html content-type header, sets the given HTTP status code and writes the given content to the response writer.
func IsErrorResponseWithCode ¶
func IsStatusBadRequest ¶
func IsStatusConflict ¶
func IsStatusForbidden ¶
func IsStatusInternalServer ¶
func IsStatusNotFound ¶
func JSON ¶
func JSON(resp http.ResponseWriter, result interface{}, code int) error
JSON creates a application/json content-type header, sets the given HTTP status code and encodes the given result object to the response writer.
func NewErrorResponse ¶
Types ¶
type ErrorResponse ¶
type ErrorResponse struct { TheError struct { Message string `json:"message,omitempty"` Code int `json:"code,omitempty"` } `json:"error"` // contains filtered or unexported fields }
func NewErrorResponseFromError ¶
func NewErrorResponseFromError(err error) ErrorResponse
NewErrorResponseFromError creates an ErrorResponse from the given error. This ErrorResponse can be sent directly to an HttpResponseWriter.
func (*ErrorResponse) Error ¶
func (er *ErrorResponse) Error() string
func (*ErrorResponse) HTTPStatusCode ¶
func (er *ErrorResponse) HTTPStatusCode() int
HTTPStatusCode returns the status code of the given ErrorResponse if such a status code was set. Otherwise it returns http.StatusBadRequest.
type RestClient ¶
type RestClient struct { RequestBuilder func(method, path string, query url.Values, reqBody interface{}) (*http.Request, error) ResultParser func(resp *http.Response, body []byte, result interface{}) error ErrorParser func(resp *http.Response, body []byte) error ResponseParser func(resp *http.Response, result interface{}) error // contains filtered or unexported fields }
func NewRestClient ¶
func NewRestClient(baseURL *url.URL, client ...*http.Client) *RestClient
func (*RestClient) DefaultErrorParser ¶
func (c *RestClient) DefaultErrorParser(resp *http.Response, body []byte) error
DefaultErrorParser tries to parse the given response body into an ErrorResponse.
func (*RestClient) DefaultRequestBuilder ¶
func (c *RestClient) DefaultRequestBuilder(method, path string, query url.Values, reqBody interface{}) (*http.Request, error)
DefaultRequestBuilder implements the default RequestBuilder behavior.
func (*RestClient) DefaultResponseParser ¶
func (c *RestClient) DefaultResponseParser(resp *http.Response, result interface{}) error
DefaultResponseParser implements the default ResponseParser behavior. It reads the response body. Then if the status == OK, it tries to parse it into the result. Otherwise if the body is not empty, it tries to parse it into an ErrorResponse.
func (*RestClient) DefaultResultParser ¶
func (c *RestClient) DefaultResultParser(resp *http.Response, body []byte, result interface{}) error
DefaultErrorParser tries to parse the given response body into a the given result object.
func (*RestClient) Request ¶
func (c *RestClient) Request(method, path string, query url.Values, reqBody interface{}, result interface{}) error
Request executes a client request. method: GET|POST|PUT|DELETE|HEAD path: Path relative to the path of the baseURL query: Query string (can be nil) reqBody: Object to marshal into the request body result: Reference to object to unmarshal the response into