response.go 1.3 KB

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