Details
Description
One issue that comes up often is when we have a service defined with some functions and we want to do Authentication & Authorization we need to manually call a function from inside the service implementation every time.
A decently popular question.
This ticket proposes to add a before_action (naming flexible) hook which will be called before the Service call method is executed. It can only raise the same errors as defined by the function.
It requires the generate_hooks option to be set
Example diff for Golang generated code for simple service:
exception NotAuthorisedException {
1: string errorMessage,
}
exception ApiException {
1: string errorMessage,
}
service MyService {
string myMethod(1: string authString, 2: string otherArgs ) throws ( 1: NotAuthorisedException e1, 2: ApiException e ),
}
+ // Called before any other action is called + BeforeAction(serviceName string, actionName string, args map[string]interface{}) (err error) + // Called if an action returned an error + ProcessError(err error) error } type MyServiceClient struct { @@ -391,7 +395,12 @@ func (p *myServiceProcessorMyMethod) Process(seqId int32, iprot, oprot thrift.TP result := MyServiceMyMethodResult{} var retval string var err2 error - if retval, err2 = p.handler.MyMethod(args.AuthString, args.OtherArgs_); err2 != nil { + err2 = p.handler.BeforeAction("MyService", "MyMethod", map[string]interface{}{"AuthString": args.AuthString, "OtherArgs_": args.OtherArgs_}) + if err2 == nil { + retval, err2 = p.handler.MyMethod(args.AuthString, args.OtherArgs_) + } + if err2 != nil { + err2 = p.handler.ProcessError(err2)
It simply calls BeforeAction, if it raises an error it does NOT procede with making the normal call.
Diffs Attached.