script.module.vk/lib/vk/api.py

39 lines
1.1 KiB
Python
Raw Normal View History

import logging
2022-03-12 17:08:13 +03:00
from vk.utils import stringify_values
logger = logging.getLogger('vk')
2022-03-12 17:08:13 +03:00
class APINamespace:
def __init__(self, api, method_common_params):
self._api = api
self._method_common_params = method_common_params
2022-03-12 17:08:13 +03:00
def __call__(self, method):
return APIMethod(self._api, method, self._method_common_params)
2022-03-12 17:08:13 +03:00
__getattr__ = __call__
2022-03-12 17:08:13 +03:00
class APIMethod:
def __init__(self, api, method, method_common_params):
self._api = api
2022-03-12 17:08:13 +03:00
self._method = method
self._method_common_params = method_common_params
2022-03-12 17:08:13 +03:00
def __getattr__(self, method):
return self.__class__(self._api, self._method + '.' + method, self._method_common_params)
2022-03-12 17:08:13 +03:00
def __call__(self, **method_params):
request_method_params = self._method_common_params.copy()
request_method_params.update(stringify_values(method_params))
2022-03-12 17:08:13 +03:00
return self._api.send(APIRequest(self._method, request_method_params))
2022-03-12 17:08:13 +03:00
class APIRequest:
def __init__(self, method, method_params):
self.method = method
self.method_params = method_params