#From Json to a class type

1 messages · Page 1 of 1 (latest)

lucid bloom
#

Hi there!
Question a bit complicated for me.

I'm currently working with an API and it allows me, a bit like discord, to retrieve lists of users and servers.

To do something a bit like Py-Cord I'm trying to create classes. Server, User to return this rather than a not very friendly json.

Of course, I end up with a lot of different values and I asked myself a question. Do I do a for each and assign to the variables the content of what the json returns? Or should I put everything in the init and assign it at that point?

Another question: I've noticed that the guild.py present in the types only contains a kind of "enum". However, in the discord.guild type, you can perform a huge number of different actions. So, where should I add my methods in my class? In another file?

class Server:
    def __init__(self, id, name, description, node, sftp_details):
        self.id = id
        self.name = name
        self.description = description
        self.node = node
        self.sftp_details = sftp_details
#
{
            "object": "server",
            "attributes": {
                "id": 1,
                "external_id": null,
                "uuid": "xxxxxx-xxxx-xxxx-xxx",
                "identifier": "xxxxxxxx",
                "name": "Server Name",
                "description": "",
                "status": null,
                "suspended": false,
                "limits": {
                    "memory": 0,
                    "swap": 0,
                    "disk": 0,
                    "io": 500,
                    "cpu": 0,
                    "threads": null,
                    "oom_disabled": true
                },
                "feature_limits": {
                    "databases": 1,
                    "allocations": 1,
                    "backups": 5
                },
                "user": 1,
                "node": 1,
                "allocation": 120,
                "nest": 6,
                "egg": 26,
                "container": {
                    "startup_command": "wine ./SonsOfTheForestDS.exe -userdatapath \"/home/container/serverconfig\" -dedicatedserver.IpAddress \"0.0.0.0\" -dedicatedserver.GamePort \"{{SERVER_PORT}}\" -dedicatedserver.QueryPort \"{{QUERY_PORT}}\" -dedicatedserver.BlobSyncPort \"{{BLOBSYNC_PORT}}\" -dedicatedserver.MaxPlayers \"{{MAX_PLAYERS}}\" -dedicatedserver.Password \"{{SRV_PW}}\" -dedicatedserver.GameMode \"{{GAME_MODE}}\" -dedicatedserver.SkipNetworkAccessibilityTest \"{{SKIP_TESTS}}\" -dedicatedserver.LogFilesEnabled \"true\" -dedicatedserver.TimestampLogFilenames \"true\"",
                    "image": "ghcr.io/parkervcp/yolks:wine_latest",
                    "installed": 1,
                    "environment": {
                        "QUERY_PORT": "00000",
                        "BLOBSYNC_PORT": "9700",
                        "SRV_NAME": "Name",
                        "MAX_PLAYERS": "8",
                        "SRV_PW": "pass",
                        "GAME_MODE": "hard",
                        "AUTO_UPDATE": "1",
                        "SRCDS_APPID": "2465200",
                        "WINEDEBUG": "-all",
                        "WINEARCH": "win64",
                        "WINEPATH": "/home/container",
                        "WINETRICKS_RUN": "mono vcrun2019",
                        "WINDOWS_INSTALL": "1",
                        "SKIP_TESTS": "false",
                        "STARTUP": "wine ./SonsOfTheForestDS.exe -userdatapath \"/home/container/serverconfig\" -dedicatedserver.IpAddress \"0.0.0.0\" -dedicatedserver.GamePort \"{{SERVER_PORT}}\" -dedicatedserver.QueryPort \"{{QUERY_PORT}}\" -dedicatedserver.BlobSyncPort \"{{BLOBSYNC_PORT}}\" -dedicatedserver.MaxPlayers \"{{MAX_PLAYERS}}\" -dedicatedserver.Password \"{{SRV_PW}}\" -dedicatedserver.GameMode \"{{GAME_MODE}}\" -dedicatedserver.SkipNetworkAccessibilityTest \"{{SKIP_TESTS}}\" -dedicatedserver.LogFilesEnabled \"true\" -dedicatedserver.TimestampLogFilenames \"true\"",
                        "P_SERVER_LOCATION": "Server Loc",
                        "P_SERVER_UUID": "xxxxxxx",
                        "P_SERVER_ALLOCATION_LIMIT": 1
                    }
                },
                "updated_at": "2024-02-23T11:39:33+00:00",
                "created_at": "2024-02-23T11:37:08+00:00"
            }
        }
craggy pine