forked from danielgtaylor/huma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
99 lines (88 loc) · 2.34 KB
/
response.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package huma
import (
"reflect"
"strings"
)
// Response describes an HTTP response that can be returned from an operation.
type Response struct {
description string
status int
contentType string
headers []string
model reflect.Type
modelRef string
}
// NewResponse creates a new response representation.
func NewResponse(status int, description string) Response {
return Response{
status: status,
description: description,
}
}
// GetStatus returns the response's HTTP status code.
func (r Response) GetStatus() int {
return r.status
}
// ContentType sets the response's content type header.
func (r Response) ContentType(ct string) Response {
return Response{
description: r.description,
status: r.status,
contentType: ct,
headers: r.headers,
model: r.model,
}
}
// Headers returns a new response with the named headers added. Sending
// headers to the client is optional, but they must be named here before
// you can send them.
func (r Response) Headers(names ...string) Response {
headers := r.headers
if headers == nil {
headers = []string{}
}
return Response{
description: r.description,
status: r.status,
contentType: r.contentType,
headers: append(headers, names...),
model: r.model,
}
}
// Model returns a new response with the given model representing the body.
// Because Go cannot pass types, `bodyModel` should be an instance of the
// response body.
func (r Response) Model(bodyModel interface{}) Response {
// Add a content type if none has been set. We prefer JSON since it's easy to
// represent in OpenAPI. Content negotiation means we also support other
// content types which the client can dynamically request.
ct := r.contentType
if ct == "" {
ct = "application/json"
}
// Allow the `Content-Type` header if not already allowed.
foundContentType := false
foundLink := false
for _, h := range r.headers {
if strings.ToLower(h) == "content-type" {
foundContentType = true
}
if strings.ToLower(h) == "link" {
foundLink = true
}
}
headers := r.headers
if !foundContentType {
headers = append(headers, "Content-Type")
}
if !foundLink {
headers = append(headers, "Link")
}
return Response{
description: r.description,
status: r.status,
contentType: ct,
headers: headers,
model: reflect.TypeOf(bodyModel),
}
}