diff --git a/.vscode/settings.json b/.vscode/settings.json index f197672..0ab04cf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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, diff --git a/CHANGELOG.md b/CHANGELOG.md index 39c48e5..24cac34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/Dockerfile b/Dockerfile index 5e88a12..b93d636 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/core/addons/api/__init__.py b/core/addons/api/__init__.py index 3082294..4ae7c46 100644 --- a/core/addons/api/__init__.py +++ b/core/addons/api/__init__.py @@ -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): diff --git a/core/const.py b/core/const.py index 82c2815..4e2333d 100644 --- a/core/const.py +++ b/core/const.py @@ -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) diff --git a/core/webserver/request.py b/core/webserver/request.py index 0d94414..0e0ff8f 100644 --- a/core/webserver/request.py +++ b/core/webserver/request.py @@ -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.""" @@ -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: @@ -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: