diff --git a/rossum_api/api_client.py b/rossum_api/api_client.py index 99fc40f..8c0b383 100644 --- a/rossum_api/api_client.py +++ b/rossum_api/api_client.py @@ -31,6 +31,7 @@ class Resource(Enum): Auth = "auth" Connector = "connectors" Document = "documents" + EmailTemplate = "email_templates" Group = "groups" Hook = "hooks" Inbox = "inboxes" diff --git a/rossum_api/elis_api_client.py b/rossum_api/elis_api_client.py index 1e8ec3e..5c400f2 100644 --- a/rossum_api/elis_api_client.py +++ b/rossum_api/elis_api_client.py @@ -21,6 +21,7 @@ from rossum_api.models.annotation import Annotation from rossum_api.models.connector import Connector from rossum_api.models.document import Document + from rossum_api.models.email_template import EmailTemplate from rossum_api.models.engine import Engine from rossum_api.models.group import Group from rossum_api.models.hook import Hook @@ -551,6 +552,30 @@ async def create_new_inbox(self, data: Dict[str, Any]) -> Inbox: return self._deserializer(Resource.Inbox, inbox) + # ##### EMAIL TEMPLATES ##### + async def list_all_email_templates( + self, + ordering: Sequence[str] = (), + **filters: Any, + ) -> AsyncIterator[Connector]: + """https://elis.rossum.ai/api/docs/#list-all-email-templates.""" + async for c in self._http_client.fetch_all(Resource.EmailTemplate, ordering, **filters): + yield self._deserializer(Resource.EmailTemplate, c) + + async def retrieve_email_template(self, email_template_id: int) -> EmailTemplate: + """https://elis.rossum.ai/api/docs/#retrieve-an-email-template-object.""" + email_template = await self._http_client.fetch_one( + Resource.EmailTemplate, email_template_id + ) + + return self._deserializer(Resource.EmailTemplate, email_template) + + async def create_new_email_template(self, data: Dict[str, Any]) -> EmailTemplate: + """https://elis.rossum.ai/api/docs/#create-new-email-template-object.""" + email_template = await self._http_client.create(Resource.EmailTemplate, data) + + return self._deserializer(Resource.EmailTemplate, email_template) + # ##### CONNECTORS ##### async def list_all_connectors( self, diff --git a/rossum_api/elis_api_client_sync.py b/rossum_api/elis_api_client_sync.py index 86b7ae7..dd747d7 100644 --- a/rossum_api/elis_api_client_sync.py +++ b/rossum_api/elis_api_client_sync.py @@ -29,6 +29,7 @@ from rossum_api.models.annotation import Annotation from rossum_api.models.connector import Connector from rossum_api.models.document import Document + from rossum_api.models.email_template import EmailTemplate from rossum_api.models.engine import Engine from rossum_api.models.group import Group from rossum_api.models.hook import Hook @@ -461,6 +462,29 @@ def create_new_inbox( """https://elis.rossum.ai/api/docs/#create-a-new-inbox.""" return self.event_loop.run_until_complete(self.elis_api_client.create_new_inbox(data)) + # ##### EMAIL TEMPLATES ##### + def list_all_email_templates( + self, + ordering: Sequence[str] = (), + **filters: Any, + ) -> Iterator[Connector]: + """https://elis.rossum.ai/api/docs/#list-all-email-templates.""" + return self._iter_over_async( + self.elis_api_client.list_all_email_templates(ordering, **filters) + ) + + def retrieve_email_template(self, email_template_id: int) -> EmailTemplate: + """https://elis.rossum.ai/api/docs/#retrieve-an-email-template-object.""" + return self.event_loop.run_until_complete( + self.elis_api_client.retrieve_email_template(email_template_id) + ) + + def create_new_email_template(self, data: Dict[str, Any]) -> EmailTemplate: + """https://elis.rossum.ai/api/docs/#create-new-email-template-object.""" + return self.event_loop.run_until_complete( + self.elis_api_client.create_new_email_template(data) + ) + # ##### CONNECTORS ##### def list_all_connectors( self, diff --git a/rossum_api/models/__init__.py b/rossum_api/models/__init__.py index 8e9cdc4..9e760d6 100644 --- a/rossum_api/models/__init__.py +++ b/rossum_api/models/__init__.py @@ -9,6 +9,7 @@ from rossum_api.models.annotation import Annotation from rossum_api.models.connector import Connector from rossum_api.models.document import Document +from rossum_api.models.email_template import EmailTemplate from rossum_api.models.engine import Engine from rossum_api.models.group import Group from rossum_api.models.hook import Hook @@ -32,6 +33,7 @@ Resource.Annotation: Annotation, Resource.Connector: Connector, Resource.Document: Document, + Resource.EmailTemplate: EmailTemplate, Resource.Group: Group, Resource.Hook: Hook, Resource.Inbox: Inbox, diff --git a/rossum_api/models/email_template.py b/rossum_api/models/email_template.py new file mode 100644 index 0000000..2c9f6e9 --- /dev/null +++ b/rossum_api/models/email_template.py @@ -0,0 +1,22 @@ +from __future__ import annotations + +from dataclasses import dataclass, field +from typing import Any, Dict, List + + +@dataclass +class EmailTemplate: + id: int + name: str + url: str + queue: str + organization: str + subject: str + message: str + type: str + enabled: bool + automate: bool + triggers: List[str] = field(default_factory=list) + to: List[Dict[str, Any]] = field(default_factory=list) + cc: List[Dict[str, Any]] = field(default_factory=list) + bcc: List[Dict[str, Any]] = field(default_factory=list) diff --git a/tests/elis_api_client/test_connectors.py b/tests/elis_api_client/test_connectors.py index 189f01a..6c05753 100644 --- a/tests/elis_api_client/test_connectors.py +++ b/tests/elis_api_client/test_connectors.py @@ -23,7 +23,7 @@ def dummy_connector(): @pytest.mark.asyncio -class TestUsers: +class TestConnectors: async def test_list_all_connectors(self, elis_client, dummy_connector, mock_generator): client, http_client = elis_client http_client.fetch_all.return_value = mock_generator(dummy_connector) @@ -64,7 +64,7 @@ async def test_create_new_connector(self, elis_client, dummy_connector): http_client.create.assert_called_with(Resource.Connector, data) -class TestUsersSync: +class TestConnectorsSync: def test_list_all_connectors(self, elis_client_sync, dummy_connector, mock_generator): client, http_client = elis_client_sync http_client.fetch_all.return_value = mock_generator(dummy_connector) diff --git a/tests/elis_api_client/test_email_templates.py b/tests/elis_api_client/test_email_templates.py new file mode 100644 index 0000000..cea9cc7 --- /dev/null +++ b/tests/elis_api_client/test_email_templates.py @@ -0,0 +1,114 @@ +from __future__ import annotations + +import pytest + +from rossum_api.api_client import Resource +from rossum_api.models.email_template import EmailTemplate + + +@pytest.fixture +def dummy_email_template(): + return { + "id": 1500, + "name": "MyQ email_template", + "queue": "https://elis.rossum.ai/api/v1/queues/8199", + "url": "https://elis.rossum.ai/api/v1/email_templates/1500", + "organization": "https://elis.rossum.ai/api/v1/organizations/1500", + "subject": "test subject", + "message": "Hehe", + "type": "custom", + "enabled": True, + "automate": False, + "triggers": [], + "to": [], + "cc": [], + "bcc": [], + } + + +@pytest.mark.asyncio +class TestEmailTemplates: + async def test_list_all_email_templates( + self, elis_client, dummy_email_template, mock_generator + ): + client, http_client = elis_client + http_client.fetch_all.return_value = mock_generator(dummy_email_template) + + email_templates = client.list_all_email_templates() + + async for c in email_templates: + assert c == EmailTemplate(**dummy_email_template) + + http_client.fetch_all.assert_called_with(Resource.EmailTemplate, ()) + + @pytest.mark.asyncio + async def test_retrieve_email_template(self, elis_client, dummy_email_template): + client, http_client = elis_client + http_client.fetch_one.return_value = dummy_email_template + + cid = dummy_email_template["id"] + email_template = await client.retrieve_email_template(cid) + + assert email_template == EmailTemplate(**dummy_email_template) + + http_client.fetch_one.assert_called_with(Resource.EmailTemplate, cid) + + async def test_create_new_email_template(self, elis_client, dummy_email_template): + client, http_client = elis_client + http_client.create.return_value = dummy_email_template + + data = { + "name": "MyQ email_template", + "queue": "https://elis.rossum.ai/api/v1/queues/8199", + "subject": "Subject", + "message": "
My Email Template Message
", + "type": "custom", + } + email_template = await client.create_new_email_template(data) + + assert email_template == EmailTemplate(**dummy_email_template) + + http_client.create.assert_called_with(Resource.EmailTemplate, data) + + +class TestEmailTemplatesSync: + def test_list_all_email_templates( + self, elis_client_sync, dummy_email_template, mock_generator + ): + client, http_client = elis_client_sync + http_client.fetch_all.return_value = mock_generator(dummy_email_template) + + email_templates = client.list_all_email_templates() + + for c in email_templates: + assert c == EmailTemplate(**dummy_email_template) + + http_client.fetch_all.assert_called_with(Resource.EmailTemplate, ()) + + def test_retrieve_email_template(self, elis_client_sync, dummy_email_template): + client, http_client = elis_client_sync + http_client.fetch_one.return_value = dummy_email_template + + cid = dummy_email_template["id"] + email_template = client.retrieve_email_template(cid) + + assert email_template == EmailTemplate(**dummy_email_template) + + http_client.fetch_one.assert_called_with(Resource.EmailTemplate, cid) + + def test_create_new_email_template(self, elis_client_sync, dummy_email_template): + client, http_client = elis_client_sync + http_client.create.return_value = dummy_email_template + + data = { + "name": "MyQ email_template", + "queue": "https://elis.rossum.ai/api/v1/queues/8199", + "subject": "Subject", + "message": "My Email Template Message
", + "type": "custom", + } + email_template = client.create_new_email_template(data) + + assert email_template == EmailTemplate(**dummy_email_template) + + http_client.create.assert_called_with(Resource.EmailTemplate, data)