Skip to content

Commit 5ca5afa

Browse files
committed
simplify httpreply
1 parent f500fab commit 5ca5afa

1 file changed

Lines changed: 19 additions & 41 deletions

File tree

httpreply/common.go

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,11 @@ import (
55
"fmt"
66
"net/http"
77
"strconv"
8-
9-
"github.com/bhmj/goblocks/log"
108
)
119

1210
// Replier defines some common and useful functions
13-
type Replier interface {
14-
Reply(w http.ResponseWriter, code int, contentType string, content []byte) (int, error)
15-
ReplyOK(w http.ResponseWriter) (int, error) // HTTP 200 { "result": "ok" }
16-
ReplyCreated(w http.ResponseWriter) (int, error) // HTTP 201
17-
ReplyNoContent(w http.ResponseWriter) (int, error) // HTTP 204
18-
ReplyError(w http.ResponseWriter, err error, code int) (int, error) // HTTP 4xx { "error": "<...>" }
19-
ReplyJSON(w http.ResponseWriter, str []byte) (int, error) // HTTP 200 <serialized object>
20-
ReplyJSONCode(w http.ResponseWriter, str []byte, code int) (int, error) // HTTP xxx <serialized object>
21-
ReplyObject(w http.ResponseWriter, data any) (int, error) // HTTP 200 <unserialized object>
22-
ReplyObjectCode(w http.ResponseWriter, data any, code int) (int, error) // HTTP xxx <unserialized object>
23-
ReplyString(w http.ResponseWriter, str string) (int, error) // HTTP 200 <text>
24-
}
25-
26-
type replier struct {
27-
logger log.MetaLogger
28-
}
29-
30-
func NewReplier(logger log.MetaLogger) Replier {
31-
return &replier{logger: logger}
32-
}
3311

34-
func (r *replier) Reply(w http.ResponseWriter, code int, contentType string, content []byte) (int, error) {
12+
func Reply(w http.ResponseWriter, code int, contentType string, content []byte) (int, error) {
3513
if contentType != "" {
3614
w.Header().Set("Content-Type", contentType)
3715
}
@@ -43,40 +21,40 @@ func (r *replier) Reply(w http.ResponseWriter, code int, contentType string, con
4321
return code, nil
4422
}
4523

46-
func (r *replier) ReplyOK(w http.ResponseWriter) (int, error) {
47-
return r.Reply(w, http.StatusOK, "", nil)
24+
func OK(w http.ResponseWriter) (int, error) {
25+
return Reply(w, http.StatusOK, "", nil)
4826
}
4927

50-
func (r *replier) ReplyCreated(w http.ResponseWriter) (int, error) {
51-
return r.Reply(w, http.StatusCreated, "", nil)
28+
func Created(w http.ResponseWriter) (int, error) {
29+
return Reply(w, http.StatusCreated, "", nil)
5230
}
5331

54-
func (r *replier) ReplyNoContent(w http.ResponseWriter) (int, error) {
55-
return r.Reply(w, http.StatusNoContent, "", nil)
32+
func NoContent(w http.ResponseWriter) (int, error) {
33+
return Reply(w, http.StatusNoContent, "", nil)
5634
}
5735

58-
func (r *replier) ReplyError(w http.ResponseWriter, err error, code int) (int, error) {
59-
return r.Reply(w, code, "application/json", []byte(`{"error":"`+fmt.Sprintf("%s", err)+`"}`))
36+
func Error(w http.ResponseWriter, err error, code int) (int, error) {
37+
return Reply(w, code, "application/json", []byte(`{"error":"`+fmt.Sprintf("%s", err)+`"}`))
6038
}
6139

62-
func (r *replier) ReplyJSON(w http.ResponseWriter, str []byte) (int, error) {
63-
return r.ReplyJSONCode(w, str, http.StatusOK)
40+
func JSON(w http.ResponseWriter, str []byte) (int, error) {
41+
return JSONCode(w, str, http.StatusOK)
6442
}
6543

66-
func (r *replier) ReplyJSONCode(w http.ResponseWriter, str []byte, code int) (int, error) {
67-
return r.Reply(w, code, "application/json", str)
44+
func JSONCode(w http.ResponseWriter, str []byte, code int) (int, error) {
45+
return Reply(w, code, "application/json", str)
6846
}
6947

70-
func (r *replier) ReplyObject(w http.ResponseWriter, obj any) (int, error) {
48+
func Object(w http.ResponseWriter, obj any) (int, error) {
7149
buf, _ := json.Marshal(obj)
72-
return r.Reply(w, http.StatusOK, "application/json", buf)
50+
return Reply(w, http.StatusOK, "application/json", buf)
7351
}
7452

75-
func (r *replier) ReplyObjectCode(w http.ResponseWriter, obj any, code int) (int, error) {
53+
func ObjectCode(w http.ResponseWriter, obj any, code int) (int, error) {
7654
buf, _ := json.Marshal(obj)
77-
return r.Reply(w, code, "application/json", buf)
55+
return Reply(w, code, "application/json", buf)
7856
}
7957

80-
func (r *replier) ReplyString(w http.ResponseWriter, str string) (int, error) {
81-
return r.Reply(w, http.StatusOK, "text/plain", []byte(str))
58+
func String(w http.ResponseWriter, str string) (int, error) {
59+
return Reply(w, http.StatusOK, "text/plain", []byte(str))
8260
}

0 commit comments

Comments
 (0)