ESN-Trondheim / ESNbot

A Slack bot for ESN Trondheim
MIT License
3 stars 2 forks source link

`parse_slack_output()` should return an object containing the relevant info #80

Open LaiAlexander opened 1 year ago

LaiAlexander commented 1 year ago

https://github.com/ESN-Trondheim/ESNbot/blob/c265e96ed8af634cfc34945060fde3d7b07a22fc/esnbot/main.py#L65C9-L65C27

This should be abstracted more. An object should be returned, insted of selected parts of the dict (output) and the dict itself. Instead, a class should be made, containing all the fields from the response (output). It's more robust and easier to expand on in the future if we generally use strong data models for the output/response that we get from Slack.

Something akin to this:

@dataclass
class SlackFile:
    name: str
    id: str
    url: str
    ...

(
@dataclass
class User:
    name: str
    id: str
    ...
)

@dataclass
class Output:
    text: str
    channel: str
    user: User
    subtype: str | None
    file: SlackFile
    ...

Doesn't have to be dataclasses. SlackFile and User isn't strictly necessary, but it could be useful to make classes that hold their info as well.

Then all attributes of the classes (and the response) would be readily available. Developers don't have to magically know all the keys in the output dict.

This would also simplify parts of the code. e.g watermark(): https://github.com/ESN-Trondheim/ESNbot/blob/c265e96ed8af634cfc34945060fde3d7b07a22fc/esnbot/commands.py#L206-L233

Here we access output several places. It would be nice to just access e.g. output.file.id instead.

Could also possibly implement some kind of validation in the classes themselves.