# py-directus
## Disclaimer: Under development
> This is very early in development and the API is subject to change.
>
> If you intend to use this library in a production environment, expect undocumented changes and bugs until the first
> stable release.
>
> If you find any issues or have any suggestions, please open an issue or a pull request.
Documentation [here](https://panos-stavrianos.github.io/py-directus/)
py-directus is a Python wrapper for asynchronous interaction with the Directus headless CMS API. It provides a convenient and
easy-to-use interface for performing CRUD operations, querying data, and managing resources in Directus.
## Features
- Asynchronous
- Login and authentication handling
- Reading and writing data from Directus collections
- Filtering, sorting, and searching data
- Aggregating data using aggregation operators
- Creating, updating, and deleting items in Directus collections
- Handling multiple users in the same session
Dependencies:
- [Pydantic](https://pydantic-docs.helpmanual.io/): This library leverages Pydantic for data validation and parsing. Pydantic is a powerful tool in Python for ensuring data integrity and handling data validation with ease.
- [HTTPX](https://www.python-httpx.org/): The library utilizes HTTPX, a fully featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2.
> Directus API:
> This library interacts with the [Directus API](https://docs.directus.io/reference/introduction.html).
>
> To make the most of this library, it is highly recommended to familiarize yourself with the Directus API documentation. Understanding the API's capabilities and endpoints will help you effectively utilize this library for seamless integration with Directus.
## Installation
You can install the library directly from [pypi](https://pypi.org/project/py-directus/) using pip:
```shell
$ pip install py-directus
```
> FastAPI support requires additional dependencies installation.
> You can install them along others like this:
```shell
$ pip install py-directus[FastAPI]
```
## Authentication and Session Handling
### Login
Create a Directus instance using email and password
```python
from py_directus import Directus
directus = await Directus("https://example.com", email="user@example.com", password="secret")
```
Alternatively create a Directus instance using the static token
```python
from py_directus import Directus
directus = await Directus("https://example.com", token="static_token")
```
Another way is to use the `with` statement to automatically logout when the session ends
```python
async with Directus(url, email, password) as directus:
# Manually login
await directus.login()
# Manually start cache
await directus.start_cache()
# do stuff
# OR
async with await Directus(url, email, password) as directus:
# do stuff
```
### Refresh Token
If you want to refresh the token you can use the `refresh` method
```python
await directus.refresh()
```
### Logout
Logout from Directus
```python
await directus.logout()
```
### Multiple Users in the Same Session
You can use multiple users in the same session by creating a new Directus instance by passing the client object
```python
connection = httpx.AsyncClient()
directus1 = await Directus(url, token=token, connection=connection)
directus2 = await Directus(url, email=email, password=password, connection=connection)
```
## Collections
There are two ways to set a collection, either by passing the collection name as a string
or by passing the collection as a Pydantic model.
Using the `collection` method you can pass the collection name as a string
```python
directus.collection("directus_users")
```
Or you can pass the collection as a `Pydantic` model
```python
from typing import Optional
from pydantic import ConfigDict
from py_directus.models import DirectusModel
class User(DirectusModel):
id: Optional[str]
first_name: Optional[str]
last_name: Optional[str]
avatar: Optional[str]
description: Optional[str]
email: Optional[str]
role: Optional[str] | Optional[Role]
status: Optional[str]
title: Optional[str]
token: Optional[str]
model_config = ConfigDict(collection="directus_users")
directus.collection(User)
```
> Don't forget to set the `collection` attribute in the `model_config` attribute
If you go with the second option, you will get the responses as `Pydantic` models (auto parsing)
> The `collection` method returns a `DirectusRequest` object which is used to perform READ, CREATE,
> UPDATE and DELETE operations
## Reading Data
When you have the `DirectusRequest` object you can use the `read` method to get the data.
This will return a `DirectusResponse` object which contains the data.
> Imporatnt note: The `read` method must be awaited
```python
await directus.collection("directus_users").read()
```
### Filtering
For an easy equality filter you can pass the field name and the value
```python
await directus.collection("directus_users").filter(first_name="John").read()
```
To add multiple equality filters you can chain the `filter` method
```python
await directus.collection("directus_users")
.filter(first_name="John")
.filter(last_name="Doe").read()
```
Using it like this you chain the filters with `AND` operator
#### F objects
To define complex logic in filters, use the `F` object
```python
from py_directus import F
await directus.collection("directus_users")
.filter(
(F(first_name="John") | F(first_name="Jane"))
& F(last_name="Doe")
).read()
```
> Important note: The `F` object does not support negation
### Sorting
You can sort the data by passing the field name to the `sort` method
```python
await directus.collection("directus_users").sort("first_name", asc=True).read()
```
To add multiple sorting fields you can chain the `sort` method
```python
await directus.collection("directus_users")
.sort("first_name", asc=True)
.sort("last_name", asc=False).read()
```
### Limiting
You can limit the data by passing the limit to the `limit` method
```python
await directus.collection("directus_users").limit(10).read()
```
### Aggregation
Aggregate the number of records in the query
```python
await directus.collection("directus_users").aggregate().read()
# OR
await directus.collection("directus_users").aggregate(count="*").read()
```
To add multiple aggregates you can chain the `aggregate` method
```python
await directus.collection("products")
.aggregate(countDistinct="id")
.aggregate(sum="price").read()
```
#### Agg objects
You can aggregate the data by defining the needed aggregation with the `Agg` class and passing it to the `aggregate` method
```python
from py_directus.aggregator import Agg
agg_obj = Agg(operator=AggregationOperators.Count)
await directus.collection("directus_users").aggregate(agg_obj).read()
```
In case you need only certain fields
```python
from py_directus.aggregator import Agg
amount_agg = Agg(operator=AggregationOperators.Sum, fields="amount")
await directus.collection("transactions").aggregate(amount_agg).read()
```
The available aggregation operators are:
- Count
- CountDistinct
- CountAll (Only in GraphQL)
- Sum
- SumDistinct
- Average
- AverageDistinct
- Minimum
- Maximum
### Grouping
You can group the data by passing the field names to the `group_by` method
```python
await directus.collection("directus_users").group_by("first_name", "last_name").read()
```
### Searching
You can search the data by passing the search term to the `search` method
```python
await directus.collection("directus_users").search("John").read()
```
### Selecting Fields
You can select the fields you want to get by passing the field names to the `fields` method
```python
await directus.collection("directus_users").fields("first_name", "last_name").read()
```
### Getting the Count Metadata
You can get the count of the data (total count and filtered count) calling `include_count`
```python
await directus.collection("directus_users").include_count().read()
```
## CRUD
### Retrieving items
After you call `read()` you get a `DirectusResponse` object which contains the data.
- `item` for single item
- `items` for multiple items
Getting the data as a dictionary or a list of dictionaries
```python
response = await directus.collection("directus_users").read()
print(response.item["first_name"])
print(response.items)
```
If you provide the `collection` method a `Pydantic` model you will get the data as a `Pydantic` object or a list of `Pydantic` objects
```python
response = await directus.collection(User).read()
print(response.item.first_name)
print(response.items)
```
### Converting to Models (pydantic) or to Dictionary
Apart from the auto parsing, you can manually convert the data to a `Pydantic` model instance or to a dictionary using:
- `item_as(User)` or `items_as(User)`
- `item_as_dict()` or `items_as_dict()`
```python
response = await directus.collection("directus_users").read()
print(response.item_as(User))
response = await directus.collection(User).read()
print(response.item_as_dict())
```
### Creating Items
The library does not support `Pydantic` models for creation, you have to pass a dictionary
- create(items: dict|List[dict])
```python
await directus.collection("directus_users").create({
"first_name": "John", "last_name": "Doe"
})
# OR
await directus.collection("directus_users").create(
[
{"first_name": "John", "last_name": "Doe"},
{"first_name": "Jane", "last_name": "Doe"}
]
)
```
### Updating Items
The library do not support `Pydantic` models for updating, you have to pass a dictionary
- `update(ids: str|int, items: dict)`
- `update(ids: List[str|int], items: List[dict])`
```python
await directus.collection("directus_users").update(1, {
"first_name": "Red",
"last_name": "John"
})
# OR
await directus.collection("directus_users").update(
[1, 2],
[
{"first_name": "Jean-Luc"},
{"first_name": "Jane", "last_name": "Doe"}
]
)
```
### Deleting Items
- `delete(ids: str|int|List[str|int])`
```python
await directus.collection("directus_users").delete(1)
# OR
await directus.collection("directus_users").delete([1, 2])
```
> Supporting `Pydantic` models for `create`/`update`/`delete` item operations is shortly coming.
## Examples
> Examples are not included with the `pypi` package, so you will have to download them separately and execute in a virtual environment.
Run individual examples as such:
```shell
python -m examples.<example_file_name>
```
## Tests
Run tests as such:
```shell
# All unit tests
python -m unittest discover -s tests/unit
# All integration tests
python -m unittest discover -s tests/integration
```
Raw data
{
"_id": null,
"home_page": "https://github.com/panos-stavrianos/py-directus",
"name": "py-directus",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "python, directus, async, asyncio, api, wrapper",
"author": "Panos Stavrianos",
"author_email": "panos@orbitsystems.gr",
"download_url": "https://files.pythonhosted.org/packages/bc/b4/27fe586f4b9e90eb91e3e7dfdf7d884cfedbfe28c9c504da496b74936026/py_directus-0.0.30.tar.gz",
"platform": null,
"description": "# py-directus\n\n## Disclaimer: Under development\n\n> This is very early in development and the API is subject to change.\n>\n> If you intend to use this library in a production environment, expect undocumented changes and bugs until the first\n> stable release.\n>\n> If you find any issues or have any suggestions, please open an issue or a pull request.\n\nDocumentation [here](https://panos-stavrianos.github.io/py-directus/)\n\npy-directus is a Python wrapper for asynchronous interaction with the Directus headless CMS API. It provides a convenient and\neasy-to-use interface for performing CRUD operations, querying data, and managing resources in Directus.\n\n## Features\n\n- Asynchronous\n- Login and authentication handling\n- Reading and writing data from Directus collections\n- Filtering, sorting, and searching data\n- Aggregating data using aggregation operators\n- Creating, updating, and deleting items in Directus collections\n- Handling multiple users in the same session\n\nDependencies:\n- [Pydantic](https://pydantic-docs.helpmanual.io/): This library leverages Pydantic for data validation and parsing. Pydantic is a powerful tool in Python for ensuring data integrity and handling data validation with ease.\n\n- [HTTPX](https://www.python-httpx.org/): The library utilizes HTTPX, a fully featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2.\n\n> Directus API:\n> This library interacts with the [Directus API](https://docs.directus.io/reference/introduction.html).\n> \n> To make the most of this library, it is highly recommended to familiarize yourself with the Directus API documentation. Understanding the API's capabilities and endpoints will help you effectively utilize this library for seamless integration with Directus.\n\n## Installation\n\nYou can install the library directly from [pypi](https://pypi.org/project/py-directus/) using pip:\n\n```shell\n$ pip install py-directus\n```\n\n> FastAPI support requires additional dependencies installation. \n> You can install them along others like this:\n\n```shell\n$ pip install py-directus[FastAPI]\n```\n\n## Authentication and Session Handling\n\n### Login\n\nCreate a Directus instance using email and password\n\n```python\nfrom py_directus import Directus\n\ndirectus = await Directus(\"https://example.com\", email=\"user@example.com\", password=\"secret\")\n```\n\nAlternatively create a Directus instance using the static token\n\n```python\nfrom py_directus import Directus\n\ndirectus = await Directus(\"https://example.com\", token=\"static_token\")\n```\n\nAnother way is to use the `with` statement to automatically logout when the session ends\n\n```python\nasync with Directus(url, email, password) as directus:\n # Manually login\n await directus.login()\n # Manually start cache\n await directus.start_cache()\n # do stuff\n\n# OR\n\nasync with await Directus(url, email, password) as directus:\n # do stuff\n```\n\n### Refresh Token\n\nIf you want to refresh the token you can use the `refresh` method\n\n```python\nawait directus.refresh()\n```\n\n### Logout\n\nLogout from Directus\n\n```python\nawait directus.logout()\n```\n\n### Multiple Users in the Same Session\n\nYou can use multiple users in the same session by creating a new Directus instance by passing the client object\n\n```python\nconnection = httpx.AsyncClient()\ndirectus1 = await Directus(url, token=token, connection=connection)\ndirectus2 = await Directus(url, email=email, password=password, connection=connection)\n```\n\n## Collections\n\nThere are two ways to set a collection, either by passing the collection name as a string\nor by passing the collection as a Pydantic model.\n\nUsing the `collection` method you can pass the collection name as a string\n\n```python\ndirectus.collection(\"directus_users\")\n```\n\nOr you can pass the collection as a `Pydantic` model\n\n```python\nfrom typing import Optional\n\nfrom pydantic import ConfigDict\n\nfrom py_directus.models import DirectusModel\n\n\nclass User(DirectusModel):\n id: Optional[str]\n first_name: Optional[str]\n last_name: Optional[str]\n avatar: Optional[str]\n description: Optional[str]\n email: Optional[str]\n role: Optional[str] | Optional[Role]\n status: Optional[str]\n title: Optional[str]\n token: Optional[str]\n\n model_config = ConfigDict(collection=\"directus_users\")\n\n\ndirectus.collection(User)\n```\n\n> Don't forget to set the `collection` attribute in the `model_config` attribute\n\nIf you go with the second option, you will get the responses as `Pydantic` models (auto parsing)\n\n> The `collection` method returns a `DirectusRequest` object which is used to perform READ, CREATE,\n> UPDATE and DELETE operations\n\n## Reading Data\n\nWhen you have the `DirectusRequest` object you can use the `read` method to get the data.\nThis will return a `DirectusResponse` object which contains the data.\n\n> Imporatnt note: The `read` method must be awaited\n\n```python\nawait directus.collection(\"directus_users\").read()\n```\n\n### Filtering\n\nFor an easy equality filter you can pass the field name and the value\n\n```python\nawait directus.collection(\"directus_users\").filter(first_name=\"John\").read()\n```\n\nTo add multiple equality filters you can chain the `filter` method\n\n```python\nawait directus.collection(\"directus_users\")\n.filter(first_name=\"John\")\n.filter(last_name=\"Doe\").read()\n```\n\nUsing it like this you chain the filters with `AND` operator\n\n#### F objects\n\nTo define complex logic in filters, use the `F` object\n\n```python\nfrom py_directus import F\n\nawait directus.collection(\"directus_users\")\n.filter(\n (F(first_name=\"John\") | F(first_name=\"Jane\")) \n & F(last_name=\"Doe\")\n).read()\n```\n\n> Important note: The `F` object does not support negation\n\n### Sorting\n\nYou can sort the data by passing the field name to the `sort` method\n\n```python\nawait directus.collection(\"directus_users\").sort(\"first_name\", asc=True).read()\n```\n\nTo add multiple sorting fields you can chain the `sort` method\n\n```python\nawait directus.collection(\"directus_users\")\n.sort(\"first_name\", asc=True)\n.sort(\"last_name\", asc=False).read()\n```\n\n### Limiting\n\nYou can limit the data by passing the limit to the `limit` method\n\n```python\nawait directus.collection(\"directus_users\").limit(10).read()\n```\n\n### Aggregation\n\nAggregate the number of records in the query\n\n```python\nawait directus.collection(\"directus_users\").aggregate().read()\n\n# OR\n\nawait directus.collection(\"directus_users\").aggregate(count=\"*\").read()\n```\n\nTo add multiple aggregates you can chain the `aggregate` method\n\n```python\nawait directus.collection(\"products\")\n.aggregate(countDistinct=\"id\")\n.aggregate(sum=\"price\").read()\n```\n\n#### Agg objects\n\nYou can aggregate the data by defining the needed aggregation with the `Agg` class and passing it to the `aggregate` method\n\n```python\nfrom py_directus.aggregator import Agg\n\nagg_obj = Agg(operator=AggregationOperators.Count)\n\nawait directus.collection(\"directus_users\").aggregate(agg_obj).read()\n```\n\nIn case you need only certain fields\n\n```python\nfrom py_directus.aggregator import Agg\n\namount_agg = Agg(operator=AggregationOperators.Sum, fields=\"amount\")\n\nawait directus.collection(\"transactions\").aggregate(amount_agg).read()\n```\n\nThe available aggregation operators are:\n\n- Count\n- CountDistinct\n- CountAll (Only in GraphQL)\n- Sum\n- SumDistinct\n- Average\n- AverageDistinct\n- Minimum\n- Maximum\n\n### Grouping\n\nYou can group the data by passing the field names to the `group_by` method\n\n```python\nawait directus.collection(\"directus_users\").group_by(\"first_name\", \"last_name\").read()\n```\n\n### Searching\n\nYou can search the data by passing the search term to the `search` method\n\n```python\nawait directus.collection(\"directus_users\").search(\"John\").read()\n```\n\n### Selecting Fields\n\nYou can select the fields you want to get by passing the field names to the `fields` method\n\n```python\nawait directus.collection(\"directus_users\").fields(\"first_name\", \"last_name\").read()\n```\n\n### Getting the Count Metadata\n\nYou can get the count of the data (total count and filtered count) calling `include_count`\n\n```python\nawait directus.collection(\"directus_users\").include_count().read()\n```\n\n## CRUD\n\n### Retrieving items\n\nAfter you call `read()` you get a `DirectusResponse` object which contains the data.\n\n- `item` for single item\n- `items` for multiple items\n\nGetting the data as a dictionary or a list of dictionaries\n\n```python\nresponse = await directus.collection(\"directus_users\").read()\nprint(response.item[\"first_name\"])\nprint(response.items)\n```\n\nIf you provide the `collection` method a `Pydantic` model you will get the data as a `Pydantic` object or a list of `Pydantic` objects\n\n```python\nresponse = await directus.collection(User).read()\nprint(response.item.first_name)\nprint(response.items)\n```\n\n### Converting to Models (pydantic) or to Dictionary\n\nApart from the auto parsing, you can manually convert the data to a `Pydantic` model instance or to a dictionary using:\n\n- `item_as(User)` or `items_as(User)`\n- `item_as_dict()` or `items_as_dict()`\n\n```python\nresponse = await directus.collection(\"directus_users\").read()\nprint(response.item_as(User))\n\nresponse = await directus.collection(User).read()\nprint(response.item_as_dict())\n```\n\n### Creating Items\n\nThe library does not support `Pydantic` models for creation, you have to pass a dictionary\n\n- create(items: dict|List[dict])\n\n```python\nawait directus.collection(\"directus_users\").create({\n \"first_name\": \"John\", \"last_name\": \"Doe\"\n})\n\n# OR\n\nawait directus.collection(\"directus_users\").create(\n [\n {\"first_name\": \"John\", \"last_name\": \"Doe\"},\n {\"first_name\": \"Jane\", \"last_name\": \"Doe\"}\n ]\n)\n```\n\n### Updating Items\n\nThe library do not support `Pydantic` models for updating, you have to pass a dictionary\n\n- `update(ids: str|int, items: dict)`\n- `update(ids: List[str|int], items: List[dict])`\n\n```python\nawait directus.collection(\"directus_users\").update(1, {\n \"first_name\": \"Red\",\n \"last_name\": \"John\"\n})\n\n# OR\n\nawait directus.collection(\"directus_users\").update(\n [1, 2],\n [\n {\"first_name\": \"Jean-Luc\"},\n {\"first_name\": \"Jane\", \"last_name\": \"Doe\"}\n ]\n)\n```\n\n### Deleting Items\n\n- `delete(ids: str|int|List[str|int])`\n\n```python\nawait directus.collection(\"directus_users\").delete(1)\n\n# OR\n\nawait directus.collection(\"directus_users\").delete([1, 2])\n```\n\n> Supporting `Pydantic` models for `create`/`update`/`delete` item operations is shortly coming.\n\n## Examples\n\n> Examples are not included with the `pypi` package, so you will have to download them separately and execute in a virtual environment.\n\nRun individual examples as such:\n\n```shell\npython -m examples.<example_file_name>\n```\n\n## Tests\n\nRun tests as such:\n\n```shell\n# All unit tests\n\npython -m unittest discover -s tests/unit\n\n# All integration tests\n\npython -m unittest discover -s tests/integration\n```\n",
"bugtrack_url": null,
"license": "MIT license",
"summary": "Python wrapper for asynchronous interaction with Directus",
"version": "0.0.30",
"project_urls": {
"Homepage": "https://github.com/panos-stavrianos/py-directus"
},
"split_keywords": [
"python",
" directus",
" async",
" asyncio",
" api",
" wrapper"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9ade96b22dfcda59629efce3e72dfa48f03b95e365b278197f9090180f372f18",
"md5": "cc7804f37cb874ffe1180a939b9534c4",
"sha256": "8809aa0374af359ee686b14568a95543dfe4c4d3b3822620dba3650686d15605"
},
"downloads": -1,
"filename": "py_directus-0.0.30-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cc7804f37cb874ffe1180a939b9534c4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 30817,
"upload_time": "2024-05-01T16:40:20",
"upload_time_iso_8601": "2024-05-01T16:40:20.590589Z",
"url": "https://files.pythonhosted.org/packages/9a/de/96b22dfcda59629efce3e72dfa48f03b95e365b278197f9090180f372f18/py_directus-0.0.30-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "bcb427fe586f4b9e90eb91e3e7dfdf7d884cfedbfe28c9c504da496b74936026",
"md5": "56d0c77f0bb031f761066d9f74654fe4",
"sha256": "98c431b0c8fa2cd5f76dd85852ecedd231e0883809802e03b566fc8f061d11b5"
},
"downloads": -1,
"filename": "py_directus-0.0.30.tar.gz",
"has_sig": false,
"md5_digest": "56d0c77f0bb031f761066d9f74654fe4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 28442,
"upload_time": "2024-05-01T16:40:27",
"upload_time_iso_8601": "2024-05-01T16:40:27.402337Z",
"url": "https://files.pythonhosted.org/packages/bc/b4/27fe586f4b9e90eb91e3e7dfdf7d884cfedbfe28c9c504da496b74936026/py_directus-0.0.30.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-01 16:40:27",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "panos-stavrianos",
"github_project": "py-directus",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "aiofiles",
"specs": [
[
">=",
"23.2.1"
]
]
},
{
"name": "httpx",
"specs": [
[
"<",
"1.0.0"
],
[
">=",
"0.25.0"
]
]
},
{
"name": "json-fix",
"specs": [
[
"<",
"1.0.0"
],
[
">=",
"0.5.2"
]
]
},
{
"name": "python-dotenv",
"specs": [
[
">=",
"1.0.0"
],
[
"<",
"2.0.0"
]
]
},
{
"name": "python-magic",
"specs": [
[
">=",
"0.4.27"
],
[
"<",
"1.0.0"
]
]
},
{
"name": "pydantic",
"specs": [
[
">",
"2.0"
]
]
},
{
"name": "rich",
"specs": [
[
"<",
"14.0.0"
],
[
">=",
"13.6.0"
]
]
},
{
"name": "websockets",
"specs": [
[
">=",
"11.0.3"
]
]
},
{
"name": "platformdirs",
"specs": []
}
],
"lcname": "py-directus"
}