neko/pkg/utils/http.go

134 lines
3.0 KiB
Go
Raw Normal View History

package utils
import (
2021-02-15 02:40:17 +13:00
"encoding/json"
"fmt"
2021-02-15 02:40:17 +13:00
"io"
"net/http"
2020-11-15 06:27:20 +13:00
"github.com/rs/zerolog/log"
)
2021-09-17 10:24:33 +12:00
func HttpJsonRequest(w http.ResponseWriter, r *http.Request, res interface{}) error {
err := json.NewDecoder(r.Body).Decode(res)
if err == nil {
return nil
}
if err == io.EOF {
return HttpBadRequest("no data provided").WithInternalErr(err)
}
2021-09-17 10:24:33 +12:00
return HttpBadRequest("unable to parse provided data").WithInternalErr(err)
}
2021-09-17 06:16:51 +12:00
func HttpJsonResponse(w http.ResponseWriter, code int, res interface{}) {
w.Header().Set("Content-Type", "application/json")
2021-09-17 06:16:51 +12:00
w.WriteHeader(code)
if err := json.NewEncoder(w).Encode(res); err != nil {
2021-09-02 09:10:06 +12:00
log.Err(err).Str("module", "http").Msg("sending http json response failed")
}
}
2021-09-17 10:24:33 +12:00
func HttpSuccess(w http.ResponseWriter, res ...interface{}) error {
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
} else {
HttpJsonResponse(w, http.StatusOK, res[0])
}
2021-09-17 10:24:33 +12:00
return nil
}
2021-09-17 06:16:51 +12:00
// HTTPError is an error with a message and an HTTP status code.
type HTTPError struct {
Code int `json:"code"`
Message string `json:"message"`
InternalErr error `json:"-"`
InternalMsg string `json:"-"`
}
func (e *HTTPError) Error() string {
if e.InternalMsg != "" {
return e.InternalMsg
}
return fmt.Sprintf("%d: %s", e.Code, e.Message)
}
func (e *HTTPError) Cause() error {
if e.InternalErr != nil {
return e.InternalErr
}
return e
}
2021-09-17 06:16:51 +12:00
// WithInternalErr adds internal error information to the error
func (e *HTTPError) WithInternalErr(err error) *HTTPError {
e.InternalErr = err
return e
}
2021-09-17 06:16:51 +12:00
// WithInternalMsg adds internal message information to the error
func (e *HTTPError) WithInternalMsg(msg string) *HTTPError {
e.InternalMsg = msg
return e
}
2021-09-17 06:16:51 +12:00
// WithInternalMsg adds internal formated message information to the error
func (e *HTTPError) WithInternalMsgf(fmtStr string, args ...interface{}) *HTTPError {
e.InternalMsg = fmt.Sprintf(fmtStr, args...)
return e
}
2021-09-17 06:16:51 +12:00
// Sends error with custom formated message
2021-09-17 10:24:33 +12:00
func (e *HTTPError) Msgf(fmtSt string, args ...interface{}) *HTTPError {
2021-09-17 06:16:51 +12:00
e.Message = fmt.Sprintf(fmtSt, args...)
2021-09-17 10:24:33 +12:00
return e
}
2021-09-17 06:16:51 +12:00
// Sends error with custom message
2021-09-17 10:24:33 +12:00
func (e *HTTPError) Msg(str string) *HTTPError {
2021-09-17 06:16:51 +12:00
e.Message = str
2021-09-17 10:24:33 +12:00
return e
}
2021-09-17 10:24:33 +12:00
func HttpError(code int, res ...string) *HTTPError {
err := &HTTPError{
Code: code,
Message: http.StatusText(code),
}
2021-09-17 06:16:51 +12:00
2021-09-17 10:24:33 +12:00
if len(res) == 1 {
err.Message = res[0]
2021-09-17 06:16:51 +12:00
}
2021-09-17 10:24:33 +12:00
return err
2021-09-17 06:16:51 +12:00
}
2021-09-17 10:24:33 +12:00
func HttpBadRequest(res ...string) *HTTPError {
return HttpError(http.StatusBadRequest, res...)
2021-09-17 06:16:51 +12:00
}
2021-09-17 10:24:33 +12:00
func HttpUnauthorized(res ...string) *HTTPError {
return HttpError(http.StatusUnauthorized, res...)
2021-09-17 06:16:51 +12:00
}
2021-09-17 10:24:33 +12:00
func HttpForbidden(res ...string) *HTTPError {
return HttpError(http.StatusForbidden, res...)
2021-09-17 06:16:51 +12:00
}
2021-09-17 10:24:33 +12:00
func HttpNotFound(res ...string) *HTTPError {
return HttpError(http.StatusNotFound, res...)
2021-09-17 06:16:51 +12:00
}
2021-09-17 10:24:33 +12:00
func HttpUnprocessableEntity(res ...string) *HTTPError {
return HttpError(http.StatusUnprocessableEntity, res...)
2021-09-17 06:16:51 +12:00
}
2021-09-17 10:24:33 +12:00
func HttpInternalServerError(res ...string) *HTTPError {
return HttpError(http.StatusInternalServerError, res...)
}