SimpleCloudNotifier/server/common/ginresp/resp.go

95 lines
2.5 KiB
Go
Raw Normal View History

2022-11-13 19:17:07 +01:00
package ginresp
import (
2022-11-18 21:25:40 +01:00
scn "blackforestbytes.com/simplecloudnotifier"
2022-11-13 19:17:07 +01:00
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"github.com/gin-gonic/gin"
"net/http"
)
type HTTPResponse interface {
2022-11-18 21:25:40 +01:00
Write(g *gin.Context)
2022-11-13 19:17:07 +01:00
}
type jsonHTTPResponse struct {
statusCode int
data any
}
func (j jsonHTTPResponse) Write(g *gin.Context) {
g.JSON(j.statusCode, j.data)
}
type emptyHTTPResponse struct {
statusCode int
data any
}
func (j emptyHTTPResponse) Write(g *gin.Context) {
g.Status(j.statusCode)
}
type textHTTPResponse struct {
statusCode int
data string
}
func (j textHTTPResponse) Write(g *gin.Context) {
g.String(j.statusCode, "%s", j.data)
}
type dataHTTPResponse struct {
statusCode int
data []byte
contentType string
}
func (j dataHTTPResponse) Write(g *gin.Context) {
g.Data(j.statusCode, j.contentType, j.data)
}
type errHTTPResponse struct {
statusCode int
data any
}
func (j errHTTPResponse) Write(g *gin.Context) {
g.JSON(j.statusCode, j.data)
}
func Status(sc int) HTTPResponse {
return &emptyHTTPResponse{statusCode: sc}
}
func JSON(sc int, data any) HTTPResponse {
return &jsonHTTPResponse{statusCode: sc, data: data}
}
func Data(sc int, contentType string, data []byte) HTTPResponse {
return &dataHTTPResponse{statusCode: sc, contentType: contentType, data: data}
}
func Text(sc int, data string) HTTPResponse {
return &textHTTPResponse{statusCode: sc, data: data}
}
func InternalError(e error) HTTPResponse {
2022-11-18 21:25:40 +01:00
return &errHTTPResponse{statusCode: http.StatusInternalServerError, data: apiError{Success: false, Error: int(apierr.INTERNAL_EXCEPTION), Message: e.Error()}}
2022-11-13 19:17:07 +01:00
}
2022-11-18 21:25:40 +01:00
func InternAPIError(errorid apierr.APIError, msg string, e error) HTTPResponse {
if scn.Conf.ReturnRawErrors {
return &errHTTPResponse{statusCode: http.StatusInternalServerError, data: apiError{Success: false, Error: int(errorid), Message: msg, RawError: e}}
} else {
return &errHTTPResponse{statusCode: http.StatusInternalServerError, data: apiError{Success: false, Error: int(errorid), Message: msg}}
}
2022-11-13 19:17:07 +01:00
}
func SendAPIError(errorid apierr.APIError, highlight int, msg string) HTTPResponse {
2022-11-18 21:25:40 +01:00
return &errHTTPResponse{statusCode: http.StatusInternalServerError, data: apiError{Success: false, Error: int(errorid), ErrorHighlight: highlight, Message: msg}}
}
func NotImplemented() HTTPResponse {
return &errHTTPResponse{statusCode: http.StatusInternalServerError, data: apiError{Success: false, Error: -1, ErrorHighlight: 0, Message: "Not Implemented"}}
2022-11-13 19:17:07 +01:00
}