Skip to content

Commit

Permalink
fix: Localize dates to TimeZone Asia/Jerusalem (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuyKh committed Mar 3, 2024
1 parent 9bc3a11 commit 2667600
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions iec_api/const.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytz

HEADERS_NO_AUTH = {
"authority": "iecapi.iec.co.il",
"accept": "application/json, text/plain, */*",
Expand All @@ -21,6 +23,7 @@
HEADERS_WITH_AUTH["Authorization"] = "Bearer 1234"
HEADERS_WITH_AUTH["Cookie"] = "ARRAffinity=?; " "ARRAffinitySameSite=?;" " GCLB=?"

TIMEZONE = pytz.timezone("Asia/Jerusalem")
IEC_API_BASE_URL = "https://iecapi.iec.co.il//api/"
GET_ACCOUNTS_URL = IEC_API_BASE_URL + "outages/accounts"
GET_CONSUMER_URL = IEC_API_BASE_URL + "customer"
Expand Down
8 changes: 8 additions & 0 deletions iec_api/models/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mashumaro import DataClassDictMixin, field_options
from mashumaro.codecs import BasicDecoder

from iec_api.const import TIMEZONE
from iec_api.models.meter_reading import MeterReading
from iec_api.models.response_descriptor import ResponseWithDescriptor

Expand Down Expand Up @@ -78,6 +79,13 @@ class Invoice(DataClassDictMixin):
metadata=field_options(alias="meterReadings"), default_factory=lambda: []
)

@classmethod
def __post_deserialize__(cls, obj: "Invoice") -> "Invoice":
obj.full_date = TIMEZONE.localize(obj.full_date)
obj.from_date = TIMEZONE.localize(obj.from_date)
obj.to_date = TIMEZONE.localize(obj.to_date)
return obj


@dataclass
class Property(DataClassDictMixin):
Expand Down
6 changes: 6 additions & 0 deletions iec_api/models/meter_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from mashumaro import DataClassDictMixin, field_options
from mashumaro.codecs import BasicDecoder

from iec_api.const import TIMEZONE
from iec_api.models.response_descriptor import ResponseWithDescriptor

# GET https://iecapi.iec.co.il//api/Device/LastMeterReading/{contract_id}/{bp_number}
Expand Down Expand Up @@ -48,6 +49,11 @@ class MeterReading(DataClassDictMixin):
usage: str
serial_number: str = field(metadata=field_options(alias="serialNumber"))

@classmethod
def __post_deserialize__(cls, obj: "MeterReading") -> "MeterReading":
obj.reading_date = TIMEZONE.localize(obj.reading_date)
return obj


@dataclass
class LastMeters(DataClassDictMixin):
Expand Down
7 changes: 7 additions & 0 deletions iec_api/models/remote_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from mashumaro import DataClassDictMixin, field_options
from mashumaro.config import BaseConfig

from iec_api.const import TIMEZONE


class ReadingResolution(IntEnum):
DAILY = 1
Expand Down Expand Up @@ -75,6 +77,11 @@ class RemoteReading(DataClassDictMixin):
date: datetime
value: float

@classmethod
def __post_deserialize__(cls, obj: "RemoteReading") -> "RemoteReading":
obj.date = TIMEZONE.localize(obj.date)
return obj


@dataclass
class RemoteReadingResponse(DataClassDictMixin):
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "iec-api"
version = "0.2.3"
version = "0.2.4"
description = "A Python wrapper for Israel Electric Company API"
authors = ["GuyKh"]
license = "MIT"
Expand All @@ -20,6 +20,7 @@ pkce = "^1.0.3"
aiohttp = "^3.9.1"
aiofiles = "^23.2.1"
loguru = "^0.7.2"
pytz = "^2024.1"

[tool.poetry.dev-dependencies]
pytest = "8.0.2"
Expand Down

0 comments on commit 2667600

Please sign in to comment.