REST API Filter Request

I’m making a request using the REST API and I’m struggling to figure out why this is not working.

At the end of the day, how can i mimic what is happening in the Shotgun Search field when Keywords is enabled? I want to have this same searching feature using the REST API and or Python.

Here is what im trying to accomplish filter wise for collecting Assets. They must meet the following criteria:

  1. Collect all Assets that are in project 122
  2. The Asset’s code must contain ‘red brick’ or it must contain both tags for ‘red’ and ‘brick’

What I’m struggling to figure out is why the following does not work? It’s written as I’d expect. When I remove one of the Tag filters to only search for one, it works. But i know for sure I have items that contain both tags. I hope someone out there can help me out please :slight_smile:

{
  "logical_operator": "and",
  "conditions": [
    ['project.Project.id', 'is', 122],
    {
      "logical_operator": 'or',
      "conditions": [
        ['code', 'contains', 'red brick'],
        {
          'logical_operator': 'and',
          'conditions': [
            ['tags', 'name_contains', 'red'],
            ['tags', 'name_contains', 'brick']
          ]
        }
      ]
    }
  ]
}

Here is a complete snippet of code you can drop into a jsfiddle or something similar and test. Just replace the vars with your URL, Client ID and Client Secret for testing…

function queryShotgun() {
  var headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': 'application/json'
  };
  var host = 'https://?????.shotgunstudio.com';
  var client_id = '?????';
  var client_secret = '?????';

  $.ajax({
    url: `${host}/api/v1/auth/access_token`,
    method: 'post',

    headers: headers,
    data: {
      grant_type: 'client_credentials',
      client_id: client_id,
      client_secret: client_secret
    },
    success: function(data) {
      console.log(JSON.stringify(data));
      console.log(data);
      $('#accessOutput').html(data.access_token);

      var headers = {
        'Content-Type': 'application/vnd+shotgun.api3_hash+json',
        'Authorization': `Bearer ${data.access_token}`
      };
      var query = {
        fields: ['code', 'tags'],
        filters: {
          "logical_operator": "and",
          "conditions": [
            ['project.Project.id', 'is', 122],
            {
              "logical_operator": 'or',
              "conditions": [
                ['code', 'contains', 'red brick'],
                {
                  'logical_operator': 'and',
                  'conditions': [
                    ['tags', 'name_contains', 'red'],
                    ['tags', 'name_contains', 'brick']
                  ]
                }
              ]
            }
          ]
        },
        sort: ['-updated_at'],
        limit: 5
      };

      $.ajax({
        url: `${host}/api/v1/entity/Asset/_search`,
        method: 'post',
        headers: headers,
        data: `${JSON.stringify(query)}`,
        success: function(data) {
          console.log(JSON.stringify(data));
          $('#queryOutput').val(JSON.stringify(data, null, 3));
        },
        error: err => {
          console.log(err)
          $('#queryOutput').val('Error:' + err.responseText);
        },
      })
    }
  })
}

window.onload = function() {
  queryShotgun();
};
1 Like

I can’t even seem to get this to work in Python either as shown below…
I’ve tried to use two different filters, neither which worked and I can’t understand why.

def findAssets():
    fields = ['id', 'type', 'code', 'project', 'tags']
    
    # ATTEMPT 1 DOES NOT WORK
    filters = [
        ['project.Project.id', 'is', 122],
        {
            "filter_operator": "any",
            "filters": [
                ["code", "contains", 'red brick'],
                {
                    "filter_operator": "all",
                    "filters": [
                        ['tags', 'name_contains', 'red'],
                        ['tags', 'name_contains', 'brick']
                    ]
                }
            ]
        }
    ]
     
    # ATTEMPT 2 DOES NOT WORK
    filters = [
        ['project.Project.id', 'is', 122],
        {
            "filter_operator": "all",
            "filters": [
                ["tags", "name_contains", 'red'],
                ['tags', 'name_contains', 'brick']
            ]
        }
    ]

    assets = sg.find('Asset', filters, fields=fields)
    for x in assets:
        print x['code']