Split Collectin's add method to add and create methods.
[arvados.git] / sdk / go / httpserver / responsewriter.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package httpserver
6
7 import (
8         "net/http"
9 )
10
11 type ResponseWriter interface {
12         http.ResponseWriter
13         WroteStatus() int
14         WroteBodyBytes() int
15 }
16
17 // responseWriter wraps http.ResponseWriter and exposes the status
18 // sent, the number of bytes sent to the client, and the last write
19 // error.
20 type responseWriter struct {
21         http.ResponseWriter
22         wroteStatus    int   // Last status given to WriteHeader()
23         wroteBodyBytes int   // Bytes successfully written
24         err            error // Last error returned from Write()
25 }
26
27 func WrapResponseWriter(orig http.ResponseWriter) ResponseWriter {
28         return &responseWriter{ResponseWriter: orig}
29 }
30
31 func (w *responseWriter) WriteHeader(s int) {
32         w.wroteStatus = s
33         w.ResponseWriter.WriteHeader(s)
34 }
35
36 func (w *responseWriter) Write(data []byte) (n int, err error) {
37         n, err = w.ResponseWriter.Write(data)
38         w.wroteBodyBytes += n
39         w.err = err
40         return
41 }
42
43 func (w *responseWriter) WroteStatus() int {
44         return w.wroteStatus
45 }
46
47 func (w *responseWriter) WroteBodyBytes() int {
48         return w.wroteBodyBytes
49 }
50
51 func (w *responseWriter) Err() error {
52         return w.err
53 }