response.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package utils
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/gravitl/netmaker/models"
  6. )
  7. // ReturnSuccessResponse - success api response
  8. func ReturnSuccessResponse(c *gin.Context, message string, responseBody interface{}) {
  9. var httpResponse models.SuccessResponse
  10. httpResponse.Code = http.StatusOK
  11. httpResponse.Message = message
  12. httpResponse.Response = responseBody
  13. if httpResponse.Response == nil {
  14. httpResponse.Response = struct{}{}
  15. }
  16. c.Writer.Header().Set("Content-Type", "application/json")
  17. c.JSON(http.StatusOK, httpResponse)
  18. }
  19. // ReturnErrorResponse - error api response
  20. func ReturnErrorResponse(c *gin.Context, errorMessage models.ErrorResponse) {
  21. httpResponse := &models.ErrorResponse{Code: errorMessage.Code, Message: errorMessage.Message}
  22. c.Writer.Header().Set("Content-Type", "application/json")
  23. c.JSON(errorMessage.Code, httpResponse)
  24. }
  25. // AbortWithError - abort api request with error
  26. func AbortWithError(c *gin.Context, errorMessage models.ErrorResponse) {
  27. httpResponse := &models.ErrorResponse{Code: errorMessage.Code, Message: errorMessage.Message}
  28. c.Writer.Header().Set("Content-Type", "application/json")
  29. c.AbortWithStatusJSON(errorMessage.Code, httpResponse)
  30. }