Skip to content

Commit

Permalink
Add basic support for EmailTemplate object
Browse files Browse the repository at this point in the history
  • Loading branch information
rehoumir committed Aug 14, 2024
1 parent a922d31 commit 73a7289
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 2 deletions.
1 change: 1 addition & 0 deletions rossum_api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Resource(Enum):
Auth = "auth"
Connector = "connectors"
Document = "documents"
EmailTemplate = "email_templates"
Group = "groups"
Hook = "hooks"
Inbox = "inboxes"
Expand Down
25 changes: 25 additions & 0 deletions rossum_api/elis_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions rossum_api/elis_api_client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions rossum_api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions rossum_api/models/email_template.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions tests/elis_api_client/test_connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
114 changes: 114 additions & 0 deletions tests/elis_api_client/test_email_templates.py
Original file line number Diff line number Diff line change
@@ -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": "<p>My Email Template Message</p>",
"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": "<p>My Email Template Message</p>",
"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)

0 comments on commit 73a7289

Please sign in to comment.