ai-search/service/response.go
2024-02-03 00:49:45 +08:00

52 lines
1.2 KiB
Go

package service
import (
"net/http"
"github.com/gin-gonic/gin"
)
type CommonResp interface {
gin.H
}
func OKResp[T CommonResp](c *gin.Context, origin *T) {
if c.ContentType() == "application/x-protobuf" {
c.ProtoBuf(http.StatusOK, origin)
} else {
c.JSON(http.StatusOK, OK(RespSuccess).WithBody(origin))
}
}
func OKRespWithJsonMarshal[T CommonResp](c *gin.Context, origin *T) {
c.JSON(http.StatusOK, OK(RespSuccess).WithBody(origin))
}
func ErrResp[T CommonResp](c *gin.Context, origin *T, err string, errCode ...int) {
if c.ContentType() == "application/x-protobuf" {
c.ProtoBuf(http.StatusInternalServerError, origin)
} else {
if len(errCode) > 0 {
c.JSON(errCode[0], Err(err).WithBody(origin))
return
}
c.JSON(http.StatusOK, Err(err).WithBody(origin))
}
}
func ErrUnAuthorized(c *gin.Context, err string) {
if c.ContentType() == "application/x-protobuf" {
c.ProtoBuf(http.StatusUnauthorized, nil)
} else {
c.JSON(http.StatusUnauthorized, Err(err).WithBody(nil))
}
}
func ErrNotFound(c *gin.Context, err string) {
if c.ContentType() == "application/x-protobuf" {
c.ProtoBuf(http.StatusNotFound, nil)
} else {
c.JSON(http.StatusNotFound, Err(err).WithBody(nil))
}
}