This repository has been archived by the owner on May 2, 2020. It is now read-only.
forked from stretchr/goweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_responder.go
74 lines (59 loc) · 2.69 KB
/
api_responder.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
package responders
import (
codecsservices "github.com/tylerb/codecs/services"
"github.com/tylerb/goweb/context"
)
const (
DefaultCallbackParameter string = "callback"
)
var (
CallbackParameter string = DefaultCallbackParameter
)
/*
APIResponder represents objects capable of provide API responses.
Note that when one of the response methods is called, it may
(depending on the request) add data to the map returned by
context.Context.CodecOptions() before passing it off to the chosen
codec's Marshal method.
*/
type APIResponder interface {
/*
Codec services
*/
// SetCodecService sets the codec service to use.
SetCodecService(codecsservices.CodecService)
// GetCodecService gets the codec service that will be used by this object.
GetCodecService() codecsservices.CodecService
/*
Transformers
*/
// TransformStandardResponseObject transforms the standard response object before it is written to the response if a
// transformer func has been set via SetStandardResponseObjectTransformer. Otherwise, it just returns the same
// object that is passed in.
TransformStandardResponseObject(ctx context.Context, object interface{}) (interface{}, error)
// SetStandardResponseObjectTransformer sets the function to use to transform the standard response object before it is
// written to the response.
//
// You should use this function to control what kind of response your API makes.
SetStandardResponseObjectTransformer(transformer func(ctx context.Context, object interface{}) (interface{}, error))
/*
Responding
*/
// Responds to the Context with the specified status, data and errors.
Respond(ctx context.Context, status int, data interface{}, errors []string) error
// WriteResponseObject writes the status code and response object to the HttpResponseWriter in
// the specified context, in the format best suited based on the
// request. In certain cases, some data may be added to the
// passed in context.Context value's CodecOptions() value.
//
// Goweb uses the WebCodecService to decide which codec to use when responding
// see http://godoc.org/github.com/tylerb/codecs/services#WebCodecService for more information.
//
// This method should be used when the Goweb Standard Response Object does not satisfy the needs of
// the API, but other Respond* methods are recommended.
WriteResponseObject(ctx context.Context, status int, responseObject interface{}) error
// RespondWithData responds with the specified data, no errors and a 200 StatusOK response.
RespondWithData(ctx context.Context, data interface{}) error
// RespondWithError responds with the specified error message and status code.
RespondWithError(ctx context.Context, status int, err string) error
}