Skip to content
This repository was archived by the owner on Aug 19, 2018. It is now read-only.

Commit a3bf8f3

Browse files
committed
add support for multiple clan requests
1 parent 078ecaf commit a3bf8f3

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

crasync/core.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import aiohttp
22
import asyncio
3-
from .models import Profile, Clan, Constants
3+
from .models import Profile, Clan, Clans, Constants
44

55

66
class Client:
@@ -33,20 +33,36 @@ async def get_profile(self, tag):
3333

3434
return Profile(self, data)
3535

36-
37-
async def get_clan(self, tag):
38-
'''Get a clan object using a tag'''
39-
40-
url = f'{self.BASE}/clan/{tag}'
41-
36+
async def get_clans(self, *, tags):
37+
'''Get a clan object using tag(s)'''
38+
tags = tags.split(',')
39+
40+
if len(tags) == 1:
41+
url = f'{self.BASE}/clan/{tags[0]}'
42+
async with self.session.get(url) as resp:
43+
if resp.status == 200:
44+
data = await resp.json()
45+
else:
46+
print('API is down. Please be patient.')
47+
return None
48+
return Clan(self, data)
49+
50+
taglist = ''
51+
i = 0
52+
for tag in tags:
53+
i += 1
54+
taglist += tag[1:]
55+
if i != len(tags):
56+
taglist += ','
57+
58+
url = f'{self.BASE}/clan/{taglist}'
4259
async with self.session.get(url) as resp:
4360
if resp.status == 200:
4461
data = await resp.json()
4562
else:
4663
print('API is down. Please be patient.')
4764
return None
48-
49-
return Clan(self, data)
65+
return Clans(self, data)
5066

5167
async def get_constants(self):
5268
'''Get a profile object using a tag.'''

crasync/models.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ def __init__(self, client, data):
99
self.raw_data = data
1010
self.name = data.get('name')
1111
self.tag = data.get('tag')
12-
#self.from_data(data)
12+
self.from_data(data)
1313
endpoint = type(self).__name__.lower()
1414
self.url = f'{client.BASE}/{endpoint}/{self.tag}'
1515

1616
def __str__(self):
1717
return f'{self.name} (#{self.tag})'
1818

19-
#async def from_data(self):
20-
#raise NotImplementedError
19+
async def from_data(self):
20+
raise NotImplemented
2121

2222
async def update(self):
2323

2424
async with self.client.session.get(self.url) as resp:
2525
data = await resp.json()
2626

2727
self.raw_data = data
28-
#self.from_data(data)
28+
self.from_data(data)
2929

3030
return self
3131

@@ -173,6 +173,10 @@ def badge_url(self):
173173
def __repr__(self):
174174
return f'<Clan tag={self.tag}>'
175175

176+
class Clans(Base):
177+
def from_data(self, data):
178+
self.clans = [Clan(self.client, c) for c in data]
179+
176180
class Profile(Base):
177181
'''Represents a player profile.
178182
Includes a clan maybe? (requires a seperate request tho)

0 commit comments

Comments
 (0)