-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add handling of alternative message encodings
- Loading branch information
Showing
12 changed files
with
357 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Type | ||
|
||
import json | ||
|
||
JSON_ENCODING = { | ||
'contentType': 'application/json' | ||
} | ||
|
||
class JsonEncoder: | ||
|
||
@staticmethod | ||
def decode(message: bytes, cls: Type): | ||
request = cls() | ||
dic = json.loads(message) | ||
request.__dict__.update(dic) | ||
return request | ||
|
||
@staticmethod | ||
def encode(obj) -> bytes: | ||
return json.dumps(obj, cls=JSONEncoderExtension).encode('utf8') | ||
|
||
|
||
class JSONEncoderExtension(json.JSONEncoder): | ||
""" | ||
Extension of the builtin JSON encoder to handle objects | ||
""" | ||
|
||
def default(self, obj): | ||
return obj.__dict__ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import Type | ||
import gzip | ||
|
||
from .JsonEncoder import JsonEncoder | ||
|
||
JSON_GZIP_ENCODING = { | ||
'contentType': 'application/json', | ||
'contentEncoding': 'gzip' | ||
} | ||
|
||
class JsonGzipEncoder(JsonEncoder): | ||
|
||
@staticmethod | ||
def decode(message: bytes, cls: Type): | ||
return JsonEncoder.decode(gzip.decompress(message), cls) | ||
|
||
@staticmethod | ||
def encode(obj) -> bytes: | ||
return gzip.compress(JsonEncoder.encode(obj)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.