JS API: Getting metadata for a list of users without doing individual request for each of them


(Serge) #1

Hi,

Using the JS API, is there a way to get the metadata for 500 backend_user asset without doing a single API call for each?

I’m looking at batchRequest atm, that looks a bit odd as the function list isn’t an array. What is the best practice here for that kind of job?

Cheers


(Bart Banda) #2

Depends on what you are using it for and what the overall implementation looks like, but I would probably just use an Asset Listing to list the users and their needed metadata out instead. You could feed that list into a JS variable as well if you needed to, as you can control the formatting of the asset listing to be a JS array for example.
Then you could ajax that Asset Listing in for when you need it in your overall JS implementation.


(Serge) #3

just use an Asset Listing to list the users and their needed metadata

The issue here is that it seems like either getChildren nor getAssetTree return any metadata. To have metadata about an asset you need to launch a getMetadata call. In this case that would have to be done for every single individual user, making it 500 calls to get the metadata for each user in the list. This is going to slow things down significantly.

Is there a way to get a bunch of user metadata in one request? (something better than batchRequest an object with 500 entries for 500 function calls)


(Serge) #4

What I can fetch with getChildren

What I can fetch with getAssetTree

The metadata I need to get, which none of those functions return


(Peter McLeod) #5

Hi

We use the JS API a bit, mostly for backend admin tasks, lot of which involve getting and setting metadata.

What Bart suggested (using an asset listing) is what we generally do.

The asset listing can be set up to return specific data or accept dynamic parameters for what data it should return.

The listing format is structured to return a JSON array.

In the frontend JS get the JSON using an ajax call and begin iterating over the results when it’s received, for example, by calling Matrix JS API functions to update the metadata as needed.

For example:

Default format something like:

%asset_position^gt:1:,%{"assetid":"%asset_assetid%","something":"%asset_metadata_Something^json_encode%"}

Page contents:

[%asset_listing^replace:(\s+): %]

You can also set a dynamic root node/asset selection parameter as well if you want to make it more flexible.

Make sure content containers are raw html and there are no designs/layouts that are going to add html to the data. Can also add a design with a json mime type if needed.

Then just make a standard ajax request and iterate over the results.

Thanks
Peter


(Serge) #6

I see. Do you have an example of such a setup? (asset listing configuration screenshots, ajax calls, output, etc).

Also, is this method prone to have caching issues? (eg: not getting the latest results). Feels pretty clunky to have to create a web page to fetch a list of users & meta…


(Serge) #7

I have created a new child > asset listing page, went into details and can’t seem to find any option that would set something like “output format” to JSON. How do you tell an asset listing page to skip all automated html headers, footers, and spit out result in a usable json format?


(Serge) #8

Actually nevermind, seems like the rather large batchRequest takes even less time than listing children of a node …

getChildren of 1 asset folder taking 16s
batchRequest of 200 user metadata (200 function calls) taking 7s.


(Peter McLeod) #9

just need to:

  • create a new design asset

  • select edit parse file

  • for a json mime type set contents to:

<MySource_PRINT id_name="__global__" var="content_type" content_type="application/json" /><MySource_AREA id_name="page_body" design_area="body" />

  • then apply the design to the asset listing in the listing’s settings screen.

  • depending on how the data is going to be accessed you might want to also apply an empty paint layout to listing to be sure only the json data you are expecting is returned.

https://matrix.squiz.net/manuals/designs/chapters/global-variables#content-type

Thanks
Peter