From d19f58699f37a40cb45dc4c4aa435ce05de8c2a6 Mon Sep 17 00:00:00 2001 From: PerchunPak Date: Tue, 23 Jul 2024 17:31:17 +0200 Subject: [PATCH] Add `as_json` to responses --- mcstatus/responses.py | 7 ++++++- tests/responses/test_bedrock.py | 10 ++++++++++ tests/responses/test_java.py | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/mcstatus/responses.py b/mcstatus/responses.py index 6786fafe..05acf6d1 100644 --- a/mcstatus/responses.py +++ b/mcstatus/responses.py @@ -1,7 +1,7 @@ from __future__ import annotations from abc import ABC, abstractmethod -from dataclasses import dataclass +from dataclasses import dataclass, asdict from typing import Any, TYPE_CHECKING from mcstatus.forge_data import ForgeData, RawForgeData @@ -101,6 +101,11 @@ def build(cls, *args, **kwargs) -> Self: """ raise NotImplementedError("You can't use abstract methods.") + def as_json(self) -> dict: + as_dict = asdict(self) + as_dict["motd"] = self.motd.simplify().to_minecraft() + return as_dict + @dataclass(frozen=True) class JavaStatusResponse(BaseStatusResponse): diff --git a/tests/responses/test_bedrock.py b/tests/responses/test_bedrock.py index 8281b24b..8f154434 100644 --- a/tests/responses/test_bedrock.py +++ b/tests/responses/test_bedrock.py @@ -65,6 +65,16 @@ def test_optional_parameters_is_none(self, field, pop_index): build = BedrockStatusResponse.build(parameters, 123.0) assert getattr(build, field) is None + def test_as_json(self, build: BedrockStatusResponse): + assert build.as_json() == { + "gamemode": "Default", + "latency": 123.0, + "map_name": "map name here", + "motd": "§4G§6a§ey§2B§1o§9w§ds§4e§6r", + "players": {"max": 69, "online": 1}, + "version": {"brand": "MCPE", "name": "1.18.100500", "protocol": 422}, + } + @BaseResponseTest.construct class TestBedrockStatusPlayers(BaseResponseTest): diff --git a/tests/responses/test_java.py b/tests/responses/test_java.py index 256cafcd..0a59dba6 100644 --- a/tests/responses/test_java.py +++ b/tests/responses/test_java.py @@ -40,6 +40,24 @@ class TestJavaStatusResponse(BaseResponseTest): def build(self) -> JavaStatusResponse: return JavaStatusResponse.build(self.RAW) # type: ignore # dict[str, Unknown] cannot be assigned to TypedDict + def test_as_json(self, build: JavaStatusResponse): + assert build.as_json() == { + "enforces_secure_chat": True, + "forge_data": None, + "icon": "data:image/png;base64,foo", + "latency": 0, + "motd": "A Minecraft Server", + "players": {"max": 20, "online": 0, "sample": None}, + "raw": { + "description": "A Minecraft Server", + "enforcesSecureChat": True, + "favicon": "data:image/png;base64,foo", + "players": {"max": 20, "online": 0}, + "version": {"name": "1.8-pre1", "protocol": 44}, + }, + "version": {"name": "1.8-pre1", "protocol": 44}, + } + @BaseResponseTest.construct class TestJavaStatusPlayers(BaseResponseTest):