diff options
| author | 2018-06-24 10:16:57 +0100 | |
|---|---|---|
| committer | 2018-06-24 10:16:57 +0100 | |
| commit | dee167e06e6ef1a4ae69fac346b1d1c75a6f98ef (patch) | |
| tree | cdb849e71aa6d8474e5d70d54ae8236d6343629d | |
| parent | [Config] Sort lines; add user complete api route (diff) | |
[Events] Implement guild member chunking for users endpoint
| -rw-r--r-- | bot/cogs/events.py | 62 |
1 files changed, 42 insertions, 20 deletions
diff --git a/bot/cogs/events.py b/bot/cogs/events.py index 145b43c2b..85fec3aa3 100644 --- a/bot/cogs/events.py +++ b/bot/cogs/events.py @@ -11,7 +11,7 @@ from bot.constants import ( Channels, DEBUG_MODE, Guild, Keys, Roles, URLs ) - +from bot.utils import chunks log = logging.getLogger(__name__) @@ -26,31 +26,53 @@ class Events: async def send_updated_users(self, *users, replace_all=False): users = list(filter(lambda user: str(Roles.verified) in user["roles"], users)) - response = None - try: - if replace_all: + for chunk in chunks(users, 1000): + response = None + + try: + if replace_all: + response = await self.bot.http_session.post( + url=URLs.site_user_api, + json=chunk, + headers={"X-API-Key": Keys.site_api} + ) + else: + response = await self.bot.http_session.put( + url=URLs.site_user_api, + json=chunk, + headers={"X-API-Key": Keys.site_api} + ) + + await response.json() # We do this to ensure we got a proper response from the site + except Exception: + if not response: + log.exception(f"Failed to send {len(chunk)} users") + else: + text = await response.text() + log.exception(f"Failed to send {len(chunk)} users", extra={"body": text}) + break # Stop right now, thank you very much + + result = {} + + if replace_all: + response = None + + try: response = await self.bot.http_session.post( - url=URLs.site_user_api, - json=users, - headers={"X-API-Key": Keys.site_api} - ) - else: - response = await self.bot.http_session.put( - url=URLs.site_user_api, - json=users, + url=URLs.site_user_complete_api, headers={"X-API-Key": Keys.site_api} ) - return await response.json() - except Exception: - if not response: - log.exception(f"Failed to send {len(users)} users") - else: - text = await response.text() - log.exception(f"Failed to send {len(users)} users | Body: \n\n{text}") + result = await response.json() + except Exception: + if not response: + log.exception(f"Failed to send {len(chunk)} users") + else: + text = await response.text() + log.exception(f"Failed to send {len(chunk)} users", extra={"body": text}) - return {} + return result async def send_delete_users(self, *users): try: |