How can I use the Palantir Foundry REST API to get a list of datasets within a directory

351 Views Asked by At

I need to check to see if a dataset exists within a Palantir Foundry directory, and if it doesn't exist, initiate the dataset creation process. I specifically want to look for a specified table name within the directory, and if it exists, return the dataset RID associated with that table. However, I'm having difficulty doing the first step. I have the following code:

def list_datasets_in_foundry_directory(
    token, 
    base_url, 
    parent_folder_rid):

    headers = {
        "authorization": "Bearer {}".format(token)
    }

    response = requests.get(f'{base_url}/api/v1/directories/{parent_folder_rid}/datasets', headers=headers)
    datasets = response.json()
    return datasets

But the response returns a 404 error.

1

There are 1 best solutions below

0
baobobs On

I found the answer in this StackOverflow post. Here is the working code:

def list_datasets_in_foundry_directory(
    parent_directory_rid,
    base_url,
    token):

"""
Returns the child objects of a compass folder.
Args:
    folder_rid: Compass folder rid,
    e.g. ri.compass.main.folder.f549ae09-9534-44c7-967a-6c86b2339231

Returns: (list of dict's) information about child objects

"""
response = requests.get(
    f"{base_url}/compass/api/folders/{parent_directory_rid}/children",
    headers={
        "content-type": "application/json",
        "authorization": f"Bearer {token}",
    },
)
response.raise_for_status()
response_as_json = response.json()
if (
    "nextPageToken" in response_as_json
    and response_as_json["nextPageToken"] is not None
):
    raise ValueError(
        f"{parent_directory_rid} has too many children "
        f"({len(response_as_json['values'])}), "
        f"paging currently not implemented."
    )
return response.json()["values"]