From 345573c6f7e63e88d8dc2910c3df39d55b49f6d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Tue, 19 Nov 2019 10:38:22 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Added=20a=20way=20to=20get=20encounters=20p?= =?UTF-8?q?er=20Pok=C3=A9mon?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/usage.md | 1 + pokepy/api.py | 13 ++++++++++--- pokepy/resources_v2.py | 6 ++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 0c3c1f8..a955fe1 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -40,6 +40,7 @@ The following is an exhaustive list of all the endpoints with links to their res * [get_item_pocket](https://pokeapi.co/docs/v2.html/#item-pockets) * [get_location](https://pokeapi.co/docs/v2.html/#locations) * [get_location_area](https://pokeapi.co/docs/v2.html/#location-areas) +* [get_location_area_encounter](https://pokeapi.co/docs/v2.html/#pokemon) * [get_pal_park_area](https://pokeapi.co/docs/v2.html/#pal-park-areas) * [get_region](https://pokeapi.co/docs/v2.html/#regions) * [get_machine](https://pokeapi.co/docs/v2.html/#machines) diff --git a/pokepy/api.py b/pokepy/api.py index 9f589ab..38ce967 100644 --- a/pokepy/api.py +++ b/pokepy/api.py @@ -74,7 +74,8 @@ class Meta(BaseClient.Meta): rv2.PokemonSpeciesResource, rv2.StatResource, rv2.TypeResource, - rv2.LanguageResource + rv2.LanguageResource, + rv2.LocationAreaEncounterSubResource ) def __init__(self, cache=None, cache_location=None, *args, **kwargs): @@ -146,13 +147,19 @@ def _assign_method(self, resource_class, method_type): 'valid_status_codes', DEFAULT_VALID_STATUS_CODES ) + always_list = getattr( + resource_class.Meta, + 'always_list', + False + ) def extract_single_element_list(func): @functools.wraps(func) def inner(*args, **kwargs): final = func(*args, **kwargs) - if isinstance(final, list) and len(final) == 1: - final = final[0] + if not always_list: + if isinstance(final, list) and len(final) == 1: + final = final[0] return final return inner diff --git a/pokepy/resources_v2.py b/pokepy/resources_v2.py index 1f8dd79..5cc0751 100644 --- a/pokepy/resources_v2.py +++ b/pokepy/resources_v2.py @@ -852,7 +852,9 @@ def __repr__(self): class LocationAreaEncounterSubResource(BaseResource): class Meta(BaseResource.Meta): name = 'Location_Area_Encounter' + resource_name = 'pokemon' identifier = 'location_area' + always_list = True subresources = { 'location_area': NamedAPIResourceSubResource, 'version_details': VersionEncounterDetailSubResource @@ -861,6 +863,10 @@ class Meta(BaseResource.Meta): def __repr__(self): return '<%s>' % self.Meta.name + @classmethod + def get_url(cls, url, uid, **kwargs): + return '%s/%s/encounters' % (url, uid) + class PokemonFormSpritesSubResource(SubResource): class Meta(BaseResource.Meta): From 98b2d68005cc325122fe26d743be6041c660fe76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Tue, 19 Nov 2019 23:03:54 +0100 Subject: [PATCH 2/2] Removed unneeded condition --- pokepy/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pokepy/api.py b/pokepy/api.py index 38ce967..6224ea2 100644 --- a/pokepy/api.py +++ b/pokepy/api.py @@ -158,7 +158,7 @@ def extract_single_element_list(func): def inner(*args, **kwargs): final = func(*args, **kwargs) if not always_list: - if isinstance(final, list) and len(final) == 1: + if len(final) == 1: final = final[0] return final return inner