How to add a rest endpoint for roles at /rest/data/roles

"John P. Rouillard" <[email protected]> Tue, 23 Apr 2024 23:17:45 -0400
Newsgroups gmane.comp.bug-tracking.roundup.devel
Message-ID <[email protected]>
Hello All:

I am working with a team to replicate the user classhelper for a
tracker. One of the things it has is a select/dropdown filter that is
populated with all the roles in the tracker.

The roles property for the User class in Roundup is a string. For this
purpose I would like to pretend that roles is a multilink to a "roles"
class.

I want: http://host/tracker/rest/data/roles to return something like:

  {
    "data": {
        "collection": [
            {
                "id": "user",
                "name": "user"
            },
            {
                "id": "admin",
                "name": "admin"
            },
            {
                "id": "anonymous",
                "name": "anonymous"
            }
        ]
    }
  }

I can add an endpoint at: http://host/tracker/rest/roles and get it to
work this way. However I would prefer to make it work at the
/data/roles endpoint as the JavaScript code has library routines to
gather and parse the returned data.

When I try using interfaces.py to set up the /data/roles endpoint:

  from roundup.rest import Routing, RestfulInstance,  _data_decorator

  class RestfulInstance:

      @Routing.route("/data/roles", 'GET')
      @_data_decorator
      def get_roles(self, input):
	  """Return all defined roles. The User class property
	     roles is a string but simulate it as a MultiLink
	     to an actual Roles class.
	  """
	  return 200, {"collection": [ {"id": rolename, "name": rolename}
		    for rolename in list(self.db.security.role.keys())]}

It looks like the rule that matches:

   /data/<:class_name>

is evaluated first and captures "roles" and reports that it's not a
class. It looks like the rest code just iterates over the list of
regexp's used to parse the URL and stops on the first match. If I
could get my "/data/roles" evaluated first it would work as I hope.
But there is no priority or other setting that I can see to make that
happen.

Anybody have a bright ideas on how to do what I want?

Thanks.

--
				-- rouilj
John Rouillard
===========================================================================
My employers don't acknowledge my existence much less my opinions.