Query an endpoint
Overview
Previously we have been able to use curl commands and swagger docs to connect and query the TilliT API. Now we will explore some examples of querying the TilliT API in high level programming languages and change the number of results we recieve.
Get an Asset
In Javascript, it is best to use the node-fetch package to simplify the http requests we make. The following code snippet will retrieve the assets in your account and print them out.
An example in Python can be found below
Query Pagination with Page and Size
Depending on how many assets you have previously created, you may notice that not ALL assets have been returned. By default every GET request will only retrieve the first 20 entries. We can change this by instead changing the request to include the size param, see the following example
We can then combine this with page to get the next two results after the initial 'page'. The prior query is equivalent to page=0
Create an Asset
We can create an asset using the POST method, for this we will need a payload body. We can find out the shape of the payload required by searching the swagger docs for the /core/assets POST request. See the below image.
Now we can form the request. You will need to find the correct assetClass and site id for your database.
We can confirm the above has worked by observing three things. 1. The status code returned is 200 2.We receive the entity with id in the response and 3. we can see the new Asset inside of TilliT.
Handling Errors
But what if something goes wrong? See the below request for what is a bad example.
This will cause a 500 status code. The response object will detail exactly why the error occured, see below.
This message is telling us that the entity Asset was supplied an invalid name, it must be a non null string. If we fix this, we get the following error.
This message shows that asset class we supplied is invalid, it must be an id reference to. Note that assetClass: {id: 1} will work. Now we we have fixed all the errors
Last updated