-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcustom_payload_translator.py
68 lines (51 loc) · 2.09 KB
/
custom_payload_translator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Defines CustomPayloadTranslator class that holds custom serialization/deserialization code"""
from __future__ import absolute_import
import abc
from typing import IO
CONTENT_TYPE = "application/custom"
ACCEPT_TYPE = "application/custom"
class CustomPayloadTranslator(abc.ABC):
"""Abstract base class for handling custom payload serialization and deserialization.
Provides a skeleton for customization requiring the overriding of the
`serialize_payload` and `deserialize_payload` methods.
Args:
content_type (str): The content type of the endpoint input data.
accept_type (str): The content type of the data accepted from the endpoint.
"""
# pylint: disable=E0601
def __init__(self, content_type: str = CONTENT_TYPE, accept_type: str = ACCEPT_TYPE) -> None:
# pylint: disable=unused-argument
self._content_type = content_type
self._accept_type = accept_type
@abc.abstractmethod
def serialize_payload_to_bytes(self, payload: object) -> bytes:
"""Serialize payload into bytes
Args:
payload (object): Data to be serialized into bytes.
Returns:
bytes: bytes of serialized data
"""
@abc.abstractmethod
def deserialize_payload_from_stream(self, stream: IO) -> object:
"""Deserialize stream into object.
Args:
stream (IO): Stream of bytes
Returns:
object: Deserialized data
"""
def serialize(self, payload: object, content_type: str = CONTENT_TYPE) -> bytes:
"""Placeholder docstring"""
# pylint: disable=unused-argument
return self.serialize_payload_to_bytes(payload)
def deserialize(self, stream: IO, content_type: str = CONTENT_TYPE) -> object:
"""Placeholder docstring"""
# pylint: disable=unused-argument
return self.deserialize_payload_from_stream(stream)
@property
def CONTENT_TYPE(self):
"""Placeholder docstring"""
return self._content_type
@property
def ACCEPT(self):
"""Placeholder docstring"""
return self._accept_type