1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-24 15:49:44 +02:00

major code repair from old to new API -- WIP

This commit is contained in:
Harvey Kandola 2017-07-24 16:24:21 +01:00
parent 25b576f861
commit 792c3e2ce8
46 changed files with 3403 additions and 171 deletions

View file

@ -14,10 +14,7 @@ package response
import (
"encoding/json"
"fmt"
"net/http"
"github.com/documize/community/core/log"
)
// Helper for writing consistent headers back to HTTP client
@ -29,55 +26,43 @@ func writeStatus(w http.ResponseWriter, status int) {
// WriteMissingDataError notifies HTTP client of missing data in request.
func WriteMissingDataError(w http.ResponseWriter, method, parameter string) {
writeStatus(w, http.StatusBadRequest)
_, err := w.Write([]byte("{Error: 'Missing data'}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Missing data %s for method %s", parameter, method))
w.Write([]byte("{Error: 'Missing data'}"))
}
// WriteNotFoundError notifies HTTP client of 'record not found' error.
func WriteNotFoundError(w http.ResponseWriter, method string, id string) {
writeStatus(w, http.StatusNotFound)
_, err := w.Write([]byte("{Error: 'Not found'}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Not found ID %s for method %s", id, method))
w.Write([]byte("{Error: 'Not found'}"))
}
// WriteServerError notifies HTTP client of general application error.
func WriteServerError(w http.ResponseWriter, method string, err error) {
writeStatus(w, http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'Internal server error'}"))
log.IfErr(err2)
log.Error(fmt.Sprintf("Internal server error for method %s", method), err)
w.Write([]byte("{Error: 'Internal server error'}"))
}
// WriteDuplicateError notifies HTTP client of duplicate data that has been rejected.
func WriteDuplicateError(w http.ResponseWriter, method, entity string) {
writeStatus(w, http.StatusConflict)
_, err := w.Write([]byte("{Error: 'Duplicate record'}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Duplicate %s record detected for method %s", entity, method))
w.Write([]byte("{Error: 'Duplicate record'}"))
}
// WriteUnauthorizedError notifies HTTP client of rejected unauthorized request.
func WriteUnauthorizedError(w http.ResponseWriter) {
writeStatus(w, http.StatusUnauthorized)
_, err := w.Write([]byte("{Error: 'Unauthorized'}"))
log.IfErr(err)
w.Write([]byte("{Error: 'Unauthorized'}"))
}
// WriteForbiddenError notifies HTTP client of request that is not allowed.
func WriteForbiddenError(w http.ResponseWriter) {
writeStatus(w, http.StatusForbidden)
_, err := w.Write([]byte("{Error: 'Forbidden'}"))
log.IfErr(err)
w.Write([]byte("{Error: 'Forbidden'}"))
}
// WriteBadRequestError notifies HTTP client of rejected request due to bad data within request.
func WriteBadRequestError(w http.ResponseWriter, method, message string) {
writeStatus(w, http.StatusBadRequest)
_, err := w.Write([]byte("{Error: 'Bad Request'}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Bad Request %s for method %s", message, method))
w.Write([]byte("{Error: 'Bad Request'}"))
}
// WriteBadLicense notifies HTTP client of invalid license (402)
@ -87,44 +72,34 @@ func WriteBadLicense(w http.ResponseWriter) {
var e struct {
Reason string
}
e.Reason = "invalid or expired Documize license"
j, _ := json.Marshal(e)
_, err := w.Write(j)
log.IfErr(err)
w.Write(j)
}
// WriteBytes dumps bytes to HTTP response
func WriteBytes(w http.ResponseWriter, data []byte) {
writeStatus(w, http.StatusOK)
_, err := w.Write(data)
log.IfErr(err)
w.Write(data)
}
// WriteString writes string to HTTP response
func WriteString(w http.ResponseWriter, data string) {
writeStatus(w, http.StatusOK)
_, err := w.Write([]byte(data))
log.IfErr(err)
w.Write([]byte(data))
}
// WriteEmpty writes empty JSON HTTP response
func WriteEmpty(w http.ResponseWriter) {
writeStatus(w, http.StatusOK)
_, err := w.Write([]byte("{}"))
log.IfErr(err)
w.Write([]byte("{}"))
}
// WriteJSON serializes data as JSON to HTTP response.
func WriteJSON(w http.ResponseWriter, v interface{}) {
writeStatus(w, http.StatusOK)
j, err := json.Marshal(v)
if err != nil {
log.IfErr(err)
}
_, err = w.Write(j)
log.IfErr(err)
j, _ := json.Marshal(v)
w.Write(j)
}