LAMaglan / PokeFightSimulator

A (simple) fight simulator between any two Pokemon using FastAPI with Jinja frontend
0 stars 0 forks source link

Refactor: inherit and use BaseModel only when necessary #59

Open LAMaglan opened 5 months ago

LAMaglan commented 5 months ago

Need to figure out when it is necessary to have a class inherit from BaseModel (specifically, Pokemon class). Could be that there should be a Pokemon class (no BaseModel), and a PokemonValidator (from BaseModel).

When using BaseModel, can pass json_schema_extra to Config which will have description of each attribute. That might be useful to remember erected structure etc. of input. But maybe it doesn't make sense in this context, since data is fetched (GET, not POST)?

LAMaglan commented 5 months ago

For the first part, have figured out the inheriting from BaseModel is not necessary. Then, I would have to set it up like this:

class Pokemon:

    def __init__(
        self,
        name: str,
        hp: Dict[str, int],
        attack: Dict[str, int],
        defense: Dict[str, int],
        special_attack: Dict[str, int],
        special_defense: Dict[str, int],
        speed: Dict[str, int],
        types: List[str],

        # Note: last, because parameter with default value must come after those
        # that do not have a default value
        level: int = 1,
    ):

        self.name = name
        self.level = level
        self.hp = hp
        self.attack = attack
        self.defense = defense
        self.special_attack = special_attack
        self.special_defense = special_defense
        self.speed = speed
        self.types = types

Sticking to BaseModel, as not sure what is best practice, and allows me to "skip" __init__, and possibility of json_schema_extra (to be continued)

LAMaglan commented 5 months ago

Have added to Pokemon https://github.com/LAMaglan/PokeFightSimulator/pull/61:

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "name": "FakeMon",
                    "level": 5,
                    "hp": {"base_stat": 10, "effort": 0},
                    "attack": {"base_stat": 30, "effort": 0},
                    "defense": {"base_stat": 35, "effort": 1},
                    "special_attack": {"base_stat": 25, "effort": 2},
                    "special_defense": {"base_stat": 10, "effort": 0},
                    "speed": {"base_stat": 10, "effort": 1},
                    "types": ["electric", "water"],
                }
            ]
        }
    }

However, examples are not showing up in swagger-doc. This might be because all endpoints are GET requests, and this is intended for a POST? See https://github.com/LAMaglan/PokeFightSimulator/issues/60

LAMaglan commented 5 months ago

Have at same time discovered that class Config was not necessary in Pokemon class