Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4221

Add Rails like Before Action to Thrift

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Won't Do
    • None
    • None
    • Wish List
    • Patch Available
    • Patch

    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.

      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.

      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.

      Attachments

        1. t_go_generator.diff
          4 kB
          Devansh Gupta
        2. golang_generated.diff
          1 kB
          Devansh Gupta

        Activity

          People

            jking3 James E. King III
            devansh_gupta91 Devansh Gupta
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: