Exemple #1
0
    async def query(self, query):
        self._num_queries += 1
        logger.debug("Num Queries: %d", self._num_queries)

        query = f"""
        query {{
            {query}
        }}"""

        await self.ensure_auth()

        async with aiohttp.ClientSession() as session:
            async with session.get(url=self.URL_API,
                                   json={"query": query},
                                   headers=self.headers) as resp:

                try:
                    result = await resp.json()
                except Exception as e:
                    logger.error(resp)
                    raise e

                # some reports are private.. but still show up in rankings..
                # lets just see what happens
                # ----> it stops..
                # "You do not have permission to view this report"
                # TODO: figure out how to skip those reports..
                if result.get("error"):
                    raise ValueError(result.get("error"))

                if result.get("errors"):
                    msg = ""
                    for error in result.get("errors"):
                        message = error.get("message")

                        if message == ERROR_MESSAGE_INVALID_REPORT:
                            raise InvalidReport()

                        # TODO: find a way to not hardcode the error message
                        if message == "You do not have permission to view this report.":
                            raise PermissionError("Private Report!")

                        msg += "\n" + error.get(
                            "message") + " path:" + "/".join(
                                error.get("path", []))

                    if msg:
                        print(query)
                        raise ValueError(msg)

                return result.get("data", {})
Exemple #2
0
    def get_instance(cls, *args, **kwargs) -> "WarcraftlogsClient":
        """Get an instance of the Client.

        This is a singleton-style wrapper,
        which will return an existing instance, if there is one,
        or create a new instance otherwise.

        Args:
            *args, **kwargs passed to the __init__

        Returns:
            <WarcraftlogsClient> instance.

        """
        if cls._instance is None:
            logger.debug("creating new WarcraftlogsClient")
            cls._instance = cls(*args, **kwargs)
        return cls._instance
Exemple #3
0
    async def update_auth_token(self):
        """Request a new Auth Token from Warcraftlogs."""
        data = {
            "grant_type": "client_credentials",
            "client_id": self.client_id,
            "client_secret": self.client_secret,
        }
        logger.debug("Auth: %s", self.client_id)
        async with aiohttp.ClientSession() as session:
            async with session.post(url=self.URL_AUTH, data=data) as resp:

                try:
                    data = await resp.json()
                except Exception as e:
                    logger.error(resp)
                    raise e

        token = data.get("access_token", "")
        self.headers["Authorization"] = "Bearer " + token
Exemple #4
0
 def failed(self, event):
     logger.debug(
         "{0.command_name} failed in {0.duration_micros:g}μs".format(event))
Exemple #5
0
 def started(self, event):
     logger.debug("{0.command_name} start".format(event))