Internally Parsing the API

🚧

Please Note:

This sections is targeted at developers who are contributing to the Snipe-IT core itself, not those who are using the API to build external applications that interface with Snipe-IT. For documentation on consuming the API from a third-party system or script, check out the reference documentation.

Snipe-IT uses the fantastic (if roughly documented) Bootstrap-Table library from wenzhixin to handle all list views in the web GUI. Starting in version 4.0 of Snipe-IT, we've dramatically changed how the JSON responses generated by Snipe-IT are consumed.

Version 4.0 cleans up the JSON, and offloads the front-end formatting to the Bootstrap Tables data formatters, which allows us to keep our JSON clean (and smaller), while still delivering the front-end the way you'd expect.

The most common formatters are located in the resources/views/partials/bootstrap-tables.blade.php file, which initializes the table with various options, and also contains the formatters that are most commonly used by most if not all list views. You'll see that we dynamically generate some of the formatters, specifically the ones that we know are used just about everywhere, and then we also have a few exceptions.

The generic formatter builder looks like this:

// This only works for model index pages because it uses the row's model ID
    function genericRowLinkFormatter(destination) {
        return function (value,row) {
            if (value) {
                return '<a href="{{ url('/') }}/' + destination + '/' + row.id + '"> ' + value + '</a>';
            }
        };
    }

    // Use this when we're introspecting into a column object and need to link
    function genericColumnObjLinkFormatter(destination) {
        return function (value,row) {
            if ((value) && (value.name)) {
                return '<a href="{{ url('/') }}/' + destination + '/' + value.id + '"> ' + value.name + '</a>';
            }
        };
    }

(You should never have to change this part.)

After that, you'll see that we loop through the most common types of items in Snipe-IT, and generate their formatters:

var formatters = [
        'hardware',
        'accessories',
        'consumables',
        'components',
        'locations',
        'users',
        'manufacturers',
        'statuslabels',
        'models',
        'licenses',
        'categories',
        'suppliers',
        'companies',
        'depreciations',
        'fieldsets',
        'groups'
    ];

    for (var i in formatters) {
        window[formatters[i] + 'LinkFormatter'] = genericRowLinkFormatter(formatters[i]);
        window[formatters[i] + 'LinkObjFormatter'] = genericColumnObjLinkFormatter(formatters[i]);
        window[formatters[i] + 'ActionsFormatter'] = genericActionsFormatter(formatters[i]);
        window[formatters[i] + 'InOutFormatter'] = genericCheckinCheckoutFormatter(formatters[i]);
    }

(You should never have to change this either.)

The key takeway here is that you have two main types of formatters other than the one-offs. LinkFormatter and LinkObjFormatter. The important thing to note here is that the LinkFormatter looks at the row of data, whereas LinkObjFormatter introspects into a JSON object.

For example, if I were on the Users listing page, I would invoke the usersLinkFormatter to format the user name, since the entire row contains information about a single user.

If I were on the assets listing page, I would use the usersLinkObjFormatter to create a link and display the name of the user.

This is because the JSON being returned when querying assets includes the user data in a compact JSON object that is a subset of the asset object, and it needs to be walked through.


What’s Next