-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleresponse.go
58 lines (48 loc) · 1.65 KB
/
simpleresponse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package gorest
import (
"encoding/json"
"fmt"
"net/http"
)
// SimpleResponse is a standard response message useful for simple ACK or NAK.
type SimpleResponse struct {
Status string `json:"status,omitempty"`
Message string `json:"message,omitempty"`
}
const (
// ACK message.
ACK = "ACK"
// NAK message.
NAK = "NAK"
)
// NewSimpleResponse creates a new SimpleResponse with provided status
// and message.
func NewSimpleResponse(status, message string) SimpleResponse {
return SimpleResponse{Status: status, Message: message}
}
// NewSimpleResponsef creates a new SimpleResponse with provided status and
// composing the message using provided format and variadic arguments.
func NewSimpleResponsef(status, format string, args ...interface{}) SimpleResponse {
return SimpleResponse{Status: status, Message: fmt.Sprintf(format, args...)}
}
// NewFailResponse creates a new NAK SimpleResponse with provided message.
func NewFailResponse(message string) SimpleResponse {
return NewSimpleResponse(NAK, message)
}
// NewFailResponsef creates a new NAK SimpleResponse composing the message
// using provided format and variadic arguments.
func NewFailResponsef(format string, args ...interface{}) SimpleResponse {
return NewSimpleResponsef(NAK, format, args...)
}
// GetBody returns the JSON encoding of the FailResponse.
func (s SimpleResponse) GetBody() ([]byte, error) {
return json.Marshal(s)
}
// GetCookie returns nil since no cookie is needed for this response.
func (s SimpleResponse) GetCookie() *http.Cookie {
return nil
}
// GetHeaders returns nil since no header is needed for this response.
func (s SimpleResponse) GetHeaders() http.Header {
return nil
}