Skip to content

Commit

Permalink
fix: Format Flags
Browse files Browse the repository at this point in the history
  • Loading branch information
OseSem committed Nov 24, 2024
1 parent 4715e35 commit bcfbf19
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 78 deletions.
87 changes: 30 additions & 57 deletions battlemetrics/flag.py
Original file line number Diff line number Diff line change
@@ -1,122 +1,95 @@
from __future__ import annotations

from datetime import UTC, datetime
from typing import TYPE_CHECKING

from . import utils
from .types.flag import Flag as FlagPayload
from .types.flag import (
FlagAttributes,
FlagRelationships
)

from datetime import datetime, UTC
from .types.flag import FlagAttributes, FlagRelationships

if TYPE_CHECKING:
from .http import HTTPClient

__all__ = ("Flag",)


class Flag:
"""Represents a Flag"""
"""Represents a Flag."""

def __init__(self, *, data :FlagPayload, http : HTTPClient) -> None:
def __init__(self, *, data: FlagPayload, http: HTTPClient) -> None:

self._http = http

self._data : FlagPayload = FlagPayload(**data)
self._attributes : FlagAttributes = data.get("attributes")
self._relationships : FlagRelationships = (
utils.format_relationships(
# type: ignore [reportAttributeAccessIssue]
data.get("relationships")
)
self._data: FlagPayload = FlagPayload(**data)
self._attributes: FlagAttributes = data.get("attributes")
self._relationships: FlagRelationships = utils.format_relationships(
# type: ignore [reportAttributeAccessIssue]
data.get("relationships"),
)

def __str__(self) -> str:
"""Return when the string method is run on this Flag"""
return self._attributes.get("name")
"""Return when the string method is run on this Flag."""
return self._attributes.get("name")

@property
def id(self) -> str:
"""Returns ID of the Flag"""
"""Returns ID of the Flag."""
return self._data.get("id")



@property
def name(self) -> str:
"""Returns Name of the Flag"""
"""Returns Name of the Flag."""
return self._attributes.get("name")

@property
def description(self) -> str | None:
"""Returns Description of the Flag"""
"""Returns Description of the Flag."""
return self._attributes.get("description")

@property
def color(self) -> str:
"""Returns Color of the Flag"""
"""Returns Color of the Flag."""
return self._attributes.get("color")

@property
def created_at(self) -> datetime:
"""Returns creation date of the Flag"""

"""Returns creation date of the Flag."""
return datetime.strptime(
self._attributes.get("createdAt"),
"%Y-%m-%dT%H:%M:%S.%fZ",
).replace(
tzinfo=UTC,
)

@property
def updated_at(self) -> datetime:
"""Returns updated date of the Flag"""
"""Returns updated date of the Flag."""
return datetime.strptime(
self._attributes.get("updatedAt"),
"%Y-%m-%dT%H:%M:%S.%fZ",
).replace(
tzinfo=UTC,
)



@property
def icon(self) -> str | None:
"""Returns Icon of the Flag"""
"""Returns Icon of the Flag."""
return self._attributes.get("icon")




@property
def organization_id(self) -> int:
"""Returns Organization ID of the Flag"""
"""Returns Organization ID of the Flag."""
return self._relationships.get("organization_id")

@property
def user_id(self) -> int:
"""Returns user ID of the Flag"""
"""Returns user ID of the Flag."""
return self._relationships.get("user_id")


# TODO : add def Player Flag Update

# TODO : add def Player Flag Delete

async def update(self):
...

async def delete(self):
...











async def update(self) -> None: ... # noqa: D102


async def delete(self) -> None: ... # noqa: D102
8 changes: 3 additions & 5 deletions battlemetrics/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

from . import utils
from .errors import BMException, Forbidden, HTTPException, NotFound, Unauthorized
from .flag import Flag
from .note import Note
from .organization import Organization
from .player import Player
from .server import Server
from .flag import Flag
from .types.organization import OrganizationPlayerStats

if TYPE_CHECKING:
Expand Down Expand Up @@ -1245,7 +1245,7 @@ async def player_session_history(
params=data,
)

async def player_flags(self, player_id: int) -> dict[str, Any]:
async def player_flags(self, player_id: int) -> list[Flag]:
"""Return all the flags on a players profile.
Parameters
Expand All @@ -1261,13 +1261,11 @@ async def player_flags(self, player_id: int) -> dict[str, Any]:
"include": "playerFlag",
}


r = await self.request(
Route(method="GET", path=f"/players/{player_id}/relationships/flags"),
params=data,
)
flagData = r.get("included", [])
return [Flag(data=flag, http=self) for flag in flagData]
return [Flag(data=flag, http=self) for flag in r.get("included")]

async def add_flag(self, player_id: int, flag_id: str | None = None) -> dict[str, Any]:
"""Create or add a flag to the targeted players profile.
Expand Down
32 changes: 16 additions & 16 deletions battlemetrics/types/flag.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
from typing import TypedDict



class FlagAttributes(TypedDict):
"""Represents the attributes of a Flag."""

name : str
description : str | None

color : str
icon : str | None
name: str
description: str | None
color: str
icon: str | None
createdAt: str
updatedAt: str

createdAt : str
updatedAt : str

class FlagRelationships(TypedDict):
"""Represents the relationships of a Flag."""

user_id : int
organization_id : int
user_id: int
organization_id: int

class Flag(TypedDict):

id : str
type : str
attributes : FlagAttributes
relationships : FlagRelationships

class Flag(TypedDict):
"""Represents a Flag."""

id: str
type: str
attributes: FlagAttributes
relationships: FlagRelationships

0 comments on commit bcfbf19

Please sign in to comment.