miniflux/http/handler/json_response.go

112 lines
2.8 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package handler
2017-11-20 06:10:04 +01:00
import (
"encoding/json"
"errors"
"net/http"
2017-12-16 03:55:57 +01:00
"github.com/miniflux/miniflux/logger"
2017-11-20 06:10:04 +01:00
)
2017-11-22 03:30:16 +01:00
// JSONResponse handles JSON responses.
type JSONResponse struct {
2017-11-20 06:10:04 +01:00
writer http.ResponseWriter
request *http.Request
}
2017-11-22 03:30:16 +01:00
// Standard sends a JSON response with the status code 200.
func (j *JSONResponse) Standard(v interface{}) {
2017-11-20 06:10:04 +01:00
j.commonHeaders()
2017-12-23 05:17:48 +01:00
j.writer.WriteHeader(http.StatusOK)
2017-11-20 06:10:04 +01:00
j.writer.Write(j.toJSON(v))
}
2017-11-22 03:30:16 +01:00
// Created sends a JSON response with the status code 201.
func (j *JSONResponse) Created(v interface{}) {
2017-11-20 06:10:04 +01:00
j.commonHeaders()
2017-12-23 05:17:48 +01:00
j.writer.WriteHeader(http.StatusCreated)
2017-11-20 06:10:04 +01:00
j.writer.Write(j.toJSON(v))
}
2017-11-22 03:30:16 +01:00
// NoContent sends a JSON response with the status code 204.
func (j *JSONResponse) NoContent() {
2017-11-20 06:10:04 +01:00
j.commonHeaders()
2017-12-23 05:17:48 +01:00
j.writer.WriteHeader(http.StatusNoContent)
2017-11-20 06:10:04 +01:00
}
2017-11-22 03:30:16 +01:00
// BadRequest sends a JSON response with the status code 400.
func (j *JSONResponse) BadRequest(err error) {
2017-12-16 03:55:57 +01:00
logger.Error("[Bad Request] %v", err)
2017-11-20 06:10:04 +01:00
j.commonHeaders()
2017-12-23 05:17:48 +01:00
j.writer.WriteHeader(http.StatusBadRequest)
2017-11-20 06:10:04 +01:00
if err != nil {
j.writer.Write(j.encodeError(err))
}
}
2017-11-22 03:30:16 +01:00
// NotFound sends a JSON response with the status code 404.
func (j *JSONResponse) NotFound(err error) {
2017-12-16 03:55:57 +01:00
logger.Error("[Not Found] %v", err)
2017-11-20 06:10:04 +01:00
j.commonHeaders()
2017-12-23 05:17:48 +01:00
j.writer.WriteHeader(http.StatusNotFound)
2017-11-20 06:10:04 +01:00
j.writer.Write(j.encodeError(err))
}
2017-11-22 03:30:16 +01:00
// ServerError sends a JSON response with the status code 500.
func (j *JSONResponse) ServerError(err error) {
2017-12-16 03:55:57 +01:00
logger.Error("[Internal Server Error] %v", err)
2017-11-20 06:10:04 +01:00
j.commonHeaders()
2017-12-23 05:17:48 +01:00
j.writer.WriteHeader(http.StatusInternalServerError)
2017-11-22 00:46:59 +01:00
if err != nil {
j.writer.Write(j.encodeError(err))
}
2017-11-20 06:10:04 +01:00
}
2017-11-22 03:30:16 +01:00
// Forbidden sends a JSON response with the status code 403.
func (j *JSONResponse) Forbidden() {
2017-12-16 03:55:57 +01:00
logger.Info("[API:Forbidden]")
2017-11-20 06:10:04 +01:00
j.commonHeaders()
2017-12-23 05:17:48 +01:00
j.writer.WriteHeader(http.StatusForbidden)
2017-11-20 06:10:04 +01:00
j.writer.Write(j.encodeError(errors.New("Access Forbidden")))
}
2017-11-22 03:30:16 +01:00
func (j *JSONResponse) commonHeaders() {
2017-11-20 06:10:04 +01:00
j.writer.Header().Set("Accept", "application/json")
2017-12-23 05:17:48 +01:00
j.writer.Header().Set("Content-Type", "application/json; charset=utf-8")
2017-11-20 06:10:04 +01:00
}
2017-11-22 03:30:16 +01:00
func (j *JSONResponse) encodeError(err error) []byte {
2017-11-20 06:10:04 +01:00
type errorMsg struct {
ErrorMessage string `json:"error_message"`
}
tmp := errorMsg{ErrorMessage: err.Error()}
data, err := json.Marshal(tmp)
if err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("encoding error: %v", err)
2017-11-20 06:10:04 +01:00
}
return data
}
2017-11-22 03:30:16 +01:00
func (j *JSONResponse) toJSON(v interface{}) []byte {
2017-11-20 06:10:04 +01:00
b, err := json.Marshal(v)
if err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("encoding error: %v", err)
2017-11-20 06:10:04 +01:00
return []byte("")
}
return b
}
2017-12-04 02:44:27 +01:00
// NewJSONResponse returns a new JSONResponse.
func NewJSONResponse(w http.ResponseWriter, r *http.Request) *JSONResponse {
return &JSONResponse{request: r, writer: w}
}