Devansh Gupta created THRIFT-4221: ------------------------------------- Summary: Add Rails like Before Action to Thrift Key: THRIFT-4221 URL: https://issues.apache.org/jira/browse/THRIFT-4221 Project: Thrift Issue Type: New Feature Components: Wish List Affects Versions: 1.0 Reporter: Devansh Gupta Fix For: 1.0
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. See: https://stackoverflow.com/questions/4621715/how-to-handle-authentication-and-authorization-with-thrift 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. Example diff for Golang generated code for simple service: {code} 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 ), } {code} {code} + // 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) {code} It simply calls BeforeAction, if it raises an error it does NOT procede with making the normal call. Diffs Attached. -- This message was sent by Atlassian JIRA (v6.3.15#6346)