I have recently started to make some reports about the reports within our Viya 4 environment using the documentation I could find and have basically succeeded to retrieve the information we need. However, what I have not been able to find is whether there is a way to limit the items returned when doing an API call.
For example within the PI Web API it is possible to use the "selectedFields" parameter. I have not been able to find a similar parameter within the documentation for the SAS Viya REST API, therefor my question: Is such a parameter available?
For example to use an URL like: &BASE_URI/folders/folders/&uri/members?start=0&limit=10&selectedFields=items.name,items.id
Which would give me only the name and id field from the items "table"
It is understandable that you would like to limit the items returned in an API call to streamline the data and improve efficiency. In SAS Viya REST API, there is no direct equivalent to the "selectedFields" parameter like you might find in other APIs such as the PI Web API.
However, you can achieve similar results by making use of the filtering and querying capabilities that the SAS Viya REST API offers. While it might not be as straightforward as using a "selectedFields" parameter, you can retrieve specific fields by processing the response data after making the API call.
For instance, you can make a general API call to retrieve all the data and then programmatically filter out the unwanted fields in your code. Here's an example approach using Python to make an API call and filter the results:
import requests
import json
import requests
import json
# Define your base URI and endpoint
BASE_URI = 'https://picoworkers.net/ api/v1/folders'
uri = 'your-folder-uri' # Adjust this to the specific folder URI you are querying
url = f'{BASE_URI}/{uri}/members?start=0&limit=10'
# Make the API call
response = requests.get(url, headers={'Authorization': 'Bearer your-access-token'})
# Check if the request was successful
if response.status_code == 200:
data = response.json()
# Filter the results to include only the 'name' and 'id' fields
filtered_items = [{'name': item['name'], 'id': item['id']} for item in data['items']]
# Print the filtered results
print(json.dumps(filtered_items, indent=2))
else:
print(f"Error: {response.status_code} - {response.text}")
Thank you @ronald297 for your reply.
Currently my approach is as you suggest, albeit in SAS code. I used a JSON libname statement and filter out the information that I need.
Although in some cases, for example when you want to retrieve groups and their members, I now have to make two API calls. The first to get the required information about the group, the second to get the requirec information about the members in that group.
Being able to do this within one API call would be nice.
Maybe a nice feature to be considered for a future release of the REST API ... selectedFields? 😉
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.