Skip to content

Commit

Permalink
Merge pull request #23 from hudsonbrendon/feat/notifications
Browse files Browse the repository at this point in the history
feat: add method notifications
  • Loading branch information
hudsonbrendon authored Jan 31, 2025
2 parents 5caff69 + 2152dd6 commit 4e2a8f2
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions solar_plus_intelbras/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class EndpointEnum(Enum):
RECORDS_YEAR = "records/year"
RECORDS_YEARS = "records/years"
INVERTERS = "inverters"
USER = "user"
NOTIFICATIONS = "notifications"


class PeriodEnum(Enum):
Expand Down
44 changes: 44 additions & 0 deletions solar_plus_intelbras/solar_plus_intelbras.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,47 @@ def alerts(
params=params,
)
return response.json()

def notifications(
self,
start_date: str,
end_date: str,
pendings: bool = True,
page: int = 1,
) -> dict:
"""Return the notifications.
Args:
start_date (str): A start date.
end_date (str): An end date.
pendings (bool, optional): A boolean. Defaults to True.
page (int, optional): A page. Defaults to 1.
Returns:
dict: A dictionary with the notifications.
"""
params = {
"pendings": pendings,
"page": page,
}

if start_date:
try:
datetime.strptime(start_date, "%Y-%m-%d")
params["start_date"] = start_date
except ValueError:
raise ValueError("start_date must be in the format YYYY-MM-DD.")

if end_date:
try:
datetime.strptime(end_date, "%Y-%m-%d")
params["end_date"] = end_date
except ValueError:
raise ValueError("end_date must be in the format YYYY-MM-DD.")

response = requests.get(
f"{self.base_api_url}{EndpointEnum.USER.value}/{EndpointEnum.NOTIFICATIONS.value}",
headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
params=params,
)
return response.json()
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,3 +926,16 @@ def alerts() -> dict:
"total_pages": 0,
"rows": [],
}


@pytest.fixture
def notifications() -> dict:
return {
"limit": 10,
"page": 1,
"sort": "",
"total_rows": 0,
"total_rows_in_this_page": 0,
"total_pages": 0,
"rows": [],
}
66 changes: 66 additions & 0 deletions tests/test_solar_plus_intelbras.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,69 @@ def test_shouldnt_return_alerts_with_end_date_invalid(
page=1,
)
assert str(exc.value) == "end_date must be in the format YYYY-MM-DD."


class TestSolarPlusIntelbrasNotifications:
def test_should_return_notifications(
self,
requests_mock: Mocker,
solar_plus_intelbras: SolarPlusIntelbras,
notifications: dict,
login_response: dict,
) -> None:
requests_mock.post(
"https://ens-server.intelbras.com.br/api/login",
json=login_response,
status_code=200,
)

requests_mock.get(
"https://ens-server.intelbras.com.br/api/user/notifications?pendings=True&page=1&start_date=2025-01-23&end_date=2025-01-23",
json=notifications,
status_code=200,
)
assert (
solar_plus_intelbras.notifications(
start_date="2025-01-23",
end_date="2025-01-23",
pendings=True,
page=1,
)
== notifications
)

def test_shouldnt_return_notifications_without_start_date(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.notifications()
assert str(exc.value) == "start_date must be in the format YYYY-MM-DD."

def test_shouldnt_return_notifications_with_start_date_invalid(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.notifications(
start_date="2025/01/23", end_date="2025-01-23"
)
assert str(exc.value) == "start_date must be in the format YYYY-MM-DD."

def test_shouldnt_return_notifications_without_end_date(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.notifications(start_date="2025-01-23")
assert str(exc.value) == "end_date must be in the format YYYY-MM-DD."

def test_shouldnt_return_notifications_with_end_date_invalid(
self,
solar_plus_intelbras: SolarPlusIntelbras,
) -> None:
with pytest.raises(Exception) as exc:
solar_plus_intelbras.notifications(
start_date="2025-01-23", end_date="2025/01/23"
)
assert str(exc.value) == "end_date must be in the format YYYY-MM-DD."

0 comments on commit 4e2a8f2

Please sign in to comment.