package httpx import ( "net/http" "github.com/gin-gonic/gin" ) // APIError is the standard error body for the management API. type APIError struct { Code int `json:"code"` Message string `json:"message"` } // OK writes a JSON success response. func OK(c *gin.Context, data any) { c.JSON(http.StatusOK, gin.H{"code": 0, "data": data}) } // Created writes a 201 response. func Created(c *gin.Context, data any) { c.JSON(http.StatusCreated, gin.H{"code": 0, "data": data}) } // Fail writes an error response with the given status. func Fail(c *gin.Context, status int, message string) { c.AbortWithStatusJSON(status, gin.H{"code": status, "message": message}) } // FailWithCode writes an error with a custom business code. func FailWithCode(c *gin.Context, status, code int, message string) { c.AbortWithStatusJSON(status, gin.H{"code": code, "message": message}) } // Bind parses the JSON body and aborts with 400 on failure. func Bind(c *gin.Context, dst any) bool { if err := c.ShouldBindJSON(dst); err != nil { Fail(c, http.StatusBadRequest, "invalid request body: "+err.Error()) return false } return true }