From 74c955a3f7ba65066d16c746303824548c4187e4 Mon Sep 17 00:00:00 2001 From: imbytecat Date: Mon, 26 Aug 2024 11:19:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(response):=20=E7=BB=9F=E4=B8=80=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- response/response.go | 73 ++++++++++++++++++++++++++++ router/handler/hello.go | 5 +- router/handler/onvif/connection.go | 39 +++++---------- router/handler/onvif/subscription.go | 15 +++--- 4 files changed, 92 insertions(+), 40 deletions(-) create mode 100644 response/response.go diff --git a/response/response.go b/response/response.go new file mode 100644 index 0000000..0698d29 --- /dev/null +++ b/response/response.go @@ -0,0 +1,73 @@ +package response + +import ( + "encoding/json" + "github.com/gin-gonic/gin" + "net/http" +) + +type Response struct { + Code int `json:"code"` + Message string `json:"message"` + Data any `json:"data,omitempty"` +} + +func NewResponse() *Response { + return &Response{ + Code: http.StatusOK, + Message: "success", + } +} + +func (r *Response) WithCode(code int) *Response { + r.Code = code + return r +} + +func (r *Response) WithMessage(message string) *Response { + r.Message = message + return r +} + +func (r *Response) WithData(data any) *Response { + r.Data = data + return r +} + +func (r *Response) Error(err error) *Response { + r.Code = http.StatusInternalServerError + r.Message = err.Error() + return r +} + +func (r *Response) Success(message ...string) *Response { + r.Code = http.StatusOK + if len(message) > 0 { + r.Message = message[0] + } else { + r.Message = "success" + } + return r +} + +func (r *Response) Fail(message ...string) *Response { + r.Code = http.StatusInternalServerError + if len(message) > 0 { + r.Message = message[0] + } else { + r.Message = "fail" + } + return r +} + +func (r *Response) Send(c *gin.Context) { + c.JSON(http.StatusOK, r) +} + +func (r *Response) ToJSON() (string, error) { + data, err := json.Marshal(r) + if err != nil { + return "", err + } + return string(data), nil +} diff --git a/router/handler/hello.go b/router/handler/hello.go index 93e0b92..c1afd63 100644 --- a/router/handler/hello.go +++ b/router/handler/hello.go @@ -2,10 +2,9 @@ package handler import ( "github.com/gin-gonic/gin" + "onvif-agent/response" ) func Hello(c *gin.Context) { - c.JSON(200, gin.H{ - "message": "Hello, World!", - }) + response.NewResponse().Success("Hello, World!").Send(c) } diff --git a/router/handler/onvif/connection.go b/router/handler/onvif/connection.go index 4ed6f86..74089fc 100644 --- a/router/handler/onvif/connection.go +++ b/router/handler/onvif/connection.go @@ -3,6 +3,7 @@ package onvif import ( "github.com/gin-gonic/gin" "net/http" + "onvif-agent/response" "onvif-agent/service/onvif" ) @@ -17,32 +18,26 @@ type CreateConnectionRequest struct { func CreateConnection(c *gin.Context) { var req CreateConnectionRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + response.NewResponse().Error(err).Send(c) return } conn, err := onvif.New(req.Xaddr, req.Username, req.Password) if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "message": err.Error(), - }) + response.NewResponse().Error(err).Send(c) return } info, err := conn.GetDeviceInfo() if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "message": err.Error(), - }) + response.NewResponse().Error(err).Send(c) return } // store connection conns[conn.Device.GetDeviceParams().Xaddr] = conn - c.JSON(http.StatusOK, gin.H{ - "device": info, - }) + response.NewResponse().WithData(info).Send(c) } func GetConnections(c *gin.Context) { @@ -51,18 +46,14 @@ func GetConnections(c *gin.Context) { for xaddr, conn := range conns { info, err := conn.GetDeviceInfo() if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "message": err.Error(), - }) + response.NewResponse().Error(err).Send(c) return } devices[xaddr] = info } - c.JSON(http.StatusOK, gin.H{ - "connections": devices, - }) + response.NewResponse().WithData(devices).Send(c) } func GetConnectionByXaddr(c *gin.Context) { @@ -70,30 +61,22 @@ func GetConnectionByXaddr(c *gin.Context) { conn := conns[xaddr] if conn == nil { - c.JSON(http.StatusNotFound, gin.H{ - "message": "Connection not found", - }) + response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c) return } info, err := conn.GetDeviceInfo() if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "message": err.Error(), - }) + response.NewResponse().Error(err).Send(c) return } - c.JSON(http.StatusOK, gin.H{ - "device": info, - }) + response.NewResponse().WithData(info).Send(c) } func DeleteConnection(c *gin.Context) { xaddr := c.Param("xaddr") delete(conns, xaddr) - c.JSON(http.StatusOK, gin.H{ - "message": "OK", - }) + response.NewResponse().Success().Send(c) } diff --git a/router/handler/onvif/subscription.go b/router/handler/onvif/subscription.go index 0658c93..e573edf 100644 --- a/router/handler/onvif/subscription.go +++ b/router/handler/onvif/subscription.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "onvif-agent/config" + "onvif-agent/response" ) func CreateEventSubscription(c *gin.Context) { @@ -17,21 +18,17 @@ func CreateEventSubscription(c *gin.Context) { conn := conns[xaddr] if conn == nil { - c.JSON(http.StatusNotFound, gin.H{ - "message": "Connection not found", - }) + response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c) return } result, err := conn.SubscribeEvents(callbackURL, "PT60S") if err != nil { - c.JSON(http.StatusServiceUnavailable, gin.H{ - "message": err.Error(), - }) + response.NewResponse().Error(err).Send(c) return } - c.JSON(http.StatusOK, result) + response.NewResponse().Success().WithData(result).Send(c) } func EventNotifyCallback(c *gin.Context) { @@ -41,7 +38,7 @@ func EventNotifyCallback(c *gin.Context) { var notify event.Notify envelope := gosoap.NewSOAPEnvelope(¬ify) if err := c.ShouldBindXML(&envelope); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + response.NewResponse().Error(err).WithCode(http.StatusBadRequest).Send(c) return } @@ -50,5 +47,5 @@ func EventNotifyCallback(c *gin.Context) { log.Printf("Topic: %s, Message: %s", msg.Topic.TopicKinds, msg.Message.Message) } - c.JSON(http.StatusOK, gin.H{"message": "OK"}) + response.NewResponse().Success().Send(c) }