From 7ca4f14605825613878563674b8782746e3c2484 Mon Sep 17 00:00:00 2001 From: Ankush Khanna Date: Wed, 12 Mar 2025 11:30:02 +0100 Subject: [PATCH] Adding fern python documentation --- .pre-commit-config.yaml | 4 +- .../python/core/add_metadata.py | 24 +++++++ .../python/core/create_index.py | 15 +++++ .../python/core/delete_doc.py | 16 +++++ .../python/core/delete_index.py | 15 +++++ .../python/core/direct_search_index.py | 45 +++++++++++++ .../python/core/get_doc_asset.py | 45 +++++++++++++ .../python/core/get_document.py | 18 +++++ .../python/core/list_index.py | 20 ++++++ .../python/core/put_doc_into_index.py | 23 +++++++ .../python/core/refresh_index.py | 14 ++++ .../python/core/search_chunks.py | 19 ++++++ .../python/core/search_docs.py | 19 ++++++ .../python/core/update_group_auth.py | 28 ++++++++ .../python/core/upload_files.py | 37 +++++++++++ .../python/datasources/create_datasource.py | 44 +++++++++++++ .../python/datasources/delete_datasource.py | 15 +++++ .../python/datasources/document_status.py | 26 ++++++++ .../python/datasources/get_datasource.py | 15 +++++ .../datasources/list_all_datasources.py | 22 +++++++ .../python/datasources/sync_datasource.py | 16 +++++ .../python/rbac/group/add_member.py | 66 +++++++++++++++++++ .../python/rbac/group/add_roles.py | 49 ++++++++++++++ .../python/rbac/group/create.py | 20 ++++++ .../python/rbac/group/delete.py | 14 ++++ .../python/rbac/group/get_details.py | 15 +++++ .../python/rbac/group/get_member_page.py | 44 +++++++++++++ .../python/rbac/group/get_page.py | 39 +++++++++++ .../python/rbac/group/get_roles_page.py | 46 +++++++++++++ .../python/rbac/group/remove_members.py | 52 +++++++++++++++ .../python/rbac/group/remove_role.py | 45 +++++++++++++ .../python/rbac/role/create.py | 32 +++++++++ .../python/rbac/role/delete.py | 15 +++++ .../python/rbac/role/get_details.py | 14 ++++ .../python/rbac/role/get_groups_page.py | 43 ++++++++++++ .../python/rbac/role/get_pages.py | 37 +++++++++++ .../python/rbac/role/update.py | 19 ++++++ .../python/rbac/user/create.py | 18 +++++ .../python/rbac/user/delete.py | 16 +++++ .../python/rbac/user/get_details.py | 16 +++++ .../python/rbac/user/get_group_page.py | 43 ++++++++++++ .../python/rbac/user/get_page.py | 42 ++++++++++++ 42 files changed, 1163 insertions(+), 2 deletions(-) create mode 100644 examples/fern_documentation/python/core/add_metadata.py create mode 100644 examples/fern_documentation/python/core/create_index.py create mode 100644 examples/fern_documentation/python/core/delete_doc.py create mode 100644 examples/fern_documentation/python/core/delete_index.py create mode 100644 examples/fern_documentation/python/core/direct_search_index.py create mode 100644 examples/fern_documentation/python/core/get_doc_asset.py create mode 100644 examples/fern_documentation/python/core/get_document.py create mode 100644 examples/fern_documentation/python/core/list_index.py create mode 100644 examples/fern_documentation/python/core/put_doc_into_index.py create mode 100644 examples/fern_documentation/python/core/refresh_index.py create mode 100644 examples/fern_documentation/python/core/search_chunks.py create mode 100644 examples/fern_documentation/python/core/search_docs.py create mode 100644 examples/fern_documentation/python/core/update_group_auth.py create mode 100644 examples/fern_documentation/python/core/upload_files.py create mode 100644 examples/fern_documentation/python/datasources/create_datasource.py create mode 100644 examples/fern_documentation/python/datasources/delete_datasource.py create mode 100644 examples/fern_documentation/python/datasources/document_status.py create mode 100644 examples/fern_documentation/python/datasources/get_datasource.py create mode 100644 examples/fern_documentation/python/datasources/list_all_datasources.py create mode 100644 examples/fern_documentation/python/datasources/sync_datasource.py create mode 100644 examples/fern_documentation/python/rbac/group/add_member.py create mode 100644 examples/fern_documentation/python/rbac/group/add_roles.py create mode 100644 examples/fern_documentation/python/rbac/group/create.py create mode 100644 examples/fern_documentation/python/rbac/group/delete.py create mode 100644 examples/fern_documentation/python/rbac/group/get_details.py create mode 100644 examples/fern_documentation/python/rbac/group/get_member_page.py create mode 100644 examples/fern_documentation/python/rbac/group/get_page.py create mode 100644 examples/fern_documentation/python/rbac/group/get_roles_page.py create mode 100644 examples/fern_documentation/python/rbac/group/remove_members.py create mode 100644 examples/fern_documentation/python/rbac/group/remove_role.py create mode 100644 examples/fern_documentation/python/rbac/role/create.py create mode 100644 examples/fern_documentation/python/rbac/role/delete.py create mode 100644 examples/fern_documentation/python/rbac/role/get_details.py create mode 100644 examples/fern_documentation/python/rbac/role/get_groups_page.py create mode 100644 examples/fern_documentation/python/rbac/role/get_pages.py create mode 100644 examples/fern_documentation/python/rbac/role/update.py create mode 100644 examples/fern_documentation/python/rbac/user/create.py create mode 100644 examples/fern_documentation/python/rbac/user/delete.py create mode 100644 examples/fern_documentation/python/rbac/user/get_details.py create mode 100644 examples/fern_documentation/python/rbac/user/get_group_page.py create mode 100644 examples/fern_documentation/python/rbac/user/get_page.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c9775d9..851131c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,7 +12,7 @@ repos: hooks: - id: pyright name: pyright (root) - exclude: '^examples/' + exclude: ^(examples/) - id: pyright name: pyright (examples) - files: '^examples/' + files: ^(examples/compass_sdk_examples) diff --git a/examples/fern_documentation/python/core/add_metadata.py b/examples/fern_documentation/python/core/add_metadata.py new file mode 100644 index 0000000..be369d6 --- /dev/null +++ b/examples/fern_documentation/python/core/add_metadata.py @@ -0,0 +1,24 @@ +from cohere.compass.clients import CompassClient +from cohere.compass.models import DocumentAttributes + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... +DOCUMENT_ID = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +doc_attr = DocumentAttributes() +doc_attr.key = "value" + +error = compass_client.add_attributes( + index_name=INDEX_NAME, + document_id=DOCUMENT_ID, + attributes=doc_attr, +) + +if error: + raise Exception(f"Failed to add attributes: {error}") diff --git a/examples/fern_documentation/python/core/create_index.py b/examples/fern_documentation/python/core/create_index.py new file mode 100644 index 0000000..3bdfa02 --- /dev/null +++ b/examples/fern_documentation/python/core/create_index.py @@ -0,0 +1,15 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.create_index(index_name=INDEX_NAME) + +if r.error: + raise Exception(f"Failed to create index: {r.error}") diff --git a/examples/fern_documentation/python/core/delete_doc.py b/examples/fern_documentation/python/core/delete_doc.py new file mode 100644 index 0000000..1a5238a --- /dev/null +++ b/examples/fern_documentation/python/core/delete_doc.py @@ -0,0 +1,16 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... +DOCUMENT_ID = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.delete_document(index_name=INDEX_NAME, document_id=DOCUMENT_ID) + +if r.error: + raise Exception(f"Failed to delete doc: {r.error}") diff --git a/examples/fern_documentation/python/core/delete_index.py b/examples/fern_documentation/python/core/delete_index.py new file mode 100644 index 0000000..4bd6a52 --- /dev/null +++ b/examples/fern_documentation/python/core/delete_index.py @@ -0,0 +1,15 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.delete_index(index_name=INDEX_NAME) + +if r.error: + raise Exception(f"Failed to delete index: {r.error}") diff --git a/examples/fern_documentation/python/core/direct_search_index.py b/examples/fern_documentation/python/core/direct_search_index.py new file mode 100644 index 0000000..824b948 --- /dev/null +++ b/examples/fern_documentation/python/core/direct_search_index.py @@ -0,0 +1,45 @@ +from typing import Optional + +from cohere.compass.clients import CompassClient +from cohere.compass.models import RetrievedChunkExtended + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... +DOCUMENT_ID = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +query = {"query": {"match_all": {}}} + + +def call_direct_search_scroll( + hits: list[RetrievedChunkExtended], + scroll_id: Optional[str] = None, +) -> list[RetrievedChunkExtended]: + if hits is None: + hits = [] + + if scroll_id is None: + return hits + + results_scroll = compass_client.direct_search_scroll( + scroll_id=scroll_id, + scroll="1m", + ) + + hits.extend(results_scroll.hits) + return call_direct_search_scroll(hits=hits, scroll_id=results.scroll_id) + + +results = compass_client.direct_search( + index_name=INDEX_NAME, + query=query, + scroll="1m", + size=100, +) + +hits = call_direct_search_scroll(hits=results.hits, scroll_id=results.scroll_id) diff --git a/examples/fern_documentation/python/core/get_doc_asset.py b/examples/fern_documentation/python/core/get_doc_asset.py new file mode 100644 index 0000000..c0380a1 --- /dev/null +++ b/examples/fern_documentation/python/core/get_doc_asset.py @@ -0,0 +1,45 @@ +import json + +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... +DOCUMENT_ID = ... +SEARCH_QUERY = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +results = compass_client.search_chunks(index_name=INDEX_NAME, query=SEARCH_QUERY) +if results.error: + raise Exception(f"Failed to search chunks: {results.error}") + +hits = results.hits + +if not hits: + raise Exception("No hits found") + +hit = hits[0] + +asset_id: str = hit.assets_info[0].asset_id # type: ignore +document_id = hit.document_id +# URL to fetch asset +presigned_url = hit.assets_info[0].presigned_url + +### Get document asset again, after pre-signed url has expired +asset, content_type = compass_client.get_document_asset( + index_name=document_id, + document_id=DOCUMENT_ID, + asset_id=asset_id, +) + +# Save the asset to a file. +if content_type in ["image/jpeg", "image/png"]: + with open(f"{asset_id}", "wb") as f: + f.write(asset) +# Print the asset as JSON +elif "text/json" in content_type: + print(json.dumps(asset, indent=2)) diff --git a/examples/fern_documentation/python/core/get_document.py b/examples/fern_documentation/python/core/get_document.py new file mode 100644 index 0000000..8f771b5 --- /dev/null +++ b/examples/fern_documentation/python/core/get_document.py @@ -0,0 +1,18 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... +DOCUMENT_ID = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.get_document(index_name=INDEX_NAME, document_id=DOCUMENT_ID) + +if r.error: + raise Exception(f"Failed to get doc: {r.error}") + +print(r.result.hits) diff --git a/examples/fern_documentation/python/core/list_index.py b/examples/fern_documentation/python/core/list_index.py new file mode 100644 index 0000000..29dfd88 --- /dev/null +++ b/examples/fern_documentation/python/core/list_index.py @@ -0,0 +1,20 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.list_indexes() +if r.error: + raise Exception(f"Failed to list indexes: {r.error}") + +indexes = r.result["indexes"] +for index in indexes: + print(f"Index name: {index['name']}") + print(f"Chunk count: {index['count']}") + print(f"Document count: {index['parent_doc_count']}") diff --git a/examples/fern_documentation/python/core/put_doc_into_index.py b/examples/fern_documentation/python/core/put_doc_into_index.py new file mode 100644 index 0000000..7199906 --- /dev/null +++ b/examples/fern_documentation/python/core/put_doc_into_index.py @@ -0,0 +1,23 @@ +from cohere.compass.clients import CompassClient, CompassParserClient + +COMPASS_API_URL = ... +PARSER_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... +FOLDER_TO_PROCESS = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +compass_parser_client = CompassParserClient(parser_url=PARSER_URL) + +docs_to_index = compass_parser_client.process_folder( + folder_path=FOLDER_TO_PROCESS, recursive=True +) + +compass_client.create_index(index_name=INDEX_NAME) +r = compass_client.insert_docs(index_name=INDEX_NAME, docs=docs_to_index, num_jobs=1) + +print(r) diff --git a/examples/fern_documentation/python/core/refresh_index.py b/examples/fern_documentation/python/core/refresh_index.py new file mode 100644 index 0000000..5e591aa --- /dev/null +++ b/examples/fern_documentation/python/core/refresh_index.py @@ -0,0 +1,14 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.refresh_index(index_name=INDEX_NAME) +if r.error: + raise Exception(f"Failed to refresh indexes: {r.error}") diff --git a/examples/fern_documentation/python/core/search_chunks.py b/examples/fern_documentation/python/core/search_chunks.py new file mode 100644 index 0000000..1437e70 --- /dev/null +++ b/examples/fern_documentation/python/core/search_chunks.py @@ -0,0 +1,19 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... +DOCUMENT_ID = ... +SEARCH_QUERY = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.search_chunks(index_name=INDEX_NAME, query=SEARCH_QUERY) + +if r.error: + raise Exception(f"Failed to search: {r.error}") + +print(r.hits) diff --git a/examples/fern_documentation/python/core/search_docs.py b/examples/fern_documentation/python/core/search_docs.py new file mode 100644 index 0000000..0d34fd0 --- /dev/null +++ b/examples/fern_documentation/python/core/search_docs.py @@ -0,0 +1,19 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... +DOCUMENT_ID = ... +SEARCH_QUERY = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.search_documents(index_name=INDEX_NAME, query=SEARCH_QUERY) + +if r.error: + raise Exception(f"Failed to search doc: {r.error}") + +print(r.hits) diff --git a/examples/fern_documentation/python/core/update_group_auth.py b/examples/fern_documentation/python/core/update_group_auth.py new file mode 100644 index 0000000..ed36676 --- /dev/null +++ b/examples/fern_documentation/python/core/update_group_auth.py @@ -0,0 +1,28 @@ +from cohere.compass import GroupAuthorizationInput +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... +DOCUMENT_ID = ... +AUTHORIZED_GROUP_NAME = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.update_group_authorization( + index_name=INDEX_NAME, + group_auth_input=GroupAuthorizationInput( + document_ids=[DOCUMENT_ID], + authorized_groups=[AUTHORIZED_GROUP_NAME], + action=GroupAuthorizationInput.Actions.ADD, + ), +) + +for doc in r.results: + if doc.error: + print(f"Error processing doc: {doc.document_id}, error: {doc.error}") + continue + print(f"Successfully processed doc: {doc.document_id}") diff --git a/examples/fern_documentation/python/core/upload_files.py b/examples/fern_documentation/python/core/upload_files.py new file mode 100644 index 0000000..f7117c6 --- /dev/null +++ b/examples/fern_documentation/python/core/upload_files.py @@ -0,0 +1,37 @@ +import uuid + +from cohere.compass.clients import CompassClient +from cohere.compass.models import DocumentAttributes + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... +DOCUMENT_ID = ... +FILE_PATH = ... +FILE_NAME = ... +CONTENT_TYPE = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +doc_attr = DocumentAttributes() +doc_attr.key = "value" + +file_path = FILE_PATH +if not file_path: + raise Exception("FILE_PATH is required") + +with open(file_path, "rb") as f: + file_bytes = f.read() + +result = compass_client.upload_document( + index_name=INDEX_NAME, + filename=FILE_NAME, + filebytes=file_bytes, + content_type=CONTENT_TYPE, + document_id=uuid.uuid4(), +) + +print(result) diff --git a/examples/fern_documentation/python/datasources/create_datasource.py b/examples/fern_documentation/python/datasources/create_datasource.py new file mode 100644 index 0000000..10df4b0 --- /dev/null +++ b/examples/fern_documentation/python/datasources/create_datasource.py @@ -0,0 +1,44 @@ +from cohere.compass.clients import CompassClient +from cohere.compass.models import AzureBlobStorageConfig, CreateDataSource, DataSource + +COMPASS_API_URL = ... +BEARER_TOKEN = ... +INDEX_NAME = ... + +DATASOURCE_ENABLED: bool = ... +DATASOURCE_NAME = ... +DATASOURCE_DESCRIPTION = ... + +AZURE_BLOB_STORAGE_CONNECTION_STRING = ... +AZURE_BLOB_STORAGE_CONTAINER_NAME = ... +AZURE_BLOB_STORAGE_NAME_STARTS_WITH = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +compass_client.create_index(index_name=INDEX_NAME) + +datasource = compass_client.create_datasource( + datasource=CreateDataSource( + datasource=DataSource( + name=DATASOURCE_NAME, + description=DATASOURCE_DESCRIPTION, + config=AzureBlobStorageConfig( + type="msft_azure_blob_storage", + connection_string=AZURE_BLOB_STORAGE_CONNECTION_STRING, + container_name=AZURE_BLOB_STORAGE_CONTAINER_NAME, + name_starts_with=AZURE_BLOB_STORAGE_NAME_STARTS_WITH, + ), + destinations=[INDEX_NAME], + enabled=DATASOURCE_ENABLED, + ) + ) +) + +if isinstance(datasource, str): + raise Exception(f"Failed to create datasources: {datasource}") + +if isinstance(datasource, DataSource): + print(datasource) diff --git a/examples/fern_documentation/python/datasources/delete_datasource.py b/examples/fern_documentation/python/datasources/delete_datasource.py new file mode 100644 index 0000000..1c19519 --- /dev/null +++ b/examples/fern_documentation/python/datasources/delete_datasource.py @@ -0,0 +1,15 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... + +DATASOURCE_ID = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.delete_datasource(datasource_id=DATASOURCE_ID) + +print(r) diff --git a/examples/fern_documentation/python/datasources/document_status.py b/examples/fern_documentation/python/datasources/document_status.py new file mode 100644 index 0000000..6e1515c --- /dev/null +++ b/examples/fern_documentation/python/datasources/document_status.py @@ -0,0 +1,26 @@ +from cohere.compass.clients import CompassClient +from cohere.compass.models import PaginatedList + +COMPASS_API_URL = ... +BEARER_TOKEN = ... + +DATASOURCE_ID = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) +r = compass_client.list_datasources_objects_states( + datasource_id=DATASOURCE_ID, +) + +if isinstance(r, str): + raise Exception(f"Failed to list datasources: {r}") + +if isinstance(r, PaginatedList): + documents = r.value + for document in documents: + print(f"Document id: {document.document_id}") + print(f"Document state: {document.state}") + print(f"Document source id: {document.source_id}") + print(f"Document destination: {document.destinations}") diff --git a/examples/fern_documentation/python/datasources/get_datasource.py b/examples/fern_documentation/python/datasources/get_datasource.py new file mode 100644 index 0000000..d39f1e3 --- /dev/null +++ b/examples/fern_documentation/python/datasources/get_datasource.py @@ -0,0 +1,15 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... + +DATASOURCE_ID = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) + +r = compass_client.get_datasource(datasource_id=DATASOURCE_ID) + +print(r) diff --git a/examples/fern_documentation/python/datasources/list_all_datasources.py b/examples/fern_documentation/python/datasources/list_all_datasources.py new file mode 100644 index 0000000..347ec6a --- /dev/null +++ b/examples/fern_documentation/python/datasources/list_all_datasources.py @@ -0,0 +1,22 @@ +from cohere.compass.clients import CompassClient +from cohere.compass.models import PaginatedList + +COMPASS_API_URL = ... +BEARER_TOKEN = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) +r = compass_client.list_datasources() + +if isinstance(r, str): + raise Exception(f"Failed to list datasources: {r}") + +if isinstance(r, PaginatedList): + datasources = r.value + for datasource in datasources: + print(f"Datasource id: {datasource.id}") + print(f"Datasource name: {datasource.name}") + print(f"Datasource description: {datasource.description}") + print(f"Datasource enabled: {datasource.enabled}") diff --git a/examples/fern_documentation/python/datasources/sync_datasource.py b/examples/fern_documentation/python/datasources/sync_datasource.py new file mode 100644 index 0000000..7937397 --- /dev/null +++ b/examples/fern_documentation/python/datasources/sync_datasource.py @@ -0,0 +1,16 @@ +from cohere.compass.clients import CompassClient + +COMPASS_API_URL = ... +BEARER_TOKEN = ... + +DATASOURCE_ID = ... + +compass_client = CompassClient( + index_url=COMPASS_API_URL, + bearer_token=BEARER_TOKEN, +) +r = compass_client.sync_datasource( + datasource_id=DATASOURCE_ID, +) + +print(r) diff --git a/examples/fern_documentation/python/rbac/group/add_member.py b/examples/fern_documentation/python/rbac/group/add_member.py new file mode 100644 index 0000000..ac8b180 --- /dev/null +++ b/examples/fern_documentation/python/rbac/group/add_member.py @@ -0,0 +1,66 @@ +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import Group, User +from requests.exceptions import HTTPError + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +try: + groups = compass_root_client.create_groups( + groups=[ + Group(group_name="unique_group_name_1"), + Group(group_name="unique_group_name_2"), + ] + ) + + for group in groups: + print(f"Created group: {group.group_name}") +except HTTPError as e: + if e.response.status_code == 409: + print("Group already exists.") + else: + raise + +try: + users = compass_root_client.create_users( + users=[ + User(user_name="unique_user_name_1"), + User(user_name="unique_user_name_2"), + ] + ) +except HTTPError as e: + if e.response.status_code == 409: + print("User already exists.") + else: + raise + +try: + # Adding unique_user_name_1 to unique_group_name_1 + group_membership = compass_root_client.add_members_to_group( + group_name="unique_group_name_1", + user_names=["unique_user_name_1"], + ) + print("Group membership added: ", group_membership) +except HTTPError as e: + if e.response.status_code == 409: + print("Membership already exists.") + else: + raise + +try: + # Adding unique_user_name_2 to unique_group_name_2 + group_membership = compass_root_client.add_members_to_group( + group_name="unique_group_name_2", + user_names=["unique_user_name_2"], + ) + print("Group membership added: ", group_membership) +except HTTPError as e: + if e.response.status_code == 409: + print("Membership already exists.") + else: + raise diff --git a/examples/fern_documentation/python/rbac/group/add_roles.py b/examples/fern_documentation/python/rbac/group/add_roles.py new file mode 100644 index 0000000..cf30371 --- /dev/null +++ b/examples/fern_documentation/python/rbac/group/add_roles.py @@ -0,0 +1,49 @@ +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import Group, Permission, Policy, Role +from requests.exceptions import HTTPError + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +try: + groups = compass_root_client.create_groups( + groups=[ + Group(group_name="unique_group_name_1"), + Group(group_name="unique_group_name_2"), + ] + ) +except HTTPError as e: + if e.response.status_code == 409: + print("Group already exists.") + else: + raise + +try: + roles = compass_root_client.create_roles( + roles=[ + Role( + role_name="developer_read_specific_index", + policies=[Policy(indexes=["user-*"], permission=Permission.READ)], + ), + ] + ) +except HTTPError as e: + if e.response.status_code == 409: + print("Role already exists.") + else: + raise + +group_roles = compass_root_client.add_roles_to_group( + group_name="unique_group_name_1", + role_names=[ + "developer_read_specific_index", + ], +) + +for group_role in group_roles: + print(f"Added roles: {group_role.role_name} to group: {group_role.group_name}") diff --git a/examples/fern_documentation/python/rbac/group/create.py b/examples/fern_documentation/python/rbac/group/create.py new file mode 100644 index 0000000..4c05159 --- /dev/null +++ b/examples/fern_documentation/python/rbac/group/create.py @@ -0,0 +1,20 @@ +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import Group + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +groups = compass_root_client.create_groups( + groups=[ + Group(group_name="unique_group_name_1"), + Group(group_name="unique_group_name_2"), + ] +) + +for group in groups: + print(f"Created group: {group.group_name}") diff --git a/examples/fern_documentation/python/rbac/group/delete.py b/examples/fern_documentation/python/rbac/group/delete.py new file mode 100644 index 0000000..2b02739 --- /dev/null +++ b/examples/fern_documentation/python/rbac/group/delete.py @@ -0,0 +1,14 @@ +from cohere.compass.clients.access_control import CompassRootClient + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... +GROUP_NAME = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) +groups = compass_root_client.delete_groups(group_names=[GROUP_NAME]) + +for group in groups: + print(f"Deleted group: {group.group_name}") diff --git a/examples/fern_documentation/python/rbac/group/get_details.py b/examples/fern_documentation/python/rbac/group/get_details.py new file mode 100644 index 0000000..60e6f13 --- /dev/null +++ b/examples/fern_documentation/python/rbac/group/get_details.py @@ -0,0 +1,15 @@ +from cohere.compass.clients.access_control import CompassRootClient + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... +GROUP_NAME = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +group = compass_root_client.get_detailed_group(group_name=GROUP_NAME) + +print(f"Group roles: {group.roles}") +print(f"Group users: {group.users}") diff --git a/examples/fern_documentation/python/rbac/group/get_member_page.py b/examples/fern_documentation/python/rbac/group/get_member_page.py new file mode 100644 index 0000000..2eb67db --- /dev/null +++ b/examples/fern_documentation/python/rbac/group/get_member_page.py @@ -0,0 +1,44 @@ +from typing import Optional + +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import PageDirection, PageInfo, User + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... +GROUP_NAME = ... +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + + +def get_all_members_in_group( + group_name: str, + filter: Optional[str] = None, + page_info: Optional[PageInfo] = None, + direction: Optional[PageDirection] = None, + users: Optional[list[User]] = None, +) -> list[User]: + user_page = compass_root_client.get_group_members_page( + group_name=group_name, + filter=filter, + page_info=page_info, + direction=direction, + ) + + users = users or [] + users.extend(user_page.users) + + if not user_page.page_info.has_next_page(): + return users + return get_all_members_in_group( + group_name=group_name, + filter=filter, + page_info=user_page.page_info, + direction=PageDirection.NEXT, + users=users, + ) + + +members = get_all_members_in_group(group_name=GROUP_NAME) +print(f"Members in group: {members}") diff --git a/examples/fern_documentation/python/rbac/group/get_page.py b/examples/fern_documentation/python/rbac/group/get_page.py new file mode 100644 index 0000000..4d9b80b --- /dev/null +++ b/examples/fern_documentation/python/rbac/group/get_page.py @@ -0,0 +1,39 @@ +from typing import Optional + +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import Group, PageDirection, PageInfo + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + + +def get_all_groups( + filter: Optional[str] = None, + page_info: Optional[PageInfo] = None, + direction: Optional[PageDirection] = None, + groups: Optional[list[Group]] = None, +) -> list[Group]: + group_pages = compass_root_client.get_groups_page( + filter=filter, page_info=page_info, direction=direction + ) + + groups = groups or [] + groups.extend(group_pages.groups) + + if not group_pages.page_info.has_next_page(): + return groups + return get_all_groups( + filter=filter, + page_info=group_pages.page_info, + direction=PageDirection.NEXT, + groups=groups, + ) + + +groups = get_all_groups() +print(f"Groups: {groups}") diff --git a/examples/fern_documentation/python/rbac/group/get_roles_page.py b/examples/fern_documentation/python/rbac/group/get_roles_page.py new file mode 100644 index 0000000..6bc5418 --- /dev/null +++ b/examples/fern_documentation/python/rbac/group/get_roles_page.py @@ -0,0 +1,46 @@ +from typing import Optional + +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import PageDirection, PageInfo, Role, User + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... +GROUP_NAME = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + + +def get_all_roles_in_group( + group_name: str, + filter: Optional[str] = None, + page_info: Optional[PageInfo] = None, + direction: Optional[PageDirection] = None, + roles: Optional[list[Role]] = None, +) -> list[User]: + roles_page = compass_root_client.get_group_roles_page( + group_name=group_name, + filter=filter, + page_info=page_info, + direction=direction, + ) + + roles = roles or [] + roles.extend(roles_page.roles) + + if not roles_page.page_info.has_next_page(): + return roles + + return get_all_roles_in_group( + group_name=group_name, + filter=filter, + page_info=roles_page.page_info, + direction=PageDirection.NEXT, + roles=roles, + ) + + +roles = get_all_roles_in_group(group_name=GROUP_NAME) +print(f"Roles in group: {roles}") diff --git a/examples/fern_documentation/python/rbac/group/remove_members.py b/examples/fern_documentation/python/rbac/group/remove_members.py new file mode 100644 index 0000000..a544346 --- /dev/null +++ b/examples/fern_documentation/python/rbac/group/remove_members.py @@ -0,0 +1,52 @@ +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import Group, User +from requests.exceptions import HTTPError + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) +try: + groups = compass_root_client.create_groups( + groups=[ + Group(group_name="unique_group_name_1"), + ] + ) +except HTTPError as e: + if e.response.status_code == 409: + print("Group already exists.") + else: + raise + +try: + users = compass_root_client.create_users( + users=[User(user_name="unique_user_name_1")] + ) +except HTTPError as e: + if e.response.status_code == 409: + print("User already exists.") + else: + raise + +try: + # Adding unique_user_name_1 to unique_group_name_1 + group_membership = compass_root_client.add_members_to_group( + group_name="unique_group_name_1", + user_names=["unique_user_name_1"], + ) + print("Group membership added: ", group_membership) +except HTTPError as e: + if e.response.status_code == 409: + print("Membership already exists.") + else: + raise + +# Removing unique_user_name_1 from unique_group_name_1 +group_membership = compass_root_client.remove_members_from_group( + group_name="unique_group_name_1", + user_names=["unique_user_name_1"], +) +print("Group membership removed: ", group_membership) diff --git a/examples/fern_documentation/python/rbac/group/remove_role.py b/examples/fern_documentation/python/rbac/group/remove_role.py new file mode 100644 index 0000000..8ef57e3 --- /dev/null +++ b/examples/fern_documentation/python/rbac/group/remove_role.py @@ -0,0 +1,45 @@ +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import Group, Permission, Policy, Role + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +groups = compass_root_client.create_groups( + groups=[ + Group(group_name="unique_group_name_1"), + ] +) + +roles = compass_root_client.create_roles( + roles=[ + Role( + role_name="developer_read_specific_index", + policies=[Policy(indexes=["user-*"], permission=Permission.READ)], + ), + ] +) + +group_roles = compass_root_client.add_roles_to_group( + group_name="unique_group_name_1", + role_names=[ + "developer_read_specific_index", + ], +) + +for group_role in group_roles: + print(f"Added roles: {group_role.role_name} to group: {group_role.group_name}") + +group_roles = compass_root_client.remove_roles_from_group( + group_name="unique_group_name_1", + role_names=[ + "developer_read_specific_index", + ], +) + +for group_role in group_roles: + print(f"Removed roles: {group_role.role_name} to group: {group_role.group_name}") diff --git a/examples/fern_documentation/python/rbac/role/create.py b/examples/fern_documentation/python/rbac/role/create.py new file mode 100644 index 0000000..76fd89a --- /dev/null +++ b/examples/fern_documentation/python/rbac/role/create.py @@ -0,0 +1,32 @@ +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import Permission, Policy, Role + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +roles = compass_root_client.create_roles( + roles=[ + # * allows all index + Role( + role_name="developer_read_all_index", + policies=[Policy(indexes=["*"], permission=Permission.READ)], + ), + Role( + role_name="developer_write_all_index", + policies=[Policy(indexes=["*"], permission=Permission.WRITE)], + ), + # user-* allows index only prefixed with `user-` + Role( + role_name="developer_read_specific_index", + policies=[Policy(indexes=["user-*"], permission=Permission.READ)], + ), + ] +) + +for role in roles: + print(f"Role information: {role.role_name}, {role.policies}") diff --git a/examples/fern_documentation/python/rbac/role/delete.py b/examples/fern_documentation/python/rbac/role/delete.py new file mode 100644 index 0000000..8e08ce5 --- /dev/null +++ b/examples/fern_documentation/python/rbac/role/delete.py @@ -0,0 +1,15 @@ +from cohere.compass.clients.access_control import CompassRootClient + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... +ROLE_NAME = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +roles = compass_root_client.delete_roles(role_names=[ROLE_NAME]) + +for role in roles: + print(f"Deleted role: {role.role_name}") diff --git a/examples/fern_documentation/python/rbac/role/get_details.py b/examples/fern_documentation/python/rbac/role/get_details.py new file mode 100644 index 0000000..f367b80 --- /dev/null +++ b/examples/fern_documentation/python/rbac/role/get_details.py @@ -0,0 +1,14 @@ +from cohere.compass.clients.access_control import CompassRootClient + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... +ROLE_NAME = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) +detailed_role = compass_root_client.get_detailed_role(role_name=ROLE_NAME) + +print(f"Role name: {detailed_role.role_name}") +print(f"Group name: {detailed_role.groups}") diff --git a/examples/fern_documentation/python/rbac/role/get_groups_page.py b/examples/fern_documentation/python/rbac/role/get_groups_page.py new file mode 100644 index 0000000..ecb89da --- /dev/null +++ b/examples/fern_documentation/python/rbac/role/get_groups_page.py @@ -0,0 +1,43 @@ +from typing import Optional + +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import Group, PageDirection, PageInfo + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... +ROLE_NAME = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + + +def get_all_role_groups( + role_name: str, + filter: Optional[str] = None, + page_info: Optional[PageInfo] = None, + groups: Optional[list[Group]] = None, + direction: Optional[PageDirection] = None, +) -> list[Group]: + role_pages = compass_root_client.get_role_groups_page( + role_name=role_name, filter=filter, page_info=page_info, direction=direction + ) + + groups = groups or [] + groups.extend(role_pages.groups) + + if not role_pages.page_info.has_next_page(): + return groups + + return get_all_role_groups( + role_name=role_name, + filter=filter, + page_info=role_pages.page_info, + groups=groups, + direction=PageDirection.NEXT, + ) + + +group = get_all_role_groups(role_name=ROLE_NAME) +print(f"Groups in role: {group}") diff --git a/examples/fern_documentation/python/rbac/role/get_pages.py b/examples/fern_documentation/python/rbac/role/get_pages.py new file mode 100644 index 0000000..9746152 --- /dev/null +++ b/examples/fern_documentation/python/rbac/role/get_pages.py @@ -0,0 +1,37 @@ +from typing import Optional + +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import PageDirection, PageInfo, Role + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + + +def get_all_roles( + filter: Optional[str] = None, + page_info: Optional[PageInfo] = None, + roles: Optional[list[Role]] = None, + direction: Optional[PageDirection] = None, +) -> list[Role]: + roles_page = compass_root_client.get_roles_page( + filter=filter, page_info=page_info, direction=direction + ) + + roles = roles or [] + roles.extend(roles_page.roles) + + if not roles_page.page_info.has_next_page(): + return roles + + return get_all_roles( + filter=filter, page_info=page_info, direction=PageDirection.NEXT + ) + + +roles = get_all_roles() +print(f"Roles: {roles}") diff --git a/examples/fern_documentation/python/rbac/role/update.py b/examples/fern_documentation/python/rbac/role/update.py new file mode 100644 index 0000000..8c247dd --- /dev/null +++ b/examples/fern_documentation/python/rbac/role/update.py @@ -0,0 +1,19 @@ +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import Permission, Policy, Role + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +role = compass_root_client.update_role( + role=Role( + role_name="developer_read_all_index", + policies=[Policy(indexes=["prefix-*"], permission=Permission.READ)], + ) +) + +print(f"Updated role information: {role.role_name}, {role.policies}") diff --git a/examples/fern_documentation/python/rbac/user/create.py b/examples/fern_documentation/python/rbac/user/create.py new file mode 100644 index 0000000..bff7c1d --- /dev/null +++ b/examples/fern_documentation/python/rbac/user/create.py @@ -0,0 +1,18 @@ +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import User + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +users_with_token = compass_root_client.create_users( + users=[User(user_name="unique_user_name_1"), User(user_name="unique_user_name_2")] +) + +for user in users_with_token: + # Token is used to access the Compass API on behalf of the user + print(f"User information: {user.user_name}, Token: {user.token}") diff --git a/examples/fern_documentation/python/rbac/user/delete.py b/examples/fern_documentation/python/rbac/user/delete.py new file mode 100644 index 0000000..f1de228 --- /dev/null +++ b/examples/fern_documentation/python/rbac/user/delete.py @@ -0,0 +1,16 @@ +from cohere.compass.clients.access_control import CompassRootClient + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +users = compass_root_client.delete_users( + user_names=["unique_user_name_1", "unique_user_name_2"] +) + +for user in users: + print(f"Deleted users: {user.user_name}") diff --git a/examples/fern_documentation/python/rbac/user/get_details.py b/examples/fern_documentation/python/rbac/user/get_details.py new file mode 100644 index 0000000..737a382 --- /dev/null +++ b/examples/fern_documentation/python/rbac/user/get_details.py @@ -0,0 +1,16 @@ +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import DetailedUser + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... +USER_NAME = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + +detailed_user: DetailedUser = compass_root_client.get_detailed_user(user_name=USER_NAME) + +print(f"User name {detailed_user.user_name}") +print(f"User groups {detailed_user.groups}") diff --git a/examples/fern_documentation/python/rbac/user/get_group_page.py b/examples/fern_documentation/python/rbac/user/get_group_page.py new file mode 100644 index 0000000..45eedaa --- /dev/null +++ b/examples/fern_documentation/python/rbac/user/get_group_page.py @@ -0,0 +1,43 @@ +from typing import Optional + +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import Group, PageDirection, PageInfo + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... +USER_NAME = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + + +def get_all_user_groups( + user_name: str, + filter: Optional[str] = None, + page_info: Optional[PageInfo] = None, + groups: Optional[list[Group]] = None, + direction: Optional[PageDirection] = None, +) -> list[Group]: + group_pages = compass_root_client.get_user_groups_page( + user_name=user_name, filter=filter, page_info=page_info, direction=direction + ) + + groups = groups or [] + groups.extend(group_pages.groups) + + if not group_pages.page_info.has_next_page(): + return groups + + return get_all_user_groups( + user_name=user_name, + filter=filter, + page_info=group_pages.page_info, + groups=groups, + direction=PageDirection.NEXT, + ) + + +groups = get_all_user_groups(user_name=USER_NAME) +print(f"User groups: {groups}") diff --git a/examples/fern_documentation/python/rbac/user/get_page.py b/examples/fern_documentation/python/rbac/user/get_page.py new file mode 100644 index 0000000..44875d2 --- /dev/null +++ b/examples/fern_documentation/python/rbac/user/get_page.py @@ -0,0 +1,42 @@ +from typing import Optional + +from cohere.compass.clients.access_control import CompassRootClient +from cohere.compass.models.access_control import PageDirection, PageInfo, User + +COMPASS_API_URL = ... +ROOT_USER_TOKEN = ... + +compass_root_client = CompassRootClient( + compass_url=COMPASS_API_URL, + root_user_token=ROOT_USER_TOKEN, +) + + +def get_all_users( + filter: Optional[str] = None, + page_info: Optional[PageInfo] = None, + direction: Optional[PageDirection] = None, + users: Optional[list[User]] = None, +) -> list[User]: + user_page = compass_root_client.get_users_page( + filter=filter, + page_info=page_info, + direction=direction, + ) + + users = users if users else [] + users.extend(user_page.users) + if not user_page.page_info.has_next_page(): + return users + return get_all_users( + filter=filter, + page_info=user_page.page_info, + direction=PageDirection.NEXT, + users=users, + ) + + +users = get_all_users() + +for user in users: + print(f"User name {user.user_name}")