rc: add NeedsResponse for rc calls

This commit is contained in:
Chaitanya Bankanhal 2020-07-27 23:31:35 +05:30 committed by Nick Craig-Wood
parent 4884bee8ba
commit 1cae4152f9
3 changed files with 29 additions and 7 deletions

View File

@ -91,7 +91,7 @@ func (p Params) Get(key string) (interface{}, error) {
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
// ErrParamNotFound and the returned value will be nil.
@ -108,6 +108,23 @@ func (p Params) GetHTTPRequest() (*http.Request, error) {
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
//
// If the parameter isn't found then error will be of type

View File

@ -265,6 +265,10 @@ func (s *Server) handlePost(w http.ResponseWriter, r *http.Request, path string)
in["_request"] = r
}
if call.NeedsResponse {
in["_response"] = &w
}
// Check to see if it is async or not
isAsync, err := in.GetBool("_async")
if rc.NotErrParamNotFound(err) {

View File

@ -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
// the Add function to create new entry points.
type Call struct {
Path string // path to activate this RC
Fn Func `json:"-"` // function to call
Title string // help for the function
AuthRequired bool // if set then this call requires authorisation to be set
Help string // multi-line markdown formatted help
NeedsRequest bool // if set then this call will be passed the original request object as _request
Path string // path to activate this RC
Fn Func `json:"-"` // function to call
Title string // help for the function
AuthRequired bool // if set then this call requires authorisation to be set
Help string // multi-line markdown formatted help
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