import asyncio
from tenacity import (retry, retry_if_exception_type, stop_after_attempt,
                      wait_chain, wait_fixed)

from .exceptions import (APIBadRequest, APIConnectionError, APIForbidden,
                         APIInactiveError, APIInvalidKey, APINotFound,
                         APIRateLimited)


class ApiMixin:
    async def get_guild(self, ctx, guild_id=None, guild_name=None):
        if guild_name:
            endpoint_id = "guild/search?name=" + guild_name.replace(' ', '%20')
            guild_id = await self.call_api(endpoint_id)
            guild_id = guild_id[0]
        elif not guild_id:
            if not ctx.guild:
                return None
            doc = await self.bot.database.get_guild(ctx.guild, self) or {}
            guild_id = doc.get("guild_ingame")
        if not guild_id:
            return None
        endpoint = "guild/{0}".format(guild_id)
        return await self.call_api(endpoint, ctx.author, ["guilds"])

    async def call_multiple(self,
                            endpoints,
                            user=None,
                            scopes=None,
                            key=None,
                            **kwargs):
        if key is None and user:
            doc = await self.fetch_key(user, scopes)
            key = doc["key"]
        tasks = []
        for e in endpoints:
            tasks.append(self.call_api(e, key=key, **kwargs))
        return await asyncio.gather(*tasks)

    @retry(retry=retry_if_exception_type(APIBadRequest),
           reraise=True,
           stop=stop_after_attempt(4),
           wait=wait_chain(wait_fixed(2), wait_fixed(4), wait_fixed(8)))
    async def call_api(self,
                       endpoint,
                       user=None,
                       scopes=None,
                       key=None,
                       schema_version=None,
                       schema_string=None):
        headers = {
            'User-Agent': "GW2Bot - a Discord bot",
            'Accept': 'application/json'
        }
        if key:
            headers.update({"Authorization": "Bearer " + key})
        if user:
            doc = await self.fetch_key(user, scopes)
            headers.update({"Authorization": "Bearer " + doc["key"]})
        if schema_version:
            schema = schema_version.replace(microsecond=0).isoformat() + "Z"
            headers.update({"X-Schema-Version": schema})
        if schema_string:
            headers.update({"X-Schema-Version": schema_string})
        apiserv = 'https://api.guildwars2.com/v2/'
        url = apiserv + endpoint
        async with self.session.get(url, headers=headers) as r:
            if r.status != 200 and r.status != 206:
                try:
                    err = await r.json()
                    err_msg = err["text"]
                except:
                    err_msg = ""
                if r.status == 400:
                    if err_msg == "invalid key":
                        raise APIInvalidKey("Invalid key")
                    raise APIBadRequest("Bad request")
                if r.status == 404:
                    raise APINotFound("Not found")
                if r.status == 403:
                    if err_msg == "invalid key":
                        raise APIInvalidKey("Invalid key")
                    raise APIForbidden("Access denied")
                if r.status == 503 and err_msg == "API not active":
                    raise APIInactiveError("API is dead")
                if r.status == 429:
                    self.log.error("API Call limit saturated")
                    raise APIRateLimited(
                        "Requests limit has been saturated. Try again later.")
                else:
                    raise APIConnectionError("{} {}".format(r.status, err_msg))
            return await r.json()

@@ -337,644 +337,8 @@
 in:%0A
-    async def get_guild(self, ctx, guild_id=None, guild_name=None):%0A        if guild_name:%0A            endpoint_id = %22guild/search?name=%22 + guild_name.replace(' ', '%2520')%0A            guild_id = await self.call_api(endpoint_id)%0A            guild_id = guild_id%5B0%5D%0A        elif not guild_id:%0A            if not ctx.guild:%0A                return None%0A            doc = await self.bot.database.get_guild(ctx.guild, self) or %7B%7D%0A            guild_id = doc.get(%22guild_ingame%22)%0A        if not guild_id:%0A            return None%0A        endpoint = %22guild/%7B0%7D%22.format(guild_id)%0A        return await self.call_api(endpoint, ctx.author, %5B%22guilds%22%5D)%0A%0A
     

Move get_guild from api.py