forked from openshift/geard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_response.go
165 lines (144 loc) · 3.87 KB
/
job_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package http
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"github.com/openshift/geard/http/client"
"github.com/openshift/geard/jobs"
"github.com/openshift/geard/utils"
)
var (
ErrContentTypeDoesNotMatch = jobs.SimpleError{jobs.ResponseNotAcceptable, "The content type you requested is not available for this action."}
)
type TabularOutput interface {
WriteTableTo(io.Writer) error
}
type httpJobResponse struct {
response http.ResponseWriter
skipStreaming bool
mode client.ResponseContentMode
succeeded bool
failed bool
pending map[string]string
}
func NewHttpJobResponse(w http.ResponseWriter, skipStreaming bool, mode client.ResponseContentMode) jobs.Response {
return &httpJobResponse{
response: w,
skipStreaming: skipStreaming,
mode: mode,
}
}
func (s *httpJobResponse) StreamResult() bool {
return !s.skipStreaming
}
func (s *httpJobResponse) Success(t jobs.ResponseSuccess) {
s.success(t, false, false)
}
func (s *httpJobResponse) SuccessWithData(t jobs.ResponseSuccess, data interface{}) {
if s.mode == client.ResponseTable {
tabular, ok := data.(TabularOutput)
if !ok {
s.Failure(ErrContentTypeDoesNotMatch)
return
}
s.response.Header().Add("Content-Type", "text/plain")
s.success(t, false, true)
tabular.WriteTableTo(s.response)
return
}
s.response.Header().Add("Content-Type", "application/json")
s.success(t, false, true)
encoder := json.NewEncoder(s.response)
encoder.Encode(&data)
}
func (s *httpJobResponse) SuccessWithWrite(t jobs.ResponseSuccess, flush, structured bool) io.Writer {
if structured {
s.response.Header().Add("Content-Type", "application/json")
} else {
s.response.Header().Add("Content-Type", "text/plain")
}
s.success(t, !s.skipStreaming, false)
var w io.Writer
if s.skipStreaming {
w = ioutil.Discard
} else if flush {
w = utils.NewWriteFlusher(s.response)
} else {
w = s.response
}
return w
}
func (s *httpJobResponse) success(t jobs.ResponseSuccess, stream, data bool) {
if s.failed {
panic("Cannot call Success() after failure")
}
if s.succeeded {
panic("Cannot call Success() twice")
}
if s.pending != nil {
header := s.response.Header()
for key := range s.pending {
header.Add("x-"+key, s.pending[key])
}
s.pending = nil
}
s.succeeded = true
s.response.WriteHeader(s.statusCode(t, stream, data))
}
func (s *httpJobResponse) statusCode(t jobs.ResponseSuccess, stream, data bool) int {
switch {
case stream:
return http.StatusAccepted
case data:
return http.StatusOK
default:
return http.StatusNoContent
}
}
func (s *httpJobResponse) WritePendingSuccess(name string, value interface{}) {
if s.pending == nil {
s.pending = make(map[string]string)
}
if h, ok := value.(HeaderSerialization); ok {
s.pending[name] = h.ToHeader()
} else {
panic("Passed value does not implement HeaderSerialization for http")
}
}
func (s *httpJobResponse) Failure(err error) {
if s.succeeded {
panic("May not invoke failure after Success()")
}
if s.failed {
panic("May not write failure twice")
}
s.failed = true
code := http.StatusInternalServerError
response := client.HttpFailureResponse{err.Error(), nil, ""}
s.response.Header().Set("Content-Type", "application/json")
if e, ok := err.(jobs.JobError); ok {
response.Data = e.ResponseData()
switch e.ResponseFailure() {
case jobs.ResponseAlreadyExists:
code = http.StatusConflict
case jobs.ResponseNotFound:
code = http.StatusNotFound
case jobs.ResponseInvalidRequest:
code = http.StatusBadRequest
case jobs.ResponseNotAcceptable:
code = http.StatusNotAcceptable
case jobs.ResponseRateLimit:
code = 429 // http.statusTooManyRequests
}
}
s.response.WriteHeader(code)
json.NewEncoder(s.response).Encode(&response)
}
type HeaderSerialization interface {
ToHeader() string
}
type StringHeader string
func (s StringHeader) ToHeader() string {
return string(s)
}