Skip to content

Repository files navigation

Directus Python SDK

Testscoverage

Requirements

  • Python 3.6+

Installation

pip install -e .

Usage

Initializa directus client

fromdirectus.clientsimportDirectusClient_V9# Create a directus client connection with user static tokenclient=DirectusClient_V9(url="http://localhost:8055", token="admin-token")
# Or create a directus client connection with email and passwordclient=DirectusClient_V9(url="http://localhost:8055", email="user@example.com", password="password")

Logging in and out of the client

client=DirectusClient_V9(url="http://localhost:8055", email="user@example.com", password="password")
# Log out and use static token insteadclient.logout()
client.static_token="admin-token"client.login(email="user2@example.com", password="password2")

Generic API requests

The directus client automatically handles the injection of access token so any directus API requests can be simplified like so:

# GET requestcollection=client.get(f"/collections/{collection_name}")
item=client.get(f"/items/{collection_name}/1")
# POST requestitems= [
{
"name": "item1"
},
{
"name": "item2"
}
]
client.post(f"/items/{collection_name}", json=items)
# PATCH requestclient.patch(f"/items/{collection_name}/1", json={
"name": "updated item1"
})
# DELETE requestclient.delete(f"/items/{collection_name}/1")

Bulk Insert

Params: collection_name: str, items: list

client.bulk_insert(collection_name="test-collection",
items=[{"Title": "test"}, {"Title": "test2"}])

Duplicate Collection

Params: collection_name: str, duplicate_collection_name: str

client.duplicate_collection(collection_name="test-collection", duplicate_collection_name="test_duplication_collection")

Checks if collection exists

Params collection_name: str, items: list

ifclient.collection_exists("test"):
print("test collection exists!")

Delete all items from a collection

Params: collection_name: str

client.delete_all_items("test")

Get collection primary key

Params: collection_name: str

pk_field=client.get_pk_field("brands")

Get all user-created collection names

Params:

print("Listing all user-created collections on directus...")
fornameinclient.get_all_user_created_collection_names():
print(name)

Get all field names of a given collection

Params: collection_name: str

print("Listing all fields in test collection...")
forfieldinclient.get_all_fields("test"):
print(json.dumps(field, indent=4))

Get all foreign key fields in directus collection

Params: collection_name: str

print("Listing all foreign key fields in test collection...")
forfieldinclient.get_all_fk_fields("brands"):
print(json.dumps(field, indent=4))

Get all relations in directus collection

Params: collection_name: str

importjsonprint("Listing all relations in test collection...")
forfieldinclient.get_relations("test"):
print(json.dumps(field, indent=4))

Create relations

Params: relation: dict

client.post_relation({
"collection": "books",
"field": "author",
"related_collection": "authors"
})

TODOs:

  • debug test cases
  • add proper pytest

About

Python SDK for directus client with convenience functions

Topics

Resources

Stars

16 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages