0

I would like create a REST endpoint which would allow a user to pass a query parameter that is incidentally a field of a taxonomy term. I am trying to achieve this by using Services Views in Drupal 7.

To simplify the problem, let's say I have a list of players and each player has a name and a skill level. For example, the unfiltered JSON output of my API would be

   [{"name": "John98","level": "Beginner"}, ...
    {"name": "PetePete", "level": "Advanced"}]

But the level is actually a taxonomy term, that also has a "min_points" integer field, for example 0 for "beginner" and 50 for "advanced" and 1000 for "expert".

In my API would like to do smth like http://example.com/api/players?points>45 and get all the players with levels that correspond to more than 45 points.

Is this possible to achieve? Thank you in advance for any input.

5
  • It is possible with the Views datasource's sub module called Views JSON. Essentially you just build the View as you normally would, add the exposed filter, then append that filter to the URL when making the call for the JSON data: api/players?points=45 Jun 23, 2018 at 13:33
  • Would it also be possible to do points>45?
    – Kabachok
    Jun 25, 2018 at 7:19
  • I have also seen that this module is not covered by the security policy, so I am not very confident about integrating it: drupal.org/project/views_datasource
    – Kabachok
    Jun 25, 2018 at 11:29
  • The = in the query string doesn't literally mean equality, it'll listen to whatever op you have selected on your exposed filter. So if your op is set to less than then the equal sign will listen to that op. As for the module not being covered by the security policy, I understand your concerns, but personally I've been using the module for about 7 years without issue in numerous production environments. Jun 25, 2018 at 15:52
  • @tyler.frankenstein Ok, thank you very much! Will try to solve my issue using your approach
    – Kabachok
    Jun 25, 2018 at 15:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.