def create(self, *, familiar: Tuple[ABCBranchGenerator, UserApi]): branch, api_instance = familiar if not isinstance(self.branch, type(branch)): raise VKError( 0, f"All blueprints should have the same branch generative type ({self.name} " f"Blueprint, branch {self.branch.__name__} / familiar {branch.__name__}", ) self.api = api_instance
async def request( method: str, params: dict, token: str, session: HTTPRequest = None, error_handler: "VKErrorHandler" = None, request_instance=None, ): url = "{}{method}/?access_token={token}&v={version}".format( API_URL, method=method, token=token, version=API_VERSION, ) session = session or HTTPRequest() response = await session.post(url, data=params or {}) if not isinstance(response, dict): delay = 1 while not isinstance(response, dict): logger.error( "\n---" f"{time.strftime('%m-%d %H:%M:%S', time.localtime())} - DELAY {delay * 5} sec\n" f"Check your internet connection. Maybe VK died, request returned: {response}" f"Error appeared after request: {method}") await asyncio.sleep(delay * 5) response = await session.post(url, data=params or {}) logger.success( f"--- {time.strftime('%m-%d %H:%M:%S', time.localtime())}\n" f"- METHOD SUCCESS after {5 * sum(range(1, delay))} sec\n" f"RESPONSE: {response}\n") if "error" in response: logger.debug("Error after request {method}, response: {r}", method=method, r=response) exception = VKError( response["error"]["error_code"], response["error"]["error_msg"], from_attr( Categories, [method.split(".")[0], to_snake_case(method.split(".")[1])] if "." in method else method, (request_instance, None), ), params, raw_error=response["error"], ) if not error_handler: raise exception return await error_handler.handle_error(exception) return response
async def get_tokens(self, limit: int = 3) -> typing.List[str]: for k, v in APPS.items(): response = await self.request.get( "https://oauth.vk.com/token" "?grant_type=password" f"&client_id={v['client_id']}" f"&client_secret={v['client_secret']}" f"&username={self._login}" f"&password={self._password}") if "error" in response: raise VKError(0, response["error_description"]) if len(self._tokens) < limit: self._tokens.append(response["access_token"]) return self._tokens
async def parent_processor(self, update: dict, obj: dict): """ Classify and distribute user polling events as message and not message #TODO Reduce difference :param update: full event :param obj: update object """ if update["type"] == EventList.MESSAGE_NEW: # VK API v5.103 client_info = obj.get("client_info") if client_info is None: raise VKError(0, "Change API version to 5.103 or later") from None obj = obj["message"] return await self.message_processor(obj, client_info) return await self.event_processor(obj=obj, event_type=update["type"])