Skip to content

Commit

Permalink
add compression to photos response (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebino committed Jun 5, 2023
1 parent 77901da commit 5e29e6f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"python.formatting.blackArgs": ["--line-length", "120"],
"[python]": {
"editor.tabSize": 4,
"editor.formatOnSave": true
"editor.formatOnSave": false
},
"python.terminal.activateEnvInCurrentTerminal": true,
"python.testing.unittestEnabled": false,
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.5.2] - 2023-06-04
### Added
- compression to photos requests


## [0.5.1] - 2022-07-07
### Removed
- unused client_secret from oauth calls
Expand Down Expand Up @@ -44,6 +49,7 @@
- dynamic addon loading with dedicated setup step
- async file logging

[0.5.2]: https://github.com/photos-network/core/compare/Release/v0.5.1...Release/v0.5.2
[0.5.1]: https://github.com/photos-network/core/compare/Release/v0.5.0...Release/v0.5.1
[0.5.0]: https://github.com/photos-network/core/compare/Release/v0.4.0...Release/v0.5.0
[0.4.0]: https://github.com/photos-network/core/compare/Release/v0.3.0...Release/v0.4.0
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.10
LABEL "description"="Photos.network core system"
LABEL "version"="0.5.1"
LABEL "version"="0.5.2"
LABEL "maintainer"="github.com/photos-network"

WORKDIR /app
Expand Down
5 changes: 2 additions & 3 deletions core/addons/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ async def get(self, core: ApplicationCore, request: web.Request) -> web.Response
response = PhotosResponse(
offset=offset, limit=limit, size=len(results), results=results
)
return web.Response(
text=json.dumps(response, cls=PhotoEncoder), content_type="application/json"
)

return self.json(result=response, encoder=PhotoEncoder)


class PhotoDetailsView(RequestView):
Expand Down
2 changes: 1 addition & 1 deletion core/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Constants used by Photos.network core."""
MAJOR_VERSION = 0
MINOR_VERSION = 5
PATCH_VERSION = 1
PATCH_VERSION = 2
__short_version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}"
__version__ = f"{__short_version__}.{PATCH_VERSION}"
REQUIRED_PYTHON_VER = (3, 7, 1)
Expand Down
25 changes: 13 additions & 12 deletions core/webserver/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ def is_callback(func: Callable[..., Any]) -> bool:
return getattr(func, "_callback", False) is True


class ComplexEncoder(json.JSONEncoder):
"""Encoder for complex classes."""

def default(self, o):
"""Encode all properties."""
if isinstance(o, complex):
return [o.real, o.imag]
# Let the base class default method raise the TypeError.
return json.JSONEncoder.default(self, o)


class RequestView:
"""Base request."""

Expand All @@ -40,10 +51,11 @@ def json(
result: Any,
status_code: int = HTTP_OK,
headers: Optional[LooseHeaders] = None,
encoder: json.JSONEncoder = ComplexEncoder,
) -> web.Response:
"""Return a JSON response."""
try:
msg = json.dumps(result, cls=ComplexEncoder, allow_nan=False).encode(
msg = json.dumps(result, cls=encoder, allow_nan=False).encode(
"UTF-8"
)
except (ValueError, TypeError) as err:
Expand Down Expand Up @@ -89,17 +101,6 @@ def register(self, core: "ApplicationCore", router: web.UrlDispatcher) -> None:
routes.append(router.add_route(method, url, handler))


class ComplexEncoder(json.JSONEncoder):
"""Encoder for complex classes."""

def default(self, o):
"""Encode all properties."""
if isinstance(o, complex):
return [o.real, o.imag]
# Let the base class default method raise the TypeError.
return json.JSONEncoder.default(self, o)


def request_handler_factory(
view: RequestView, core: "ApplicationCore", handler: Callable
) -> Callable:
Expand Down

0 comments on commit 5e29e6f

Please sign in to comment.