rc: add NeedsResponse for rc calls
This commit is contained in:
parent
4884bee8ba
commit
1cae4152f9
3 changed files with 29 additions and 7 deletions
|
@ -91,7 +91,7 @@ func (p Params) Get(key string) (interface{}, error) {
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHTTPRequest gets a http.Request parameter associated with the request with the key "HttpRequest"
|
// GetHTTPRequest gets a http.Request parameter associated with the request with the key "_request"
|
||||||
//
|
//
|
||||||
// If the parameter isn't found then error will be of type
|
// If the parameter isn't found then error will be of type
|
||||||
// ErrParamNotFound and the returned value will be nil.
|
// ErrParamNotFound and the returned value will be nil.
|
||||||
|
@ -108,6 +108,23 @@ func (p Params) GetHTTPRequest() (*http.Request, error) {
|
||||||
return request, nil
|
return request, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetHTTPResponseWriter gets a http.ResponseWriter parameter associated with the request with the key "_response"
|
||||||
|
//
|
||||||
|
// If the parameter isn't found then error will be of type
|
||||||
|
// ErrParamNotFound and the returned value will be nil.
|
||||||
|
func (p Params) GetHTTPResponseWriter() (*http.ResponseWriter, error) {
|
||||||
|
key := "_response"
|
||||||
|
value, err := p.Get(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
request, ok := value.(*http.ResponseWriter)
|
||||||
|
if !ok {
|
||||||
|
return nil, ErrParamInvalid{errors.Errorf("expecting http.ResponseWriter value for key %q (was %T)", key, value)}
|
||||||
|
}
|
||||||
|
return request, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetString gets a string parameter from the input
|
// GetString gets a string parameter from the input
|
||||||
//
|
//
|
||||||
// If the parameter isn't found then error will be of type
|
// If the parameter isn't found then error will be of type
|
||||||
|
|
|
@ -265,6 +265,10 @@ func (s *Server) handlePost(w http.ResponseWriter, r *http.Request, path string)
|
||||||
in["_request"] = r
|
in["_request"] = r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if call.NeedsResponse {
|
||||||
|
in["_response"] = &w
|
||||||
|
}
|
||||||
|
|
||||||
// Check to see if it is async or not
|
// Check to see if it is async or not
|
||||||
isAsync, err := in.GetBool("_async")
|
isAsync, err := in.GetBool("_async")
|
||||||
if rc.NotErrParamNotFound(err) {
|
if rc.NotErrParamNotFound(err) {
|
||||||
|
|
|
@ -17,12 +17,13 @@ type Func func(ctx context.Context, in Params) (out Params, err error)
|
||||||
// Call defines info about a remote control function and is used in
|
// Call defines info about a remote control function and is used in
|
||||||
// the Add function to create new entry points.
|
// the Add function to create new entry points.
|
||||||
type Call struct {
|
type Call struct {
|
||||||
Path string // path to activate this RC
|
Path string // path to activate this RC
|
||||||
Fn Func `json:"-"` // function to call
|
Fn Func `json:"-"` // function to call
|
||||||
Title string // help for the function
|
Title string // help for the function
|
||||||
AuthRequired bool // if set then this call requires authorisation to be set
|
AuthRequired bool // if set then this call requires authorisation to be set
|
||||||
Help string // multi-line markdown formatted help
|
Help string // multi-line markdown formatted help
|
||||||
NeedsRequest bool // if set then this call will be passed the original request object as _request
|
NeedsRequest bool // if set then this call will be passed the original request object as _request
|
||||||
|
NeedsResponse bool // if set then this call will be passed the original response object as _response
|
||||||
}
|
}
|
||||||
|
|
||||||
// Registry holds the list of all the registered remote control functions
|
// Registry holds the list of all the registered remote control functions
|
||||||
|
|
Loading…
Add table
Reference in a new issue