# API Client (python)

Python API clients help you perform Labii API calls, such as authentication, get, patch, post, and delete.

## Usage <a href="#usage" id="usage"></a>

1. Install the Labii SDK

```
pip install labii-sdk
```

1. Import the package

```
from labii_sdk.api_client import APIObject
```

1. Initial the API object

```
api = APIObject()
```

## Initial API <a href="#initial-api" id="initial-api"></a>

During API initiation, the following parameters can be customized:

* `base_url`, the server base url, defaults to <https://www.labii.dev>. If you are using a different data center, make sure to change the `base_url` setting.
* `api_key`, the API Key to get authenticated.
* `organization__sid`, your organization sid. You can find it by logging into Labii (<https://www.labii.com/login/>) and going to Side menu -> Settings -> Organization -> Sid

```
api = APIObject(
    base_url="https://www.labii.dev",
    api_key="xxx"
    organization__sid=None
)
```

## Get list URL <a href="#get-list-url" id="get-list-url"></a>

Use this function to create a url for [list view](https://docs.labii.com/api/overview#list-view).

* `app` and `model` (required), the app and object to limit the type of data in Labii. Learn more at <https://docs.labii.com/api/overview#objects>.
* `level` (default to "organization"), the scope of records to retrieve. Learn more at <https://docs.labii.com/api/overview#levels>
* `sid` (default to "organization\_\_sid"), the static and encrypted Labii object id. It has to match with level.
* `serializer` (default to "list"), the scope of fields of the return data. Learn more at <https://docs.labii.com/api/overview#serializer>
  * **name**, return only sid and name, very fast
  * **list**, return selected fields of the objects, fast
  * **detail**, return all fields of the objects, slow
* `query` (optional), additional query to limit the results.

```
url = api.get_list_url(
    app="tables",
    model="section",
    level="organization",
    sid="xxx",
    serializer='list',
    query="is_archived=false"
)

# It can be simplified as:
url = api.get_list_url(
    app="tables",
    model="section"
)
```

## Get detail URL <a href="#get-detail-url" id="get-detail-url"></a>

Use this function to create a url for [detail view](https://docs.labii.com/api/overview#detail-view).

* `app` and `model` (required), same as above
* `sid` (required), the sid of the object.
* `query` (optional), passing additional parameters during querying.

```
url = api.get_detail_url(
    app="tables",
    model="section",
    sid="xxx",
    query=""
)
```

## Get headers <a href="#get-headers" id="get-headers"></a>

Return necessary headers for API functions.

* `is_authorized` (default to `True`), To use a header without authentication information, set `is_authorized=False`.

```
headers = api.get_headers(
    is_authorized=True
)
```

## Login <a href="#login" id="login"></a>

Get authentication token.

* `api.login()`
  * The user will be prompted to provide an email/password if they have not previously provided a credential.
  * When an email or password has already been provided, you will receive a new token.
* `api.login(email=xxx, password=xxx)`
  * Login with a provided email/password.

## Check token <a href="#check-token" id="check-token"></a>

Labii enforces the **Excessive Session Timeout** seurity (U.S. CNSS - CNSSI No. 1253 section AC-11). Your account will be logged out after 30 minutes of inactivity. Call this function to generate a new token if your application is continuously running or is performing queries after 30 minutes of inactivity.\
**Note:** For the function to work, you must run `api.login()` first.

```
labii.check_token()
```

## Post <a href="#post" id="post"></a>

A function to do POST for labii api

* `url` (str), the URL to post. Generate a standard URL with `get_list_url`.
* `data` (dict), a JSON dict data to post. Learn more at <https://docs.labii.com/api/methods>
* `is_authorized` (default to `True`), whether authorized tokens should be used.

```
api.post(
    url="xxx",
    data={},
    is_authorized=True
)
```

## Patch <a href="#patch" id="patch"></a>

A function to do PATCH for labii api

* `url` (str), the URL to post. Generate a standard URL with `get_detail_url`.
* `data` (dict), a JSON dict data to post. Learn more at <https://docs.labii.com/api/methods>
* `is_authorized` (default to `True`), whether authorized tokens should be used.

```
api.patch(
    url="xxx",
    data={},
    is_authorized=True
)
```

## Get <a href="#get" id="get"></a>

A function to do GET for labii api.

* `url` (str), the URL to post. Generate a standard URL with `get_list_url` or `get_detail_url`.
* `all_pages` (bool, default=False), Whether all pages should be returned. When False, only return the data for the first page. Only applies to list views.
* `is_authorized` (default to `True`), whether authorized tokens should be used.

```
api.get(
    url="xxx",
    all_pages=False
    is_authorized=True
)
```

## Delete <a href="#delete" id="delete"></a>

A function to do DELETE for labii api.

* `url` (str), the URL to post. Generate a standard URL with `get_detail_url`.
* `is_authorized` (default to `True`), whether authorized tokens should be used.

```
api.delete(
    url="xxx",
    is_authorized=True
)
```
