Skip to content

Commit

Permalink
refactor: restructure API and remove deprecated send functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jan 19, 2025
1 parent 446e6f1 commit f1ee49e
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 110 deletions.
13 changes: 0 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

*

## [0.2.1] - 2024-04-30

### Changed

* Direct inclusion of pyi files in `setup.py`

## [0.2.0] - 2024-04-20

### Changed

* Sanitization of the code structure, making compliant with `black`
* Improved support for type hints
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
keywords="colony-print api",
url="http://colony-print-api.hive.pt",
zip_safe=False,
packages=["colony_print"],
packages=["colony_print", "colony_print.scripts"],
test_suite="colony_print.test",
package_dir={"": os.path.normpath("src")},
package_data={"colony_print": ["*.pyi"]},
Expand Down
4 changes: 3 additions & 1 deletion src/colony_print/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
# -*- coding: utf-8 -*-

from . import base
from . import node

from .base import BASE_URL, API, Attachment, AttachmentPayload, Message, MessagePayload
from .base import BASE_URL, API, Ping
from .node import NodeAPI, Node
23 changes: 4 additions & 19 deletions src/colony_print/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

import appier

from .node import NodeAPI

BASE_URL = "https://print.bemisc.com/api/"
""" The default base URL to be used when no other
base URL value is provided to the constructor """


class API(appier.API):
class API(appier.API, NodeAPI):
"""
Implementation of the Colony Print API specification
for a simplified python client usage.
Expand Down Expand Up @@ -42,23 +44,6 @@ def ping(self):
contents = self.get(url, auth=False)
return contents

def send(self, payload):
url = self.base_url + "send"
contents = self.post(url, data_j=payload)
return contents


class Attachment(dict):
pass


class AttachmentPayload(dict):
pass


class Message(dict):
pass


class MessagePayload(dict):
class Ping(dict):
pass
60 changes: 7 additions & 53 deletions src/colony_print/base.pyi
Original file line number Diff line number Diff line change
@@ -1,58 +1,12 @@
from typing import NotRequired, Sequence, TypedDict
from typing import TypedDict
from appier import API as BaseAPI

BASE_URL: str = ...

class Attachment(TypedDict):
name: str
data: str
mime: str
hash: str
etag: str
guid: str
engine: str
from .node import NodeAPI

class AttachmentPayload(TypedDict):
name: str
data: str
mime: NotRequired[str]
hash: NotRequired[str]
etag: NotRequired[str]
guid: NotRequired[str]
engine: NotRequired[str]

class Message(TypedDict):
sender: str | None
receivers: Sequence[str]
cc: Sequence[str]
bcc: Sequence[str]
reply_to: Sequence[str]
subject: str
title: str
subtitle: str | None
contents: str
html: str
plain: str
copyright: str
logo_url: str | None
attachments: Sequence[Attachment]
id: str | None
inline: bool
style: str
mode: str
BASE_URL: str = ...

class MessagePayload(TypedDict):
receivers: Sequence[str]
subject: NotRequired[str]
title: NotRequired[str]
contents: NotRequired[str]
html: NotRequired[str]
plain: NotRequired[str]
copyright: NotRequired[str]
attachments: NotRequired[Sequence[AttachmentPayload]]
inline: NotRequired[bool]
style: NotRequired[str]
mode: NotRequired[str]
class Ping(TypedDict):
time: float

class API(BaseAPI):
def send(self, payload: MessagePayload) -> Message: ...
class API(BaseAPI, NodeAPI):
def ping(self) -> Ping: ...
14 changes: 14 additions & 0 deletions src/colony_print/node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-


class NodeAPI(object):

def list_nodes(self, *args, **kwargs):
url = self.base_url + "nodes"
contents = self.get(url, **kwargs)
return contents


class Node(dict):
pass
14 changes: 14 additions & 0 deletions src/colony_print/node.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

from typing import NotRequired, Sequence, TypedDict

class Node(TypedDict):
name: str
mode: str
location: NotRequired[str]
node_printer: NotRequired[str]
engines: Sequence[str]

class NodeAPI:
def list_nodes(self) -> Sequence[Node]: ...
4 changes: 2 additions & 2 deletions src/colony_print/scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

from . import sender
from . import printer

from .sender import send
from .printer import print
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
import colony_print


def send(*args, **kwargs):
def print(*args, **kwargs):
api = colony_print.API()
return api.send(kwargs)


if __name__ == "__main__":
receivers = appier.conf("RECEIVERS", [], cast=list)
subject = appier.conf("SUBJECT", None)
title = appier.conf("TITLE", None)
contents = appier.conf("CONTENTS", None)
copyright = appier.conf("COPYRIGHT", None)
node = appier.conf("NODE", [], cast=list)
printer = appier.conf("PRINTER", None)

kwargs = dict()
if receivers:
Expand Down
18 changes: 3 additions & 15 deletions src/examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,10 @@ def ping(self):
result = api.ping()
return result

@appier.route("/send", "GET")
def send(self):
receivers = self.field("receivers", [], cast=list)
subject = self.field("subject", "Test email")
title = self.field("title", "Test email")
contents = self.field("contents", "Test email")
copyright = self.field("copyright", "Hive Solutions")
payload = dict(
receivers=receivers,
subject=subject,
title=title,
contents=contents,
copyright=copyright,
)
@appier.route("/nodes", "GET")
def nodes(self):
api = self.get_api()
result = api.send(payload)
result = api.list_nodes()
return result

def get_api(self):
Expand Down
6 changes: 6 additions & 0 deletions src/examples/base.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import colony_print

def get_api() -> colony_print.API: ...

0 comments on commit f1ee49e

Please sign in to comment.