Skip to content

Commit

Permalink
Merge pull request #265 from melvio/236-support-graceful-degradation-…
Browse files Browse the repository at this point in the history
…for-new-pets-and-new-potions

236 support graceful degradation for new pets and new potions
  • Loading branch information
melvio authored Sep 23, 2024
2 parents a1e9c22 + d53f01b commit 58ad4d4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 38 deletions.
15 changes: 3 additions & 12 deletions src/hopla/cli/hatch_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from hopla.hoplalib.user.usercontroller import HabiticaUserRequest
from hopla.hoplalib.user.usermodels import HabiticaUser
from hopla.hoplalib.zoo.foodmodels import FeedStatus
from hopla.hoplalib.zoo.petmodels import Pet, InvalidPet
from hopla.hoplalib.zoo.petmodels import Pet

log = logging.getLogger()

Expand Down Expand Up @@ -55,7 +55,7 @@ def hatch_all(no_interactive: bool) -> None:
user: HabiticaUser = HabiticaUserRequest().request_user_data_or_exit()
eggs = EggCollection(user.get_eggs())
potions = HatchPotionCollection(user.get_hatch_potions())
pets: List[Pet] = to_pet_list(user.get_pets(), dont_raise_on_unrecognized_pets=True)
pets: List[Pet] = to_pet_list(user.get_pets())

plan_maker = HatchPlanMaker(
egg_collection=eggs, hatch_potion_collection=potions, pets=pets
Expand Down Expand Up @@ -107,19 +107,10 @@ def _hatch_eggs_without_confirmation(plan: HatchPlan) -> None:
f"{response_json['error']}: {response_json['message']}")


def to_pet_list(pets: Dict[str, int], dont_raise_on_unrecognized_pets=False) -> List[Pet]:
def to_pet_list(pets: Dict[str, int]) -> List[Pet]:
"""
Helper method that takes a pet_dict and returns a List[Pet].
:param pets: dictionary of pets
:param dont_raise_on_unrecognized_pets: if True, swallow InvalidPet exceptions
:return: corresponding List[Pet]
"""
result: List[Pet] = []
if dont_raise_on_unrecognized_pets:
for (pet_name, feed_status) in pets.items():
try:
result.append(Pet(pet_name, feed_status=FeedStatus(feed_status)))
except InvalidPet as exc:
log.info(f"{pet_name=} not supported,skipping.", exc_info=exc)
return result
return [Pet(name, feed_status=FeedStatus(n)) for (name, n) in pets.items()]
4 changes: 2 additions & 2 deletions src/hopla/cli/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def request(method: str,
show_status_code_flag: bool,
show_response_flag: bool,
path: str):
# pylint: disable=too-many-arguments
"""Perform a HTTP request on the Habitica API.
# pylint: disable=too-many-arguments,too-many-positional-arguments
"""Perform an HTTP request on the Habitica API.
PATH This is the path of the endpoint that you want to perform a HTTP
request on. For example, /api/v3/groups.
Expand Down
10 changes: 7 additions & 3 deletions src/hopla/hoplalib/hatchery/eggmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Module with models for eggs.
"""
import logging
from dataclasses import dataclass, field
from typing import Any, Dict, Iterator

Expand Down Expand Up @@ -62,9 +63,12 @@ class EggCollection:

def __post_init__(self):
"""Use the given eggs to create __eggs."""
self.__eggs: Dict[str, Egg] = {
name: Egg(name, quantity=quantity) for (name, quantity) in self.eggs.items()
}
self.__eggs: Dict[str, Egg] = {}
for name, n in self.eggs.items():
if name in EggData.egg_names:
self.__eggs[name] = Egg(name, quantity=n)
else:
logging.error(f"{name} is not yet a supported egg name")

def __len__(self) -> int:
return len(self.__eggs)
Expand Down
21 changes: 0 additions & 21 deletions src/tests/cli/test_hatch_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,3 @@ def test_to_pet_list_fail(self):

with self.assertRaises(InvalidPet):
to_pet_list(pets)

def test_to_pet_list_ok_dont_raise(self):
pet1, feed_status1 = "Wolf-Base", 5
pet2, feed_status2 = "Axolotl-Red", 10
pet3, feed_status3 = "Fox-Golden", -1
pet4, feed_status4 = "Giraffe-doesnt-exist", 0

pets: Dict[str, int] = {
pet1: feed_status1, pet2: feed_status2,
pet3: feed_status3, pet4: feed_status4
}

result: List[Pet] = to_pet_list(pets, dont_raise_on_unrecognized_pets=True)

expected: List[Pet] = [
Pet(pet1, feed_status=FeedStatus(feed_status1)),
Pet(pet2, feed_status=FeedStatus(feed_status2)),
Pet(pet3, feed_status=FeedStatus(feed_status3))
]

assert result == expected

0 comments on commit 58ad4d4

Please sign in to comment.