def create_api(app_id=None, login=None, password=None, phone_number=None, scope='offline', api_version=None, http_params=None, interactive=False, service_token=None): """Factory method to explicitly create API with app_id, login, password and phone_number parameters. If the app_id, login, password are not passed, then token-free session will be created automatically :param app_id: int: vk application id, more info: https://vk.com/dev/main :param login: str: vk login :param password: str: vk password :param phone_number: str: phone number with country code (+71234568990) :param scope: str or list of str: vk session scope :param api_version: str: vk api version, check https://vk.com/dev/versions :param interactive: bool: flag which indicates to use InteractiveVKSession :param service_token: str: new way of querying vk api, instead of getting oauth token :param http_params: dict: requests http parameters passed along :return: api instance :rtype : vk_requests.api.API """ session = VKSession(app_id=app_id, user_login=login, user_password=password, phone_number=phone_number, scope=scope, service_token=service_token, api_version=api_version, interactive=interactive) return API(session=session, http_params=http_params)
def create_api(app_id=None, login=None, password=None, phone_number=None, timeout=10, scope='offline', api_version=None, interactive=False, **method_default_args): """Factory method to explicitly create API with app_id, login, password and phone_number parameters. If the app_id, login, password are not passed, then token-free session will be created automatically :param app_id: int: vk application id, more info: https://vk.com/dev/main :param login: str: vk login :param password: str: vk password :param phone_number: str: phone number with country code (+71234568990) :param timeout: int: api timeout in seconds :param scope: str or list of str: vk session scope :param api_version: str: vk api version, check https://vk.com/dev/versions :param interactive: bool: flag which indicates to use InteractiveVKSession :param method_default_args: api kwargs :return: api instance :rtype : vk_requests.api.API """ session_cls = InteractiveVKSession if interactive else VKSession session = session_cls(app_id=app_id, user_login=login, user_password=password, phone_number=phone_number, scope=scope, api_version=api_version) return API(session=session, timeout=timeout, **method_default_args)
def create_api(app_id=None, login=None, password=None, phone_number=None, timeout=10, scope='offline', api_version=None, session_cls=VKSession, stored_token=None, **method_default_args): """Factory method to explicitly create API with app_id, login, password and phone_number parameters. If the app_id, login, password are not passed, then token-free session will be created automatically :param app_id: int: vk application id, more info: https://vk.com/dev/main :param login: str: vk login :param password: str: vk password :param phone_number: str: phone number with country code (+71234568990) :param timeout: int: api timeout in seconds :param scope: str or list of str: vk session scope :param api_version: str: vk api version :param session_cls: VKSession: session implementation class :param method_default_args: api kwargs :return: api instance :rtype : vk_requests.api.API Passing StoredVKSession as vk session class with an active token could speed up the initial connection process, which is especially helpful when the callee could use many independent sessions in a relatively short period of time (1 day) and using singleton class for storing api instance is not an available option. If there is any possibility that provided token invalid, expired or could expire during session activity it would be much safer to provide app_id, login and password as well otherwise api will fail with ValueError. All changes were made with a primary intention to not to break existing code. example call: from vk_requests.auth import StoredVKSession api = vk_requests.create_api(app_id=app_id, login=login, password=password, stored_token=token, session_cls=StoredVKSession) Important: stored token should have same scope as callee passing to api factory :param stored_token: str: previously obtained, preferably valid token """ session = session_cls(app_id, login, password, phone_number=phone_number, scope=scope, api_version=api_version, stored_token=stored_token) return API(session=session, timeout=timeout, **method_default_args)
def test_interactive_session_init(self): session = InteractiveVKSession() api = API(session=session, timeout=10) self.assertIsInstance(api, API)