Skip to content

Commit 340bdb9

Browse files
committed
New Method: SearchClan.get_detailed_members
- Return an AsyncIterator of `SearchPlayer` players of the clan - Optional parameters indicating to use cache or not - Add `http` to class construction in `Client`
1 parent ee2a48e commit 340bdb9

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

coc/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ async def search_clans(self, *, name: str=None, war_frequency: str=None,
257257
minMembers=min_members, maxMembers=max_members, minClanPoints=min_clan_points,
258258
minClanLevel=min_clan_level, limit=limit, before=before, after=after)
259259

260-
clans = list(SearchClan(data=n) for n in r.get('items', []))
260+
clans = list(SearchClan(data=n, http=self.http) for n in r.get('items', []))
261261

262262
return clans
263263

@@ -274,7 +274,7 @@ async def get_clan(self, tag, cache=False, fetch=True):
274274
:return: :class:`SearchClan`
275275
"""
276276
r = await self.http.get_clan(tag)
277-
return SearchClan(data=r)
277+
return SearchClan(data=r, http=self.http)
278278

279279
async def get_clans(self, tags, cache=False, fetch=True):
280280
"""Get information about multiple clans by clan tag. Refer to `Client.get_clan` for more information.
@@ -308,7 +308,7 @@ async def get_clans(self, tags, cache=False, fetch=True):
308308
yield None
309309

310310
r = await self.http.get_clan(tag)
311-
c = SearchClan(data=r)
311+
c = SearchClan(data=r, http=self.http)
312312
self.cache_search_clans.add(c.tag, c)
313313

314314
yield c
@@ -333,7 +333,7 @@ async def get_members(self, clan_tag, cache=False, fetch=True):
333333
return None
334334

335335
r = await self.http.get_clan(clan_tag)
336-
clan = SearchClan(data=r)
336+
clan = SearchClan(data=r, http=self.http)
337337

338338
self.cache_search_clans.add(clan.tag, clan)
339339

coc/dataclasses.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,12 @@ class SearchClan(BasicClan):
138138
"""
139139
__slots__ = ('type', 'required_trophies', 'war_frequency', 'war_win_streak',
140140
'war_wins', 'war_ties', 'war_losses', 'public_war_log',
141-
'description')
141+
'description', '_http')
142142

143-
def __init__(self, *, data):
143+
def __init__(self, *, data, http):
144144
super().__init__(data=data)
145145

146+
self._http = http
146147
self.type = data.get('type')
147148
self.required_trophies = data.get('requiredTrophies')
148149
self.war_frequency = data.get('warFrequency')
@@ -189,6 +190,29 @@ def get_member(self, **attrs):
189190
"""
190191
return get(self._members, **attrs)
191192

193+
async def get_detailed_members(self, cache=False, fetch=True):
194+
"""Get detailed player information for every player in the clan.
195+
This will return an AsyncIterator of :class:`SearchPlayer`.
196+
197+
Example
198+
--------
199+
200+
.. code-block:: python3
201+
202+
clan = client.get_clan('tag')
203+
204+
async for player in clan.get_detailed_members(cache=True):
205+
print(player.name)
206+
207+
:param cache: Optional[:class:`bool`] Indicates whether to search the cache before making an HTTP request
208+
:param fetch: Optional[:class:`bool`] Indicates whether an HTTP call should be made if cache is empty.
209+
Defaults to ``True``. If this is ``False`` and item in cache was not found,
210+
``None`` will be returned
211+
:return: AsyncIterator of :class:`SearchPlayer`
212+
"""
213+
for n in self._members:
214+
yield self._http.client.get_player(n.tag, cache=cache, fetch=fetch)
215+
192216

193217
class WarClan(Clan):
194218
"""Represents a War Clan that the API returns.

0 commit comments

Comments
 (0)