Skip to main content Link Menu Expand (external link) Document Search Copy Copied

SDK

A python library for building applications with Matatika services.


Installation

Prerequisites

  • Python >=3.7
  • pip

The Matatika CLI/SDK is available through PyPi here and can be installed using pip:

pip install matatika

You can check if the CLI/SDK installed successfully by running the following command:

matatika --version

The available classes and methods are documented below.


matatika.library

library module

MatatikaClient Objects

class MatatikaClient()

Class to handle client context

Arguments:

  • auth_token str - Authentication token
  • client_id str - Client ID
  • client_secret str - Client secret
  • endpoint_url str - Endpoint URL
  • workspace_id str - Workspace ID

Example:

Create a client with an access token

# create 'auth_token' variable

client = MatatikaClient(auth_token)

Create a client with client credentials

# create 'client_id' and 'client_secret' variables

client = MatatikaClient(client_id=client_id, client_secret=client_secret)

Create a client with an alternate API endpoint URL

# create 'auth_token' and 'endpoint_url' variables

client = MatatikaClient(auth_token, endpoint_url=endpoint_url)

Create a client with a target workspace

# create 'auth_token' and 'workspace_id' variables

client = MatatikaClient(auth_token, workspace_id=workspace_id)

auth_token

@property
def auth_token() -> str

Gets the client auth token

Returns:

  • str - Client auth token

Example:

# create MatatikaClient object

auth_token = client.auth_token
print(auth_token)

client_id

@property
def client_id() -> str

Gets the client ID

Returns:

  • str - Client ID

Example:

# create MatatikaClient object

client_id = client.client_id
print(client_id)

client_secret

@property
def client_secret() -> str

Gets the client secret

Returns:

  • str - Client secret

Example:

# create MatatikaClient object

client_secret = client.client_secret
print(client_secret)

endpoint_url

@property
def endpoint_url() -> str

Gets the client endpoint URL

Returns:

  • str - Client endpoint URL

Example:

# create MatatikaClient object

endpoint_url = client.endpoint_url
print(endpoint_url)

workspace_id

@property
def workspace_id() -> str

Gets the client workspace URL

Returns:

  • str - Client workspace URL

Example:

# create MatatikaClient object

workspace_id = client.workspace_id
print(workspace_id)

auth_token

@auth_token.setter
def auth_token(value: str)

Sets the client authentication token

Arguments:

  • value str - Authentication token

Example:

# create MatatikaClient object
# create 'auth_token' variable

client.auth_token = auth_token
print(client.auth_token)

client_id

@client_id.setter
def client_id(value: str)

Sets the client ID

Arguments:

  • value str - Client ID

Example:

# create MatatikaClient object
# create 'client_id' variable

client.client_id = client_id
print(client.client_id)

client_secret

@client_secret.setter
def client_secret(value: str)

Sets the client secret

Arguments:

  • value str - Client secret

Example:

# create MatatikaClient object
# create 'client_secret' variable

client.client_secret = client_secret
print(client.client_secret)

endpoint_url

@endpoint_url.setter
def endpoint_url(value: str)

Sets the client endpoint URL

Arguments:

  • value str - Endpoint URL

Example:

# create MatatikaClient object
# create 'endpoint_url' variable

client.endpoint_url = endpoint_url
print(client.endpoint_url)

workspace_id

@workspace_id.setter
def workspace_id(value: str)

Sets the client workspace ID

Arguments:

  • value str - Workspace ID

Example:

# create MatatikaClient object
# create 'workspace_id' variable

client.workspace_id = workspace_id
print(client.workspace_id)

profile

def profile() -> dict

Gets the authenticated user profile

Returns:

  • dict - Authenticated user profile

Example:

# create MatatikaClient object

profile = client.profile()

print(profile['id'])
print(profile['name'])
print(profile['email'])

publish

def publish(resource_type,
            resources: List[object]) -> List[Tuple[object, int]]

Publishes objects

Arguments:

  • objects List[object] - objects to publish

Returns:

  • List[Tuple[object,int]] - Published objects and status actions

Example:

# create MatatikaClient object
# create 'objects' variable
responses = client.publish(objects)
for object, status_code in responses:
    print(
        f"[{status_code}]	Successfully published the object {object.object_id}")

list_resources

def list_resources(resource: Resource) -> Union[list, None]

Lists all available resources of the specified type

Arguments:

  • resource_type Resource - Resource type to return (workspaces/datasets)

Returns:

  • Union[list,None] - Available resources

Examples:

List all workspaces

# create MatatikaClient object

from matatika.types import Resource

workspaces = client.list_resources(Resource.WORKSPACE)

for workspace in workspaces:
    print(workspace['id'], workspace['name'], workspace['domains'])

List all datasets in the workspace provided upon client object instantiation

# create MatatikaClient object

from matatika.types import Resource

datasets = client.list_resources(Resource.DATASET)

for dataset in datasets:
    print(dataset['id'], dataset['alias'], dataset['title'])

List all datasets in the workspace ‘c6db37fd-df5e-4ac6-8824-a4608932bda0’

# create MatatikaClient object

client.workspace_id = '8566fe13-f30b-4536-aecf-b3879bd0910f'
datasets = client.list_resources('datasets')

for dataset in datasets:
    print(dataset['id'], dataset['alias'], dataset['title'])

delete_resources

def delete_resources(resource_type: Resource, *resource_ids) -> None

Deletes a resource of the specified type

Arguments:

  • resource_type Resource - Resource type to delete (dataset)
  • resource_id str - Resource ID

Returns:

None

Examples:

Delete a workspace

# create MatatikaClient object
# create 'workspace_id' variable

from matatika.types import Resource

client.delete_resources(Resource.WORKSPACE, workspace_id)
print(f"Successfully deleted workspace {workspace_id}")

Delete multiple workspaces

# create MatatikaClient object
# create 'workspace1_id', 'workspace2_id' and 'workspace3_id' variables

from matatika.types import Resource

client.delete_resources(Resource.WORKSPACE, workspace1_id, workspace2_id, workspace3_id)
print(f"Successfully deleted workspaces: {workspace1_id}, {workspace2_id}, {workspace3_id}")

Delete a dataset

# create MatatikaClient object
# create 'dataset_id' variable

from matatika.types import Resource

client.delete_resources(Resource.DATASET, dataset_id)
print(f"Successfully deleted dataset {dataset_id}")

Delete multiple datasets

# create MatatikaClient object
# create 'dataset1_id', 'dataset2_id' and 'dataset3_id' variables

from matatika.types import Resource

client.delete_resources(Resource.DATASET, dataset1_id, dataset2_id, dataset3_id)
print(f"Successfully deleted datasets: {dataset1_id}, {dataset2_id}, {dataset3_id}")

fetch

def fetch(dataset_id_or_alias: str,
          data_format: DataFormat = None) -> Union[dict, list, str]

Fetches the data of a dataset using the query property

Arguments:

  • dataset_id_or_alias str - Dataset ID or alias
  • data_format DataFormat, optional - Format to return the data as (defaults to a native Python object)

Returns:

  • Union[dict,list,str] - Dataset data

Examples:

Fetch data as a native Python object

# create MatatikaClient object
# create 'dataset_id_or_alias' variable

data = client.fetch(dataset_id_or_alias)

if data:
    print(data)
else:
    print(f"No data was found for dataset {dataset_id_or_alias}")

Fetch data as a raw string

# create MatatikaClient object
# create 'dataset_id_or_alias' variable

from matatika.types import DataFormat

data = client.fetch(dataset_id_or_alias, data_format=DataFormat.RAW)

if data:
    print(data)
else:
    print(f"No data was found for dataset {dataset_id_or_alias}")

Fetch data formatted as per the Chart.js specification

# create MatatikaClient object
# create 'dataset_id_or_alias' variable

from matatika.types import DataFormat

data = client.fetch(dataset_id_or_alias,
                    data_format=DataFormat.CHARTJS)

if data:
    print(data)
else:
    print(f"No data was found for dataset {dataset_id_or_alias}")

Fetch data in CSV format

# create MatatikaClient object
# create 'dataset_id_or_alias' variable

from matatika.types import DataFormat

data = client.fetch(dataset_id_or_alias, data_format=DataFormat.CSV)

if data:
    print(data)
else:
    print(f"No data was found for dataset {dataset_id_or_alias}")

get_dataset

def get_dataset(dataset_id_or_alias: str, raw: bool = False) -> Dataset

Gets a dataset

Arguments:

  • dataset_id_or_alias(str) - Dataset ID or alias raw(bool, optional): Whether to return the dataset as a raw string or not (defaults to False)

Returns:

  • Dataset - Dataset object

Examples:

Fetch a dataset as a Dataset object

# create MatatikaClient object
# create 'dataset_id_or_alias' variable

dataset = client.get_dataset(dataset_id_or_alias)
print(dataset)

Fetch a dataset as a raw string

# create MatatikaClient object
# create 'dataset_id_or_alias' variable

dataset = client.get_dataset(dataset_id_or_alias, raw=True)
print(dataset)