holiday-api/render.go

43 lines
914 B
Go
Raw Normal View History

2023-06-16 07:42:50 +00:00
package main
import (
2023-06-23 17:31:02 +00:00
"github.com/gin-gonic/gin"
2023-06-16 07:42:50 +00:00
)
2023-12-31 12:35:29 +00:00
type CSV interface {
CSV() []byte
}
func render(c *gin.Context, status int, response any, contentType *string) {
value := c.GetHeader("accept")
if contentType != nil {
switch *contentType {
case "xml":
value = "text/xml"
case "json":
value = "application/json"
case "csv":
value = "text/csv"
}
}
switch value {
case "text/csv":
if csvResponse, ok := response.(CSV); ok {
c.Data(200, value+"; charset=utf-8", csvResponse.CSV())
} else {
c.Header("content-type", "application/json; charset=utf-8")
c.JSON(status, response)
}
2023-06-20 14:10:46 +00:00
case "application/xml":
fallthrough
case "text/xml":
2023-12-31 12:35:29 +00:00
c.Header("content-type", value+"; charset=utf-8; header=present;")
2023-06-23 17:31:02 +00:00
c.XML(status, response)
2023-06-20 14:10:46 +00:00
case "application/json":
fallthrough
default:
2023-06-23 17:31:02 +00:00
c.Header("content-type", "application/json; charset=utf-8")
c.JSON(status, response)
2023-06-16 07:42:50 +00:00
}
}